2017-01-10 18:33:17 +00:00
|
|
|
// Copyright 2016 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-06-24 15:40:18 +00:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
package termui
|
|
|
|
|
2016-02-11 14:16:52 +00:00
|
|
|
import (
|
|
|
|
"image"
|
2017-01-10 18:33:17 +00:00
|
|
|
"io"
|
2016-02-11 14:16:52 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-01-10 18:33:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"runtime/debug"
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/maruel/panicparse/stack"
|
2016-02-11 14:16:52 +00:00
|
|
|
tm "github.com/nsf/termbox-go"
|
|
|
|
)
|
2015-06-24 15:40:18 +00:00
|
|
|
|
|
|
|
// Bufferer should be implemented by all renderable components.
|
|
|
|
type Bufferer interface {
|
2016-02-11 14:16:52 +00:00
|
|
|
Buffer() Buffer
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes termui library. This function should be called before any others.
|
|
|
|
// After initialization, the library must be finalized by 'Close' function.
|
|
|
|
func Init() error {
|
2016-02-11 14:16:52 +00:00
|
|
|
if err := tm.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sysEvtChs = make([]chan Event, 0)
|
|
|
|
go hookTermboxEvt()
|
|
|
|
|
|
|
|
renderJobs = make(chan []Bufferer)
|
|
|
|
//renderLock = new(sync.RWMutex)
|
|
|
|
|
2015-06-24 15:40:18 +00:00
|
|
|
Body = NewGrid()
|
|
|
|
Body.X = 0
|
|
|
|
Body.Y = 0
|
2016-02-11 14:16:52 +00:00
|
|
|
Body.BgColor = ThemeAttr("bg")
|
|
|
|
Body.Width = TermWidth()
|
|
|
|
|
|
|
|
DefaultEvtStream.Init()
|
|
|
|
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
|
|
|
|
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
|
|
|
|
DefaultEvtStream.Merge("custom", usrEvtCh)
|
|
|
|
|
|
|
|
DefaultEvtStream.Handle("/", DefualtHandler)
|
|
|
|
DefaultEvtStream.Handle("/sys/wnd/resize", func(e Event) {
|
|
|
|
w := e.Data.(EvtWnd)
|
|
|
|
Body.Width = w.Width
|
|
|
|
})
|
|
|
|
|
|
|
|
DefaultWgtMgr = NewWgtMgr()
|
|
|
|
DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for bs := range renderJobs {
|
|
|
|
render(bs...)
|
|
|
|
}
|
2015-06-24 15:40:18 +00:00
|
|
|
}()
|
2016-02-11 14:16:52 +00:00
|
|
|
|
|
|
|
return nil
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close finalizes termui library,
|
|
|
|
// should be called after successful initialization when termui's functionality isn't required anymore.
|
|
|
|
func Close() {
|
|
|
|
tm.Close()
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:16:52 +00:00
|
|
|
var renderLock sync.Mutex
|
|
|
|
|
|
|
|
func termSync() {
|
|
|
|
renderLock.Lock()
|
|
|
|
tm.Sync()
|
|
|
|
termWidth, termHeight = tm.Size()
|
|
|
|
renderLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:40:18 +00:00
|
|
|
// TermWidth returns the current terminal's width.
|
|
|
|
func TermWidth() int {
|
2016-02-11 14:16:52 +00:00
|
|
|
termSync()
|
|
|
|
return termWidth
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TermHeight returns the current terminal's height.
|
|
|
|
func TermHeight() int {
|
2016-02-11 14:16:52 +00:00
|
|
|
termSync()
|
|
|
|
return termHeight
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders all Bufferer in the given order from left to right,
|
|
|
|
// right could overlap on left ones.
|
2016-02-11 14:16:52 +00:00
|
|
|
func render(bs ...Bufferer) {
|
2017-01-10 18:33:17 +00:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
Close()
|
|
|
|
fmt.Fprintf(os.Stderr, "Captured a panic(value=%v) when rendering Bufferer. Exit termui and clean terminal...\nPrint stack trace:\n\n", e)
|
|
|
|
//debug.PrintStack()
|
|
|
|
gs, err := stack.ParseDump(bytes.NewReader(debug.Stack()), os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
debug.PrintStack()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
p := &stack.Palette{}
|
|
|
|
buckets := stack.SortBuckets(stack.Bucketize(gs, stack.AnyValue))
|
|
|
|
srcLen, pkgLen := stack.CalcLengths(buckets, false)
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
io.WriteString(os.Stdout, p.BucketHeader(&bucket, false, len(buckets) > 1))
|
|
|
|
io.WriteString(os.Stdout, p.StackLines(&bucket.Signature, srcLen, pkgLen, false))
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
2016-02-11 14:16:52 +00:00
|
|
|
for _, b := range bs {
|
|
|
|
|
|
|
|
buf := b.Buffer()
|
|
|
|
// set cels in buf
|
|
|
|
for p, c := range buf.CellMap {
|
|
|
|
if p.In(buf.Area) {
|
|
|
|
|
|
|
|
tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
|
|
|
|
|
|
|
|
}
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|
2016-02-11 14:16:52 +00:00
|
|
|
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|
2016-02-11 14:16:52 +00:00
|
|
|
|
|
|
|
renderLock.Lock()
|
|
|
|
// render
|
2015-06-24 15:40:18 +00:00
|
|
|
tm.Flush()
|
2016-02-11 14:16:52 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
var renderJobs chan []Bufferer
|
|
|
|
|
|
|
|
func Render(bs ...Bufferer) {
|
|
|
|
//go func() { renderJobs <- bs }()
|
|
|
|
renderJobs <- bs
|
2015-06-24 15:40:18 +00:00
|
|
|
}
|