termui/render.go

51 lines
934 B
Go
Raw Normal View History

2015-03-20 20:21:50 +00:00
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
2015-02-03 14:07:31 +00:00
package termui
import tm "github.com/nsf/termbox-go"
// all renderable components should implement this
2015-03-03 18:28:09 +00:00
type Bufferer interface {
2015-02-03 14:07:31 +00:00
Buffer() []Point
}
func Init() error {
2015-03-20 12:24:48 +00:00
Body = NewGrid()
Body.X = 0
Body.Y = 0
Body.BgColor = theme.BodyBg
defer (func() {
w, _ := tm.Size()
Body.Width = w
})()
2015-02-03 14:07:31 +00:00
return tm.Init()
}
2015-02-04 01:56:49 +00:00
func Close() {
2015-02-03 14:07:31 +00:00
tm.Close()
}
2015-03-20 12:24:48 +00:00
func TermWidth() int {
w, _ := tm.Size()
return w
}
func TermHeight() int {
_, h := tm.Size()
return h
}
// render all from left to right, right could overlap on left ones
2015-03-03 18:28:09 +00:00
func Render(rs ...Bufferer) {
tm.Clear(tm.ColorDefault, toTmAttr(theme.BodyBg))
2015-02-04 01:56:49 +00:00
for _, r := range rs {
buf := r.Buffer()
for _, v := range buf {
2015-03-03 18:28:09 +00:00
tm.SetCell(v.X, v.Y, v.Ch, toTmAttr(v.Fg), toTmAttr(v.Bg))
2015-02-04 01:56:49 +00:00
}
2015-02-03 14:07:31 +00:00
}
tm.Flush()
}