diff --git a/exhibit/terminal.go b/exhibit/terminal.go index bc65c34..889aba6 100644 --- a/exhibit/terminal.go +++ b/exhibit/terminal.go @@ -118,6 +118,10 @@ func (t *Terminal) Shutdown() { } func (t *Terminal) Clear() { + t.displayLock.Lock() + defer t.displayLock.Unlock() + t.display.Cells = make(map[image.Point]Cell) + t.writeBuffer([]byte(clear)) } @@ -140,6 +144,9 @@ func (t *Terminal) CursorVisible() bool { } func (t *Terminal) Size() image.Point { + t.displayLock.Lock() + defer t.displayLock.Unlock() + return t.display.Size() } @@ -153,8 +160,6 @@ func (t *Terminal) setSize(s image.Point) { t.display = NewBlock(0, 0, s.X, s.Y) - t.Clear() - select { case t.sizeChange <- s: default: diff --git a/exhibit/window_widget.go b/exhibit/window_widget.go index c998307..b9d4d4b 100644 --- a/exhibit/window_widget.go +++ b/exhibit/window_widget.go @@ -6,7 +6,6 @@ import ( ) type WindowWidget struct { - Style Style blockLock sync.Mutex block Block @@ -29,10 +28,16 @@ func (w *WindowWidget) AddWidget(widget Widget) { } func (w WindowWidget) Size() image.Point { - return w.block.Rect.Size() + w.blockLock.Lock() + defer w.blockLock.Unlock() + + return w.block.Size() } func (w *WindowWidget) SetSize(p image.Point) { + w.blockLock.Lock() + defer w.blockLock.Unlock() + w.block.SetSize(p) } @@ -65,15 +70,15 @@ func (w *WindowWidget) SetBorder(b Border) { } func (w *WindowWidget) Render(origin image.Point) Block { - if w.block.Rect.Size().X == 0 || w.block.Rect.Size().Y == 0 { - return NewBlock(0, 0, 0, 0) - } + w.blockLock.Lock() + defer w.blockLock.Unlock() w.widgetLock.Lock() defer w.widgetLock.Unlock() - w.blockLock.Lock() - defer w.blockLock.Unlock() + if w.block.Rect.Size().X == 0 || w.block.Rect.Size().Y == 0 { + return NewBlock(0, 0, 0, 0) + } var borderAdj image.Point border := w.Border() diff --git a/spectator.go b/spectator.go index f7c8c55..dff5da3 100644 --- a/spectator.go +++ b/spectator.go @@ -30,6 +30,9 @@ var history *exhibit.ListWidget var numLock sync.Mutex var num int +var sizeLock sync.Mutex +var sizeChanged bool + var low, high decimal.Decimal func main() { @@ -57,7 +60,7 @@ func main() { scene := exhibit.Scene{terminal, window} - watchSize(terminal.SizeChange) + watchSize(terminal) ob, err = NewOrderBook(coin) if err != nil { @@ -117,9 +120,15 @@ func flatten(entries Entries) (decimal.Decimal, decimal.Decimal) { func renderLoop(scene *exhibit.Scene, interval time.Duration) { timer := time.NewTicker(interval) + changed := time.NewTicker(2 * time.Second) for { select { + case <-changed.C: + if didsizeChanged() { + terminal.Clear() + setSizeChanged(false) + } case <-timer.C: scene.Render() } @@ -127,38 +136,41 @@ func renderLoop(scene *exhibit.Scene, interval time.Duration) { } func recalcSizes(sz image.Point) { - numLock.Lock() - defer numLock.Unlock() + numLock.Lock() + defer numLock.Unlock() - num = numOfOrderPerSide(sz.Y) + sizeLock.Lock() + defer sizeLock.Unlock() - if history.Size() != image.Pt(33, sz.Y) { - history.SetSize(image.Pt(33, sz.Y)) - } + num = numOfOrderPerSide(sz.Y) - hOr := image.Pt(sz.X-35, 0) - if history.Origin() != hOr { - history.SetOrigin(hOr) - } + if history.Size() != image.Pt(33, sz.Y) { + history.SetSize(image.Pt(33, sz.Y)) + } - num := numOfOrderPerSide(sz.Y) - size := image.Point{23, num} - if topAsks.Size() != size { - topAsks.SetSize(size) - } + hOr := image.Pt(sz.X-35, 0) + if history.Origin() != hOr { + history.SetOrigin(hOr) + } - bOrigin := image.Pt(0, size.Y+3) - if topBids.Origin() != bOrigin { - topBids.SetOrigin(bOrigin) - } - if topBids.Size() != size { - topBids.SetSize(size) - } + num := numOfOrderPerSide(sz.Y) + size := image.Point{23, num} + if topAsks.Size() != size { + topAsks.SetSize(size) + } - mOr := image.Pt(0, num+1) - if midPrice.Origin() != mOr { - midPrice.SetOrigin(mOr) - } + 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) + } } func padString(value string, length int) string { @@ -176,7 +188,6 @@ func padString(value string, length int) string { } return s + value - } func fmtObEntry(price, size decimal.Decimal) string { @@ -213,10 +224,11 @@ func fmtMid(high, low decimal.Decimal) string { return padString(mid, 9) + padString(diff.StringFixed(2), 14) } -func watchSize(c <-chan image.Point) { +func watchSize(t *exhibit.Terminal) { go func() { - for s := range c { + for s := range t.SizeChange { recalcSizes(s) + setSizeChanged(true) } }() } @@ -317,3 +329,17 @@ func addTrade(msg Message) { history.Commit() } + +func didsizeChanged() bool { + sizeLock.Lock() + defer sizeLock.Unlock() + + return sizeChanged +} + +func setSizeChanged(c bool) { + sizeLock.Lock() + defer sizeLock.Unlock() + + sizeChanged = c +}