From ab87ad72d1c72b4b263f3e747a7fca7e5cc9cd74 Mon Sep 17 00:00:00 2001 From: gizak Date: Sun, 6 Dec 2015 12:51:37 -0500 Subject: [PATCH] Fix https://github.com/gizak/termui/issues/63 --- render.go | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/render.go b/render.go index 0cf9b88..2de0482 100644 --- a/render.go +++ b/render.go @@ -5,6 +5,8 @@ package termui import ( + "image" + "sync" "time" tm "github.com/nsf/termbox-go" @@ -26,11 +28,7 @@ func Init() error { go hookTermboxEvt() renderJobs = make(chan []Bufferer) - go func() { - for bs := range renderJobs { - render(bs...) - } - }() + //renderLock = new(sync.RWMutex) Body = NewGrid() Body.X = 0 @@ -51,6 +49,13 @@ func Init() error { DefaultWgtMgr = NewWgtMgr() DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook()) + + go func() { + for bs := range renderJobs { + render(bs...) + } + }() + return nil } @@ -60,16 +65,24 @@ func Close() { tm.Close() } +var renderLock sync.Mutex + +func termSync() { + renderLock.Lock() + tm.Sync() + renderLock.Unlock() +} + // TermWidth returns the current terminal's width. func TermWidth() int { - tm.Sync() + termSync() w, _ := tm.Size() return w } // TermHeight returns the current terminal's height. func TermHeight() int { - tm.Sync() + termSync() _, h := tm.Size() return h } @@ -77,8 +90,7 @@ func TermHeight() int { // Render renders all Bufferer in the given order from left to right, // right could overlap on left ones. func render(bs ...Bufferer) { - // set tm bg - tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg"))) + for _, b := range bs { buf := b.Buffer() // set cels in buf @@ -88,12 +100,34 @@ func render(bs ...Bufferer) { } } } + + renderLock.Lock() // render tm.Flush() + + renderLock.Unlock() +} + +func Clear() { + tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg"))) +} + +func clearArea(r image.Rectangle, bg Attribute) { + for i := r.Min.X; i < r.Max.X; i++ { + for j := r.Min.Y; j < r.Max.Y; j++ { + tm.SetCell(i, j, ' ', tm.ColorDefault, toTmAttr(bg)) + } + } +} + +func ClearArea(r image.Rectangle, bg Attribute) { + clearArea(r, bg) + tm.Flush() } var renderJobs chan []Bufferer func Render(bs ...Bufferer) { - go func() { renderJobs <- bs }() + //go func() { renderJobs <- bs }() + renderJobs <- bs }