From f23ed68e303d86b6c897e2c43c99961624d24ef6 Mon Sep 17 00:00:00 2001 From: gizak Date: Tue, 5 May 2015 10:55:35 -0400 Subject: [PATCH] Fix https://github.com/gizak/termui/issues/43 Add termbox.Sync() call before getting terminal's width/height Adjust examples see https://github.com/gizak/termui/pull/39 --- example/dashboard.go | 6 +++--- example/grid.go | 16 ++++++++++------ render.go | 2 ++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/example/dashboard.go b/example/dashboard.go index d855ef2..c14bb44 100644 --- a/example/dashboard.go +++ b/example/dashboard.go @@ -47,7 +47,7 @@ func main() { spark := ui.Sparkline{} spark.Height = 1 spark.Title = "srv 0:" - spdata := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6} + spdata := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6} spark.Data = spdata spark.LineColor = ui.ColorCyan spark.TitleColor = ui.ColorWhite @@ -119,8 +119,8 @@ func main() { draw := func(t int) { g.Percent = t % 101 list.Items = strs[t%9:] - sp.Lines[0].Data = spdata[t%10:] - sp.Lines[1].Data = spdata[t/2%10:] + sp.Lines[0].Data = spdata[:30+t%50] + sp.Lines[1].Data = spdata[:35+t%50] lc.Data = sinps[t/2:] lc1.Data = sinps[2*t:] bc.Data = bcdata[t/2%10:] diff --git a/example/grid.go b/example/grid.go index 9dbbc11..4912141 100644 --- a/example/grid.go +++ b/example/grid.go @@ -8,7 +8,6 @@ package main import ui "github.com/gizak/termui" import "math" - import "time" func main() { @@ -39,7 +38,7 @@ func main() { spark := ui.Sparkline{} spark.Height = 8 spdata := sinpsint - spark.Data = spdata + spark.Data = spdata[:100] spark.LineColor = ui.ColorCyan spark.TitleColor = ui.ColorWhite @@ -93,24 +92,28 @@ func main() { ui.Body.Align() done := make(chan bool) + redraw := make(chan bool) - draw := func() { + update := func() { for i := 0; i < 103; i++ { for _, g := range gs { g.Percent = (g.Percent + 3) % 100 } - sp.Lines[0].Data = spdata[i:] + sp.Lines[0].Data = spdata[:100+i] lc.Data = sinps[2*i:] time.Sleep(time.Second / 2) + redraw <- true } done <- true } evt := ui.EventCh() - go draw() + ui.Render(ui.Body) + go update() + for { select { case e := <-evt: @@ -120,10 +123,11 @@ func main() { if e.Type == ui.EventResize { ui.Body.Width = ui.TermWidth() ui.Body.Align() + go func() { redraw <- true }() } case <-done: return - default: + case <-redraw: ui.Render(ui.Body) } } diff --git a/render.go b/render.go index 735fe5b..d697d0a 100644 --- a/render.go +++ b/render.go @@ -34,12 +34,14 @@ func Close() { // TermWidth returns the current terminal's width. func TermWidth() int { + tm.Sync() w, _ := tm.Size() return w } // TermHeight returns the current terminal's height. func TermHeight() int { + tm.Sync() _, h := tm.Size() return h }