Use an interface type to pass the list entries
This commit is contained in:
parent
c22258a219
commit
2f1a186ce2
@ -4,6 +4,11 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ListEntry interface {
|
||||||
|
String() string
|
||||||
|
Attributes() Attributes
|
||||||
|
}
|
||||||
|
|
||||||
type ListWidget struct {
|
type ListWidget struct {
|
||||||
Constraints Constraints
|
Constraints Constraints
|
||||||
Attrs Attributes
|
Attrs Attributes
|
||||||
@ -15,7 +20,9 @@ type ListWidget struct {
|
|||||||
border bool
|
border bool
|
||||||
|
|
||||||
listLock sync.Mutex
|
listLock sync.Mutex
|
||||||
list [][]rune
|
list [][]Cell
|
||||||
|
|
||||||
|
listBuf [][]Cell
|
||||||
|
|
||||||
lastSize Size
|
lastSize Size
|
||||||
}
|
}
|
||||||
@ -56,7 +63,6 @@ func (l *ListWidget) Render() [][]Cell {
|
|||||||
c.Value = ' '
|
c.Value = ' '
|
||||||
l.cells[y] = append(l.cells[y], c)
|
l.cells[y] = append(l.cells[y], c)
|
||||||
} else {
|
} else {
|
||||||
l.cells[y][x].Attrs = l.Attrs
|
|
||||||
l.cells[y][x].Pos.X = x
|
l.cells[y][x].Pos.X = x
|
||||||
l.cells[y][x].Pos.Y = y
|
l.cells[y][x].Pos.Y = y
|
||||||
}
|
}
|
||||||
@ -68,21 +74,29 @@ func (l *ListWidget) Render() [][]Cell {
|
|||||||
return append([][]Cell(nil), l.cells...)
|
return append([][]Cell(nil), l.cells...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ListWidget) SetList(list []string) {
|
func (l *ListWidget) AddEntry(entry ListEntry) {
|
||||||
buf := make([][]rune, len(list))
|
if l.listBuf == nil {
|
||||||
|
l.listBuf = make([][]Cell, 1)
|
||||||
for i, s := range list {
|
} else {
|
||||||
buf[i] = make([]rune, 0)
|
l.listBuf = append(l.listBuf, []Cell{})
|
||||||
|
|
||||||
for _, r := range s {
|
|
||||||
buf[i] = append(buf[i], r)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.listLock.Lock()
|
||||||
l.list = buf
|
l.list = append([][]Cell{}, l.listBuf...)
|
||||||
l.listLock.Unlock()
|
l.listLock.Unlock()
|
||||||
|
l.listBuf = nil
|
||||||
l.recalculateCells()
|
l.recalculateCells()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,15 +139,15 @@ func (l *ListWidget) recalculateCells() {
|
|||||||
border = 1
|
border = 1
|
||||||
|
|
||||||
top := make([]Cell, longest+2)
|
top := make([]Cell, longest+2)
|
||||||
top[0] = Cell{Value: '┏'}
|
top[0] = Cell{Value: '┏', Attrs: l.Attrs}
|
||||||
top[longest+1] = Cell{Value: '┓'}
|
top[longest+1] = Cell{Value: '┓', Attrs: l.Attrs}
|
||||||
for i := 1; i <= longest; i++ {
|
for i := 1; i <= longest; i++ {
|
||||||
top[i] = Cell{Value: '━'}
|
top[i] = Cell{Value: '━', Attrs: l.Attrs}
|
||||||
}
|
}
|
||||||
|
|
||||||
bottom := append([]Cell(nil), top...)
|
bottom := append([]Cell(nil), top...)
|
||||||
bottom[0] = Cell{Value: '┗'}
|
bottom[0] = Cell{Value: '┗', Attrs: l.Attrs}
|
||||||
bottom[longest+1] = Cell{Value: '┛'}
|
bottom[longest+1] = Cell{Value: '┛', Attrs: l.Attrs}
|
||||||
|
|
||||||
cells = append([][]Cell{top}, cells...)
|
cells = append([][]Cell{top}, cells...)
|
||||||
cells = append(cells, bottom)
|
cells = append(cells, bottom)
|
||||||
@ -150,7 +164,7 @@ func (l *ListWidget) recalculateCells() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if l.border {
|
if l.border {
|
||||||
c := Cell{Value: '┃'}
|
c := Cell{Value: '┃', Attrs: l.Attrs}
|
||||||
cells[i+border][0] = c
|
cells[i+border][0] = c
|
||||||
cells[i+border][longest+1] = c
|
cells[i+border][longest+1] = c
|
||||||
}
|
}
|
||||||
@ -160,7 +174,7 @@ func (l *ListWidget) recalculateCells() {
|
|||||||
if j > start+len(s)-1 || j < start {
|
if j > start+len(s)-1 || j < start {
|
||||||
c.Value = ' '
|
c.Value = ' '
|
||||||
} else {
|
} else {
|
||||||
c.Value = s[j-start]
|
c = s[j-start]
|
||||||
}
|
}
|
||||||
|
|
||||||
cells[i+border][j+border] = c
|
cells[i+border][j+border] = c
|
||||||
|
18
list_entry.go
Normal file
18
list_entry.go
Normal 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
|
||||||
|
}
|
21
spectator.go
21
spectator.go
@ -81,7 +81,7 @@ func main() {
|
|||||||
window.Constraints.Bottom = true
|
window.Constraints.Bottom = true
|
||||||
topAsks.SetBorder(true)
|
topAsks.SetBorder(true)
|
||||||
topAsks.SetRightAlign(true)
|
topAsks.SetRightAlign(true)
|
||||||
topAsks.Attrs.ForegroundColor = exhibit.FGRed
|
topAsks.Attrs.ForegroundColor = exhibit.FGYellow
|
||||||
|
|
||||||
topBids = &exhibit.ListWidget{}
|
topBids = &exhibit.ListWidget{}
|
||||||
topBids.SetBorder(true)
|
topBids.SetBorder(true)
|
||||||
@ -168,7 +168,6 @@ func main() {
|
|||||||
num := numOfOrderPerSide(sz.Y)
|
num := numOfOrderPerSide(sz.Y)
|
||||||
|
|
||||||
aIt := asks.Iterator()
|
aIt := asks.Iterator()
|
||||||
s := make([]string, num)
|
|
||||||
|
|
||||||
var low, high decimal.Decimal
|
var low, high decimal.Decimal
|
||||||
for i := num - 1; i >= 0; i-- {
|
for i := num - 1; i >= 0; i-- {
|
||||||
@ -177,13 +176,15 @@ func main() {
|
|||||||
entries := aIt.Value().(Entries)
|
entries := aIt.Value().(Entries)
|
||||||
price, size := flatten(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 {
|
if i == num-1 {
|
||||||
low = price
|
low = price
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
topAsks.SetList(append([]string{}, s...))
|
topAsks.Commit()
|
||||||
|
|
||||||
bIt := bids.Iterator()
|
bIt := bids.Iterator()
|
||||||
for i := 0; i < num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
@ -192,22 +193,20 @@ func main() {
|
|||||||
entries := bIt.Value().(Entries)
|
entries := bIt.Value().(Entries)
|
||||||
price, size := flatten(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 {
|
if i == 0 {
|
||||||
high = price
|
high = price
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
topBids.SetList(append([]string(nil), s...))
|
topBids.Commit()
|
||||||
|
|
||||||
diff := low.Sub(high)
|
diff := low.Sub(high)
|
||||||
|
|
||||||
s = []string{"", fmt.Sprintf("%v Spread: %v",
|
midPrice.AddEntry(ListEntry{Value: high.Add(diff.Div(decimal.
|
||||||
high.Add(diff.Div(decimal.New(2, 0))).StringFixed(3),
|
New(2, 0))).StringFixed(3)})
|
||||||
diff.StringFixed(2)), ""}
|
midPrice.Commit()
|
||||||
|
|
||||||
midPrice.SetList(append([]string(nil), s...))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user