Use an interface type to pass the list entries

This commit is contained in:
Kevin Cotugno 2017-12-27 00:20:41 -08:00
parent c22258a219
commit 2f1a186ce2
3 changed files with 63 additions and 32 deletions

View File

@ -4,6 +4,11 @@ import (
"sync"
)
type ListEntry interface {
String() string
Attributes() Attributes
}
type ListWidget struct {
Constraints Constraints
Attrs Attributes
@ -15,7 +20,9 @@ type ListWidget struct {
border bool
listLock sync.Mutex
list [][]rune
list [][]Cell
listBuf [][]Cell
lastSize Size
}
@ -56,7 +63,6 @@ func (l *ListWidget) Render() [][]Cell {
c.Value = ' '
l.cells[y] = append(l.cells[y], c)
} else {
l.cells[y][x].Attrs = l.Attrs
l.cells[y][x].Pos.X = x
l.cells[y][x].Pos.Y = y
}
@ -68,21 +74,29 @@ func (l *ListWidget) Render() [][]Cell {
return append([][]Cell(nil), l.cells...)
}
func (l *ListWidget) SetList(list []string) {
buf := make([][]rune, len(list))
for i, s := range list {
buf[i] = make([]rune, 0)
for _, r := range s {
buf[i] = append(buf[i], r)
}
func (l *ListWidget) AddEntry(entry ListEntry) {
if l.listBuf == nil {
l.listBuf = make([][]Cell, 1)
} else {
l.listBuf = append(l.listBuf, []Cell{})
}
l.listLock.Lock()
l.list = buf
l.listLock.Unlock()
index := len(l.listBuf) - 1
for _, r := range entry.String() {
cell := Cell{}
cell.Value = r
cell.Attrs = entry.Attributes()
l.listBuf[index] = append(l.listBuf[index], cell)
}
}
func (l *ListWidget) Commit() {
l.listLock.Lock()
l.list = append([][]Cell{}, l.listBuf...)
l.listLock.Unlock()
l.listBuf = nil
l.recalculateCells()
}
@ -125,15 +139,15 @@ func (l *ListWidget) recalculateCells() {
border = 1
top := make([]Cell, longest+2)
top[0] = Cell{Value: '┏'}
top[longest+1] = Cell{Value: '┓'}
top[0] = Cell{Value: '┏', Attrs: l.Attrs}
top[longest+1] = Cell{Value: '┓', Attrs: l.Attrs}
for i := 1; i <= longest; i++ {
top[i] = Cell{Value: '━'}
top[i] = Cell{Value: '━', Attrs: l.Attrs}
}
bottom := append([]Cell(nil), top...)
bottom[0] = Cell{Value: '┗'}
bottom[longest+1] = Cell{Value: '┛'}
bottom[0] = Cell{Value: '┗', Attrs: l.Attrs}
bottom[longest+1] = Cell{Value: '┛', Attrs: l.Attrs}
cells = append([][]Cell{top}, cells...)
cells = append(cells, bottom)
@ -150,7 +164,7 @@ func (l *ListWidget) recalculateCells() {
}
if l.border {
c := Cell{Value: '┃'}
c := Cell{Value: '┃', Attrs: l.Attrs}
cells[i+border][0] = c
cells[i+border][longest+1] = c
}
@ -160,7 +174,7 @@ func (l *ListWidget) recalculateCells() {
if j > start+len(s)-1 || j < start {
c.Value = ' '
} else {
c.Value = s[j-start]
c = s[j-start]
}
cells[i+border][j+border] = c

18
list_entry.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"git.kevincotugno.com/kcotugno/spectator/exhibit"
)
type ListEntry struct {
Value string
Attrs exhibit.Attributes
}
func (e ListEntry) String() string {
return e.Value
}
func (e ListEntry) Attributes() exhibit.Attributes {
return e.Attrs
}

View File

@ -81,7 +81,7 @@ func main() {
window.Constraints.Bottom = true
topAsks.SetBorder(true)
topAsks.SetRightAlign(true)
topAsks.Attrs.ForegroundColor = exhibit.FGRed
topAsks.Attrs.ForegroundColor = exhibit.FGYellow
topBids = &exhibit.ListWidget{}
topBids.SetBorder(true)
@ -168,7 +168,6 @@ func main() {
num := numOfOrderPerSide(sz.Y)
aIt := asks.Iterator()
s := make([]string, num)
var low, high decimal.Decimal
for i := num - 1; i >= 0; i-- {
@ -177,13 +176,15 @@ func main() {
entries := aIt.Value().(Entries)
price, size := flatten(entries)
s[i] = fmt.Sprintf("%v - %v ", price.StringFixed(2), size.StringFixed(8))
topAsks.AddEntry(ListEntry{Value: size.StringFixed(8),
Attrs: exhibit.Attributes{ForegroundColor:
exhibit.FGBlue}})
if i == num-1 {
low = price
}
}
topAsks.SetList(append([]string{}, s...))
topAsks.Commit()
bIt := bids.Iterator()
for i := 0; i < num; i++ {
@ -192,22 +193,20 @@ func main() {
entries := bIt.Value().(Entries)
price, size := flatten(entries)
s[i] = fmt.Sprintf("%v - %v ", price.StringFixed(2), size.StringFixed(8))
topBids.AddEntry(ListEntry{Value: size.StringFixed(8)})
if i == 0 {
high = price
}
}
topBids.SetList(append([]string(nil), s...))
topBids.Commit()
diff := low.Sub(high)
s = []string{"", fmt.Sprintf("%v Spread: %v",
high.Add(diff.Div(decimal.New(2, 0))).StringFixed(3),
diff.StringFixed(2)), ""}
midPrice.SetList(append([]string(nil), s...))
midPrice.AddEntry(ListEntry{Value: high.Add(diff.Div(decimal.
New(2, 0))).StringFixed(3)})
midPrice.Commit()
}
}