2017-01-14 06:07:43 +00:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-20 20:21:50 +00:00
|
|
|
// 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
|
|
|
|
|
2015-09-18 15:41:44 +00:00
|
|
|
import (
|
2015-12-06 17:51:37 +00:00
|
|
|
"image"
|
2018-09-06 20:57:56 +00:00
|
|
|
"runtime/debug"
|
2015-12-06 17:51:37 +00:00
|
|
|
"sync"
|
2015-09-18 15:41:44 +00:00
|
|
|
|
2018-09-06 21:48:09 +00:00
|
|
|
tb "github.com/nsf/termbox-go"
|
2015-09-18 15:41:44 +00:00
|
|
|
)
|
2015-02-03 14:07:31 +00:00
|
|
|
|
2015-03-24 21:16:43 +00:00
|
|
|
// Bufferer should be implemented by all renderable components.
|
2015-03-03 18:28:09 +00:00
|
|
|
type Bufferer interface {
|
2015-04-21 13:56:10 +00:00
|
|
|
Buffer() Buffer
|
2015-02-03 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:16:43 +00: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 14:07:31 +00:00
|
|
|
func Init() error {
|
2018-09-06 21:48:09 +00:00
|
|
|
if err := tb.Init(); err != nil {
|
2015-05-09 23:29:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.SetInputMode(tb.InputEsc | tb.InputMouse)
|
2018-09-06 23:36:14 +00:00
|
|
|
// DefaultEvtStream = NewEvtStream()
|
2015-09-18 15:41:44 +00:00
|
|
|
|
2018-09-06 23:36:14 +00:00
|
|
|
// sysEvtChs = make([]chan Event, 0)
|
|
|
|
// go hookTermboxEvt()
|
2015-10-10 15:20:44 +00:00
|
|
|
|
2015-09-18 15:41:44 +00:00
|
|
|
renderJobs = make(chan []Bufferer)
|
2015-12-06 17:51:37 +00:00
|
|
|
//renderLock = new(sync.RWMutex)
|
2015-09-18 15:41:44 +00:00
|
|
|
|
2015-10-10 15:20:44 +00:00
|
|
|
Body = NewGrid()
|
|
|
|
Body.X = 0
|
|
|
|
Body.Y = 0
|
|
|
|
Body.BgColor = ThemeAttr("bg")
|
|
|
|
Body.Width = TermWidth()
|
|
|
|
|
2018-09-06 23:36:14 +00:00
|
|
|
Handle("<Resize>", func(e Event) {
|
|
|
|
payload := e.Payload.(Resize)
|
|
|
|
Body.Width = payload.Width
|
2015-10-07 18:25:59 +00:00
|
|
|
})
|
2015-09-18 15:41:44 +00:00
|
|
|
|
2015-10-07 18:25:59 +00:00
|
|
|
DefaultWgtMgr = NewWgtMgr()
|
2018-09-06 23:36:14 +00:00
|
|
|
EventHook(DefaultWgtMgr.WgtHandlersHook())
|
2015-12-06 17:51:37 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for bs := range renderJobs {
|
|
|
|
render(bs...)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-09 23:29:22 +00:00
|
|
|
return nil
|
2015-02-03 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:16:43 +00:00
|
|
|
// Close finalizes termui library,
|
|
|
|
// should be called after successful initialization when termui's functionality isn't required anymore.
|
2015-02-04 01:56:49 +00:00
|
|
|
func Close() {
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.Close()
|
2015-02-03 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 17:51:37 +00:00
|
|
|
var renderLock sync.Mutex
|
|
|
|
|
|
|
|
func termSync() {
|
|
|
|
renderLock.Lock()
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.Sync()
|
|
|
|
termWidth, termHeight = tb.Size()
|
2015-12-06 17:51:37 +00:00
|
|
|
renderLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:16:43 +00:00
|
|
|
// TermWidth returns the current terminal's width.
|
2015-03-20 12:24:48 +00:00
|
|
|
func TermWidth() int {
|
2015-12-06 17:51:37 +00:00
|
|
|
termSync()
|
2015-12-09 19:04:28 +00:00
|
|
|
return termWidth
|
2015-03-20 12:24:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:16:43 +00:00
|
|
|
// TermHeight returns the current terminal's height.
|
2015-03-20 12:24:48 +00:00
|
|
|
func TermHeight() int {
|
2015-12-06 17:51:37 +00:00
|
|
|
termSync()
|
2015-12-09 19:04:28 +00:00
|
|
|
return termHeight
|
2015-03-20 12:24:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:16:43 +00:00
|
|
|
// Render renders all Bufferer in the given order from left to right,
|
|
|
|
// right could overlap on left ones.
|
2015-10-10 15:20:44 +00:00
|
|
|
func render(bs ...Bufferer) {
|
2016-11-03 17:10:55 +00:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
Close()
|
2018-09-06 20:57:56 +00:00
|
|
|
panic(debug.Stack())
|
2016-11-03 17:10:55 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-04-21 13:56:10 +00:00
|
|
|
for _, b := range bs {
|
2015-12-09 19:04:28 +00:00
|
|
|
|
2015-04-21 13:56:10 +00:00
|
|
|
buf := b.Buffer()
|
|
|
|
// set cels in buf
|
|
|
|
for p, c := range buf.CellMap {
|
2015-05-09 23:29:22 +00:00
|
|
|
if p.In(buf.Area) {
|
2015-12-09 19:04:28 +00:00
|
|
|
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
|
2015-12-09 19:04:28 +00:00
|
|
|
|
2015-04-21 13:56:10 +00:00
|
|
|
}
|
2015-02-04 01:56:49 +00:00
|
|
|
}
|
2015-12-09 19:04:28 +00:00
|
|
|
|
2015-02-03 14:07:31 +00:00
|
|
|
}
|
2015-12-06 17:51:37 +00:00
|
|
|
|
|
|
|
renderLock.Lock()
|
2015-04-21 13:56:10 +00:00
|
|
|
// render
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.Flush()
|
2015-12-06 17:51:37 +00:00
|
|
|
renderLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Clear() {
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.Clear(tb.ColorDefault, toTmAttr(ThemeAttr("bg")))
|
2015-12-06 17:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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++ {
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.SetCell(i, j, ' ', tb.ColorDefault, toTmAttr(bg))
|
2015-12-06 17:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClearArea(r image.Rectangle, bg Attribute) {
|
|
|
|
clearArea(r, bg)
|
2018-09-06 21:48:09 +00:00
|
|
|
tb.Flush()
|
2015-02-03 14:07:31 +00:00
|
|
|
}
|
2015-09-18 15:41:44 +00:00
|
|
|
|
|
|
|
var renderJobs chan []Bufferer
|
|
|
|
|
2015-10-10 15:20:44 +00:00
|
|
|
func Render(bs ...Bufferer) {
|
2015-12-06 17:51:37 +00:00
|
|
|
//go func() { renderJobs <- bs }()
|
|
|
|
renderJobs <- bs
|
2015-09-18 15:41:44 +00:00
|
|
|
}
|