termui/render.go

144 lines
2.9 KiB
Go
Raw Normal View History

2017-01-13 23:07:43 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2015-03-20 14:21:50 -06:00
// 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
2015-09-18 09:41:44 -06:00
import (
"image"
2018-09-06 14:57:56 -06:00
"runtime/debug"
"sync"
2015-09-18 09:41:44 -06:00
"time"
tm "github.com/nsf/termbox-go"
)
2015-02-03 07:07:31 -07:00
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 {
Buffer() Buffer
2015-02-03 07:07:31 -07:00
}
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-05-09 17:29:22 -06:00
if err := tm.Init(); err != nil {
return err
}
2017-05-27 04:11:46 -06:00
tm.SetInputMode(tm.InputEsc | tm.InputMouse)
2017-05-16 23:18:44 -06:00
DefaultEvtStream = NewEvtStream()
2015-09-18 09:41:44 -06:00
sysEvtChs = make([]chan Event, 0)
go hookTermboxEvt()
2015-10-10 09:20:44 -06:00
2015-09-18 09:41:44 -06:00
renderJobs = make(chan []Bufferer)
//renderLock = new(sync.RWMutex)
2015-09-18 09:41:44 -06:00
2015-10-10 09:20:44 -06:00
Body = NewGrid()
Body.X = 0
Body.Y = 0
Body.BgColor = ThemeAttr("bg")
Body.Width = TermWidth()
2015-09-18 09:41:44 -06:00
DefaultEvtStream.Init()
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
2015-10-13 10:45:03 -06:00
DefaultEvtStream.Merge("custom", usrEvtCh)
DefaultEvtStream.Handle("/", DefaultHandler)
2015-10-07 12:25:59 -06:00
DefaultEvtStream.Handle("/sys/wnd/resize", func(e Event) {
w := e.Data.(EvtWnd)
Body.Width = w.Width
})
2015-09-18 09:41:44 -06:00
2015-10-07 12:25:59 -06:00
DefaultWgtMgr = NewWgtMgr()
DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook())
go func() {
for bs := range renderJobs {
render(bs...)
}
}()
2015-05-09 17:29:22 -06:00
return nil
2015-02-03 07:07:31 -07:00
}
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()
}
var renderLock sync.Mutex
func termSync() {
renderLock.Lock()
tm.Sync()
termWidth, termHeight = tm.Size()
renderLock.Unlock()
}
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 {
termSync()
return termWidth
2015-03-20 06:24:48 -06:00
}
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 {
termSync()
return termHeight
2015-03-20 06:24:48 -06:00
}
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-10-10 09:20:44 -06:00
func render(bs ...Bufferer) {
defer func() {
if e := recover(); e != nil {
Close()
2018-09-06 14:57:56 -06:00
panic(debug.Stack())
}
}()
for _, b := range bs {
buf := b.Buffer()
// set cels in buf
for p, c := range buf.CellMap {
2015-05-09 17:29:22 -06:00
if p.In(buf.Area) {
tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
}
2015-02-03 18:56:49 -07:00
}
2015-02-03 07:07:31 -07:00
}
renderLock.Lock()
// render
2015-02-03 07:07:31 -07:00
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()
2015-02-03 07:07:31 -07:00
}
2015-09-18 09:41:44 -06:00
var renderJobs chan []Bufferer
2015-10-10 09:20:44 -06:00
func Render(bs ...Bufferer) {
//go func() { renderJobs <- bs }()
renderJobs <- bs
2015-09-18 09:41:44 -06:00
}