Frequently calling termbox.Sync() will upset terminal.
This commit is contained in:
gizak 2015-12-09 14:04:28 -05:00
parent 47e145b445
commit 0cb4aedd6f
2 changed files with 11 additions and 6 deletions

5
pos.go
View File

@ -66,6 +66,9 @@ func MoveArea(a image.Rectangle, dx, dy int) image.Rectangle {
return a return a
} }
var termWidth int
var termHeight int
func TermRect() image.Rectangle { func TermRect() image.Rectangle {
return image.Rect(0, 0, TermWidth(), TermHeight()) return image.Rect(0, 0, termWidth, termHeight)
} }

View File

@ -70,21 +70,20 @@ var renderLock sync.Mutex
func termSync() { func termSync() {
renderLock.Lock() renderLock.Lock()
tm.Sync() tm.Sync()
termWidth, termHeight = tm.Size()
renderLock.Unlock() renderLock.Unlock()
} }
// TermWidth returns the current terminal's width. // TermWidth returns the current terminal's width.
func TermWidth() int { func TermWidth() int {
termSync() termSync()
w, _ := tm.Size() return termWidth
return w
} }
// TermHeight returns the current terminal's height. // TermHeight returns the current terminal's height.
func TermHeight() int { func TermHeight() int {
termSync() termSync()
_, h := tm.Size() return termHeight
return h
} }
// Render renders all Bufferer in the given order from left to right, // Render renders all Bufferer in the given order from left to right,
@ -92,19 +91,22 @@ func TermHeight() int {
func render(bs ...Bufferer) { func render(bs ...Bufferer) {
for _, b := range bs { for _, b := range bs {
buf := b.Buffer() buf := b.Buffer()
// set cels in buf // set cels in buf
for p, c := range buf.CellMap { for p, c := range buf.CellMap {
if p.In(buf.Area) { if p.In(buf.Area) {
tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg)) tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
} }
} }
} }
renderLock.Lock() renderLock.Lock()
// render // render
tm.Flush() tm.Flush()
renderLock.Unlock() renderLock.Unlock()
} }