gotop/termui/render.go

41 lines
833 B
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package termui
import (
"sync"
tb "github.com/nsf/termbox-go"
)
2018-02-23 16:42:39 +08:00
// Bufferer should be implemented by all renderable components.
2018-02-19 15:25:02 +08:00
type Bufferer interface {
Buffer() *Buffer
GetXOffset() int
GetYOffset() int
}
2018-02-23 16:42:39 +08:00
// Render renders all Bufferers in the given order to termbox, then asks termbox to print the screen.
2018-02-19 15:25:02 +08:00
func Render(bs ...Bufferer) {
var wg sync.WaitGroup
for _, b := range bs {
wg.Add(1)
go func(b Bufferer) {
defer wg.Done()
buf := b.Buffer()
// set cells in buf
for p, c := range buf.CellMap {
if p.In(buf.Area) {
2018-02-21 16:46:11 +08:00
tb.SetCell(p.X+b.GetXOffset(), p.Y+b.GetYOffset(), c.Ch, tb.Attribute(c.Fg)+1, tb.Attribute(c.Bg)+1)
2018-02-19 15:25:02 +08:00
}
}
}(b)
}
wg.Wait()
tb.Flush()
}
2018-02-23 16:42:39 +08:00
// Clear clears the screen with the default Bg color.
2018-02-19 15:25:02 +08:00
func Clear() {
tb.Clear(tb.ColorDefault+1, tb.Attribute(Theme.Bg)+1)
2018-02-19 15:25:02 +08:00
}