Add all widget back to the ui
This commit is contained in:
parent
166245fccd
commit
df3c9456c1
@ -14,9 +14,6 @@ type ListWidget struct {
|
||||
blockLock sync.Mutex
|
||||
block Block
|
||||
|
||||
attributesLock sync.Mutex
|
||||
attributes Attributes
|
||||
|
||||
rightAlignLock sync.Mutex
|
||||
rightAlign bool
|
||||
|
||||
@ -29,21 +26,6 @@ type ListWidget struct {
|
||||
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 {
|
||||
l.blockLock.Lock()
|
||||
defer l.blockLock.Unlock()
|
||||
|
@ -223,7 +223,7 @@ func (t *Terminal) reconcileCells() {
|
||||
|
||||
current := t.display.Cells[c.Point]
|
||||
|
||||
if current.Value == c.Value {
|
||||
if current.Value == c.Value && current.Attrs == c.Attrs {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,4 @@ type Widget interface {
|
||||
SetSize(image.Point)
|
||||
Origin() image.Point
|
||||
SetOrigin(image.Point)
|
||||
Attributes() Attributes
|
||||
SetAttributes(Attributes)
|
||||
}
|
||||
|
@ -10,9 +10,6 @@ type WindowWidget struct {
|
||||
blockLock sync.Mutex
|
||||
block Block
|
||||
|
||||
attributesLock sync.Mutex
|
||||
attributes Attributes
|
||||
|
||||
borderLock sync.Mutex
|
||||
border Border
|
||||
|
||||
@ -31,20 +28,6 @@ func (w *WindowWidget) AddWidget(widget 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 {
|
||||
return w.block.Rect.Size()
|
||||
}
|
||||
|
14
queue.go
14
queue.go
@ -1,8 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Queue struct {
|
||||
@ -24,10 +23,9 @@ func (q *Queue) Length() int {
|
||||
return q.length
|
||||
}
|
||||
|
||||
func (q *Queue) Enqueue(v interface{}) {
|
||||
func (q *Queue) Enqueue(v interface{}) error {
|
||||
if q.length == 256 {
|
||||
fmt.Println("Queue Full")
|
||||
os.Exit(1)
|
||||
return errors.New("Queue Full")
|
||||
}
|
||||
|
||||
q.end++
|
||||
@ -38,6 +36,8 @@ func (q *Queue) Enqueue(v interface{}) {
|
||||
|
||||
q.length++
|
||||
q.data[q.end] = v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Queue) Dequeue() interface{} {
|
||||
@ -65,6 +65,10 @@ func (q *Queue) Dequeue() interface{} {
|
||||
func (q *Queue) Element(i int) interface{} {
|
||||
var v interface{}
|
||||
|
||||
if i > q.length {
|
||||
return nil
|
||||
}
|
||||
|
||||
if q.begin == 0 {
|
||||
v = q.data[i]
|
||||
} else {
|
||||
|
158
spectator.go
158
spectator.go
@ -13,6 +13,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -71,6 +72,7 @@ var window *exhibit.WindowWidget
|
||||
var topAsks *exhibit.ListWidget
|
||||
var topBids *exhibit.ListWidget
|
||||
var midPrice *exhibit.ListWidget
|
||||
var history *exhibit.ListWidget
|
||||
|
||||
func main() {
|
||||
terminal = exhibit.Init()
|
||||
@ -78,22 +80,20 @@ func main() {
|
||||
terminal.HideCursor()
|
||||
|
||||
window = &exhibit.WindowWidget{}
|
||||
window.SetBorder(exhibit.Border{Visible: true})
|
||||
topAsks = &exhibit.ListWidget{}
|
||||
// topAsks.SetSize(image.Point{100, 100})
|
||||
topAsks.SetRightAlign(true)
|
||||
topAsks.SetAttributes(exhibit.Attributes{ForegroundColor: exhibit.FGCyan})
|
||||
window.SetBorder(exhibit.Border{Visible: true, Attributes: exhibit.Attributes{ForegroundColor: exhibit.FGYellow}})
|
||||
|
||||
topAsks = &exhibit.ListWidget{}
|
||||
topBids = &exhibit.ListWidget{}
|
||||
// topBids.SetRightAlign(true)
|
||||
// topBids.SetAttributes(exhibit.Attributes{ForegroundColor: exhibit.FGGreen})
|
||||
|
||||
midPrice = &exhibit.ListWidget{}
|
||||
// midPrice.SetRightAlign(true)
|
||||
midPrice.SetSize(image.Pt(24, 1))
|
||||
|
||||
history = &exhibit.ListWidget{}
|
||||
|
||||
window.AddWidget(topAsks)
|
||||
// window.AddWidget(midPrice)
|
||||
// window.AddWidget(topBids)
|
||||
window.AddWidget(midPrice)
|
||||
window.AddWidget(topBids)
|
||||
window.AddWidget(history)
|
||||
|
||||
scene := exhibit.Scene{terminal, window}
|
||||
|
||||
@ -169,10 +169,32 @@ func main() {
|
||||
|
||||
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)
|
||||
aP := image.Point{14, num}
|
||||
if topAsks.Size() != aP {
|
||||
topAsks.SetSize(aP)
|
||||
size := image.Point{23, num}
|
||||
if topAsks.Size() != size {
|
||||
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()
|
||||
@ -185,8 +207,8 @@ func main() {
|
||||
entries := aIt.Value().(Entries)
|
||||
price, size := flatten(entries)
|
||||
|
||||
asks[i] = ListEntry{Value: size.StringFixed(8),
|
||||
Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGMagenta}}
|
||||
asks[i] = ListEntry{Value: fmtObEntry(price, size),
|
||||
Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGRed}}
|
||||
|
||||
if i == 0 {
|
||||
low = price
|
||||
@ -206,7 +228,8 @@ func main() {
|
||||
entries := bIt.Value().(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 {
|
||||
high = price
|
||||
@ -215,10 +238,7 @@ func main() {
|
||||
|
||||
topBids.Commit()
|
||||
|
||||
diff := low.Sub(high)
|
||||
|
||||
midPrice.AddEntry(ListEntry{Value: high.Add(diff.Div(decimal.
|
||||
New(2, 0))).StringFixed(3)})
|
||||
midPrice.AddEntry(ListEntry{Value: fmtMid(high, low)})
|
||||
midPrice.Commit()
|
||||
}
|
||||
}
|
||||
@ -283,6 +303,47 @@ func match(msg Message) {
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -401,7 +462,7 @@ func treeEntries(tree *redblacktree.Tree, key decimal.Decimal) (Entries, bool) {
|
||||
}
|
||||
|
||||
func numOfOrderPerSide(y int) int {
|
||||
total := y - 3 - 4
|
||||
total := y - 3 - 2
|
||||
|
||||
return (total / 2)
|
||||
}
|
||||
@ -455,3 +516,58 @@ func ReverseDecimalComparator(a, b interface{}) int {
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user