2015-03-20 14:21:50 -06: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 07:07:31 -07:00
|
|
|
package termui
|
|
|
|
|
|
|
|
import tm "github.com/nsf/termbox-go"
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Bufferer should be implemented by all renderable components.
|
2015-03-03 11:28:09 -07:00
|
|
|
type Bufferer interface {
|
2015-02-03 07:07:31 -07:00
|
|
|
Buffer() []Point
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Init initializes termui library. This function should be called before any others.
|
|
|
|
// After initialization, the library must be finalized by 'Close' function.
|
2015-02-03 07:07:31 -07:00
|
|
|
func Init() error {
|
2015-03-20 06:24:48 -06:00
|
|
|
Body = NewGrid()
|
|
|
|
Body.X = 0
|
|
|
|
Body.Y = 0
|
|
|
|
Body.BgColor = theme.BodyBg
|
2015-04-08 10:06:47 -06:00
|
|
|
defer func() {
|
2015-03-20 06:24:48 -06:00
|
|
|
w, _ := tm.Size()
|
|
|
|
Body.Width = w
|
2015-04-08 10:06:47 -06:00
|
|
|
evtListen()
|
|
|
|
}()
|
2015-02-03 07:07:31 -07:00
|
|
|
return tm.Init()
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Close finalizes termui library,
|
|
|
|
// should be called after successful initialization when termui's functionality isn't required anymore.
|
2015-02-03 18:56:49 -07:00
|
|
|
func Close() {
|
2015-02-03 07:07:31 -07:00
|
|
|
tm.Close()
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// TermWidth returns the current terminal's width.
|
2015-03-20 06:24:48 -06:00
|
|
|
func TermWidth() int {
|
2015-05-05 08:55:35 -06:00
|
|
|
tm.Sync()
|
2015-03-20 06:24:48 -06:00
|
|
|
w, _ := tm.Size()
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// TermHeight returns the current terminal's height.
|
2015-03-20 06:24:48 -06:00
|
|
|
func TermHeight() int {
|
2015-05-05 08:55:35 -06:00
|
|
|
tm.Sync()
|
2015-03-20 06:24:48 -06:00
|
|
|
_, h := tm.Size()
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Render renders all Bufferer in the given order from left to right,
|
|
|
|
// right could overlap on left ones.
|
2015-03-03 11:28:09 -07:00
|
|
|
func Render(rs ...Bufferer) {
|
2015-03-11 14:15:59 -06:00
|
|
|
tm.Clear(tm.ColorDefault, toTmAttr(theme.BodyBg))
|
2015-02-03 18:56:49 -07:00
|
|
|
for _, r := range rs {
|
|
|
|
buf := r.Buffer()
|
|
|
|
for _, v := range buf {
|
2015-03-03 11:28:09 -07:00
|
|
|
tm.SetCell(v.X, v.Y, v.Ch, toTmAttr(v.Fg), toTmAttr(v.Bg))
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
2015-02-03 07:07:31 -07:00
|
|
|
}
|
|
|
|
tm.Flush()
|
|
|
|
}
|