2015-03-21 04:21:50 +08: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 22:07:31 +08:00
|
|
|
package termui
|
|
|
|
|
2015-09-18 23:41:44 +08:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tm "github.com/nsf/termbox-go"
|
|
|
|
)
|
2015-02-03 22:07:31 +08:00
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Bufferer should be implemented by all renderable components.
|
2015-03-04 02:28:09 +08:00
|
|
|
type Bufferer interface {
|
2015-04-21 21:56:10 +08:00
|
|
|
Buffer() Buffer
|
2015-02-03 22:07:31 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08: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 22:07:31 +08:00
|
|
|
func Init() error {
|
2015-05-10 07:29:22 +08:00
|
|
|
if err := tm.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-18 23:41:44 +08:00
|
|
|
|
|
|
|
sysEvtChs = make([]chan Event, 0)
|
|
|
|
go hookTermboxEvt()
|
|
|
|
renderJobs = make(chan []Bufferer)
|
|
|
|
go func() {
|
|
|
|
for bs := range renderJobs {
|
|
|
|
Render(bs...)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
DefaultEvtStream.Init()
|
|
|
|
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
|
|
|
|
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
|
|
|
|
DefaultEvtStream.Handle("/", DefualtHandler)
|
|
|
|
|
2015-05-10 07:29:22 +08:00
|
|
|
return nil
|
2015-02-03 22:07:31 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Close finalizes termui library,
|
|
|
|
// should be called after successful initialization when termui's functionality isn't required anymore.
|
2015-02-04 09:56:49 +08:00
|
|
|
func Close() {
|
2015-02-03 22:07:31 +08:00
|
|
|
tm.Close()
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// TermWidth returns the current terminal's width.
|
2015-03-20 20:24:48 +08:00
|
|
|
func TermWidth() int {
|
2015-05-05 22:55:35 +08:00
|
|
|
tm.Sync()
|
2015-03-20 20:24:48 +08:00
|
|
|
w, _ := tm.Size()
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// TermHeight returns the current terminal's height.
|
2015-03-20 20:24:48 +08:00
|
|
|
func TermHeight() int {
|
2015-05-05 22:55:35 +08:00
|
|
|
tm.Sync()
|
2015-03-20 20:24:48 +08:00
|
|
|
_, h := tm.Size()
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Render renders all Bufferer in the given order from left to right,
|
|
|
|
// right could overlap on left ones.
|
2015-04-21 21:56:10 +08:00
|
|
|
func Render(bs ...Bufferer) {
|
|
|
|
// set tm bg
|
2015-09-21 15:11:58 +08:00
|
|
|
tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg")))
|
2015-04-21 21:56:10 +08:00
|
|
|
for _, b := range bs {
|
|
|
|
buf := b.Buffer()
|
|
|
|
// set cels in buf
|
|
|
|
for p, c := range buf.CellMap {
|
2015-05-10 07:29:22 +08:00
|
|
|
if p.In(buf.Area) {
|
2015-04-21 21:56:10 +08:00
|
|
|
tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
|
|
|
|
}
|
2015-02-04 09:56:49 +08:00
|
|
|
}
|
2015-02-03 22:07:31 +08:00
|
|
|
}
|
2015-04-21 21:56:10 +08:00
|
|
|
// render
|
2015-02-03 22:07:31 +08:00
|
|
|
tm.Flush()
|
|
|
|
}
|
2015-09-18 23:41:44 +08:00
|
|
|
|
|
|
|
var renderJobs chan []Bufferer
|
|
|
|
|
|
|
|
func SendBufferToRender(bs ...Bufferer) {
|
|
|
|
go func() { renderJobs <- bs }()
|
|
|
|
}
|