This commit is contained in:
gizak 2015-12-06 12:51:37 -05:00
parent a302a528a8
commit ab87ad72d1

View File

@ -5,6 +5,8 @@
package termui package termui
import ( import (
"image"
"sync"
"time" "time"
tm "github.com/nsf/termbox-go" tm "github.com/nsf/termbox-go"
@ -26,11 +28,7 @@ func Init() error {
go hookTermboxEvt() go hookTermboxEvt()
renderJobs = make(chan []Bufferer) renderJobs = make(chan []Bufferer)
go func() { //renderLock = new(sync.RWMutex)
for bs := range renderJobs {
render(bs...)
}
}()
Body = NewGrid() Body = NewGrid()
Body.X = 0 Body.X = 0
@ -51,6 +49,13 @@ func Init() error {
DefaultWgtMgr = NewWgtMgr() DefaultWgtMgr = NewWgtMgr()
DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook()) DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook())
go func() {
for bs := range renderJobs {
render(bs...)
}
}()
return nil return nil
} }
@ -60,16 +65,24 @@ func Close() {
tm.Close() tm.Close()
} }
var renderLock sync.Mutex
func termSync() {
renderLock.Lock()
tm.Sync()
renderLock.Unlock()
}
// TermWidth returns the current terminal's width. // TermWidth returns the current terminal's width.
func TermWidth() int { func TermWidth() int {
tm.Sync() termSync()
w, _ := tm.Size() w, _ := tm.Size()
return w return w
} }
// TermHeight returns the current terminal's height. // TermHeight returns the current terminal's height.
func TermHeight() int { func TermHeight() int {
tm.Sync() termSync()
_, h := tm.Size() _, h := tm.Size()
return h return h
} }
@ -77,8 +90,7 @@ func TermHeight() int {
// Render renders all Bufferer in the given order from left to right, // Render renders all Bufferer in the given order from left to right,
// right could overlap on left ones. // right could overlap on left ones.
func render(bs ...Bufferer) { func render(bs ...Bufferer) {
// set tm bg
tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg")))
for _, b := range bs { for _, b := range bs {
buf := b.Buffer() buf := b.Buffer()
// set cels in buf // set cels in buf
@ -88,12 +100,34 @@ func render(bs ...Bufferer) {
} }
} }
} }
renderLock.Lock()
// render // render
tm.Flush() 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 var renderJobs chan []Bufferer
func Render(bs ...Bufferer) { func Render(bs ...Bufferer) {
go func() { renderJobs <- bs }() //go func() { renderJobs <- bs }()
renderJobs <- bs
} }