Add all widget back to the ui

This commit is contained in:
Kevin Cotugno 2017-12-30 22:55:36 -08:00
parent 166245fccd
commit df3c9456c1
6 changed files with 147 additions and 64 deletions

View File

@ -14,9 +14,6 @@ type ListWidget struct {
blockLock sync.Mutex blockLock sync.Mutex
block Block block Block
attributesLock sync.Mutex
attributes Attributes
rightAlignLock sync.Mutex rightAlignLock sync.Mutex
rightAlign bool rightAlign bool
@ -29,21 +26,6 @@ type ListWidget struct {
listBuf [][]Cell listBuf [][]Cell
} }
func (l *ListWidget) Attributes() Attributes {
l.attributesLock.Lock()
defer l.attributesLock.Unlock()
return l.attributes
}
func (l *ListWidget) SetAttributes(a Attributes) {
l.attributesLock.Lock()
l.attributes = a
l.attributesLock.Unlock()
l.recalculateCells()
}
func (l *ListWidget) Size() image.Point { func (l *ListWidget) Size() image.Point {
l.blockLock.Lock() l.blockLock.Lock()
defer l.blockLock.Unlock() defer l.blockLock.Unlock()

View File

@ -223,7 +223,7 @@ func (t *Terminal) reconcileCells() {
current := t.display.Cells[c.Point] current := t.display.Cells[c.Point]
if current.Value == c.Value { if current.Value == c.Value && current.Attrs == c.Attrs {
continue continue
} }

View File

@ -10,6 +10,4 @@ type Widget interface {
SetSize(image.Point) SetSize(image.Point)
Origin() image.Point Origin() image.Point
SetOrigin(image.Point) SetOrigin(image.Point)
Attributes() Attributes
SetAttributes(Attributes)
} }

View File

@ -10,9 +10,6 @@ type WindowWidget struct {
blockLock sync.Mutex blockLock sync.Mutex
block Block block Block
attributesLock sync.Mutex
attributes Attributes
borderLock sync.Mutex borderLock sync.Mutex
border Border border Border
@ -31,20 +28,6 @@ func (w *WindowWidget) AddWidget(widget Widget) {
w.widgets = append(w.widgets, widget) w.widgets = append(w.widgets, widget)
} }
func (w WindowWidget) Attributes() Attributes {
w.attributesLock.Lock()
defer w.attributesLock.Unlock()
return w.attributes
}
func (w *WindowWidget) SetAttributes(a Attributes) {
w.attributesLock.Lock()
defer w.attributesLock.Unlock()
w.attributes = a
}
func (w WindowWidget) Size() image.Point { func (w WindowWidget) Size() image.Point {
return w.block.Rect.Size() return w.block.Rect.Size()
} }

View File

@ -1,8 +1,7 @@
package main package main
import ( import (
"fmt" "errors"
"os"
) )
type Queue struct { type Queue struct {
@ -24,10 +23,9 @@ func (q *Queue) Length() int {
return q.length return q.length
} }
func (q *Queue) Enqueue(v interface{}) { func (q *Queue) Enqueue(v interface{}) error {
if q.length == 256 { if q.length == 256 {
fmt.Println("Queue Full") return errors.New("Queue Full")
os.Exit(1)
} }
q.end++ q.end++
@ -38,6 +36,8 @@ func (q *Queue) Enqueue(v interface{}) {
q.length++ q.length++
q.data[q.end] = v q.data[q.end] = v
return nil
} }
func (q *Queue) Dequeue() interface{} { func (q *Queue) Dequeue() interface{} {
@ -65,6 +65,10 @@ func (q *Queue) Dequeue() interface{} {
func (q *Queue) Element(i int) interface{} { func (q *Queue) Element(i int) interface{} {
var v interface{} var v interface{}
if i > q.length {
return nil
}
if q.begin == 0 { if q.begin == 0 {
v = q.data[i] v = q.data[i]
} else { } else {

View File

@ -13,6 +13,7 @@ import (
"log" "log"
"net/http" "net/http"
"time" "time"
"unicode/utf8"
) )
const ( const (
@ -71,6 +72,7 @@ var window *exhibit.WindowWidget
var topAsks *exhibit.ListWidget var topAsks *exhibit.ListWidget
var topBids *exhibit.ListWidget var topBids *exhibit.ListWidget
var midPrice *exhibit.ListWidget var midPrice *exhibit.ListWidget
var history *exhibit.ListWidget
func main() { func main() {
terminal = exhibit.Init() terminal = exhibit.Init()
@ -78,22 +80,20 @@ func main() {
terminal.HideCursor() terminal.HideCursor()
window = &exhibit.WindowWidget{} window = &exhibit.WindowWidget{}
window.SetBorder(exhibit.Border{Visible: true}) window.SetBorder(exhibit.Border{Visible: true, Attributes: exhibit.Attributes{ForegroundColor: exhibit.FGYellow}})
topAsks = &exhibit.ListWidget{}
// topAsks.SetSize(image.Point{100, 100})
topAsks.SetRightAlign(true)
topAsks.SetAttributes(exhibit.Attributes{ForegroundColor: exhibit.FGCyan})
topAsks = &exhibit.ListWidget{}
topBids = &exhibit.ListWidget{} topBids = &exhibit.ListWidget{}
// topBids.SetRightAlign(true)
// topBids.SetAttributes(exhibit.Attributes{ForegroundColor: exhibit.FGGreen})
midPrice = &exhibit.ListWidget{} midPrice = &exhibit.ListWidget{}
// midPrice.SetRightAlign(true) midPrice.SetSize(image.Pt(24, 1))
history = &exhibit.ListWidget{}
window.AddWidget(topAsks) window.AddWidget(topAsks)
// window.AddWidget(midPrice) window.AddWidget(midPrice)
// window.AddWidget(topBids) window.AddWidget(topBids)
window.AddWidget(history)
scene := exhibit.Scene{terminal, window} scene := exhibit.Scene{terminal, window}
@ -169,10 +169,32 @@ func main() {
sz := terminal.Size() sz := terminal.Size()
if history.Size() != image.Pt(33, sz.Y) {
history.SetSize(image.Pt(33, sz.Y))
}
hOr := image.Pt(sz.X-35, 0)
if history.Origin() != hOr {
history.SetOrigin(hOr)
}
num := numOfOrderPerSide(sz.Y) num := numOfOrderPerSide(sz.Y)
aP := image.Point{14, num} size := image.Point{23, num}
if topAsks.Size() != aP { if topAsks.Size() != size {
topAsks.SetSize(aP) topAsks.SetSize(size)
}
bOrigin := image.Pt(0, size.Y+3)
if topBids.Origin() != bOrigin {
topBids.SetOrigin(bOrigin)
}
if topBids.Size() != size {
topBids.SetSize(size)
}
mOr := image.Pt(0, num+1)
if midPrice.Origin() != mOr {
midPrice.SetOrigin(mOr)
} }
aIt := asks.Iterator() aIt := asks.Iterator()
@ -185,8 +207,8 @@ func main() {
entries := aIt.Value().(Entries) entries := aIt.Value().(Entries)
price, size := flatten(entries) price, size := flatten(entries)
asks[i] = ListEntry{Value: size.StringFixed(8), asks[i] = ListEntry{Value: fmtObEntry(price, size),
Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGMagenta}} Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGRed}}
if i == 0 { if i == 0 {
low = price low = price
@ -206,7 +228,8 @@ func main() {
entries := bIt.Value().(Entries) entries := bIt.Value().(Entries)
price, size := flatten(entries) price, size := flatten(entries)
topBids.AddEntry(ListEntry{Value: size.StringFixed(8)}) topBids.AddEntry(ListEntry{Value: fmtObEntry(price, size),
Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGGreen}})
if i == 0 { if i == 0 {
high = price high = price
@ -215,10 +238,7 @@ func main() {
topBids.Commit() topBids.Commit()
diff := low.Sub(high) midPrice.AddEntry(ListEntry{Value: fmtMid(high, low)})
midPrice.AddEntry(ListEntry{Value: high.Add(diff.Div(decimal.
New(2, 0))).StringFixed(3)})
midPrice.Commit() midPrice.Commit()
} }
} }
@ -283,6 +303,47 @@ func match(msg Message) {
} }
trades.Enqueue(msg) trades.Enqueue(msg)
max := history.Size().Y
length := trades.Length()
var num int
if length > max {
num = max
} else {
num = length
}
for i := 0; i < num; i++ {
var index int
adj := trades.Length() - i - 1
if adj < 0 {
break
} else {
index = adj
}
e := trades.Element(index)
if e != nil {
msg := e.(Message)
var attrs exhibit.Attributes
switch msg.Side {
case "buy":
attrs.ForegroundColor = exhibit.FGRed
case "sell":
attrs.ForegroundColor = exhibit.FGGreen
}
le := ListEntry{fmtHistoryEntry(msg), attrs}
history.AddEntry(le)
}
}
history.Commit()
} }
func change(msg Message) { func change(msg Message) {
@ -401,7 +462,7 @@ func treeEntries(tree *redblacktree.Tree, key decimal.Decimal) (Entries, bool) {
} }
func numOfOrderPerSide(y int) int { func numOfOrderPerSide(y int) int {
total := y - 3 - 4 total := y - 3 - 2
return (total / 2) return (total / 2)
} }
@ -455,3 +516,58 @@ func ReverseDecimalComparator(a, b interface{}) int {
return 0 return 0
} }
} }
func recalcSizes() {
}
func padString(value string, length int) string {
c := utf8.RuneCountInString(value)
if c >= length {
return value
}
pad := length - c
var s string
for i := 0; i < pad; i++ {
s = s + " "
}
return s + value
}
func fmtObEntry(price, size decimal.Decimal) string {
s := padString(price.StringFixed(2), 8)
s = s + " "
s = s + padString(size.StringFixed(8), 14)
return s
}
func fmtHistoryEntry(msg Message) string {
var arrow string
switch msg.Side {
case "buy":
arrow = "↓"
case "sell":
arrow = "↑"
}
s := padString(msg.Size.StringFixed(8), 14)
s = s + " "
s = s + padString(msg.Price.StringFixed(2), 8)
s = s + arrow
s = s + " "
s = s + msg.Time.Local().Format(timeFormat)
return s
}
func fmtMid(high, low decimal.Decimal) string {
diff := low.Sub(high)
mid := high.Add(diff.Div(decimal.New(2, 0))).StringFixed(3)
return padString(mid, 9) + padString(diff.StringFixed(2), 14)
}