mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
2.7 KiB
136 lines
2.7 KiB
9 years ago
|
// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved.
|
||
10 years ago
|
// Use of this source code is governed by a MIT license that can
|
||
|
// be found in the LICENSE file.
|
||
|
|
||
|
package termui
|
||
|
|
||
9 years ago
|
import (
|
||
|
"image"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
tm "github.com/nsf/termbox-go"
|
||
|
)
|
||
10 years ago
|
|
||
|
// Bufferer should be implemented by all renderable components.
|
||
|
type Bufferer interface {
|
||
9 years ago
|
Buffer() Buffer
|
||
10 years ago
|
}
|
||
|
|
||
|
// 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 {
|
||
9 years ago
|
if err := tm.Init(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
sysEvtChs = make([]chan Event, 0)
|
||
|
go hookTermboxEvt()
|
||
|
|
||
|
renderJobs = make(chan []Bufferer)
|
||
|
//renderLock = new(sync.RWMutex)
|
||
|
|
||
10 years ago
|
Body = NewGrid()
|
||
|
Body.X = 0
|
||
|
Body.Y = 0
|
||
9 years ago
|
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...)
|
||
|
}
|
||
10 years ago
|
}()
|
||
9 years ago
|
|
||
|
return nil
|
||
10 years ago
|
}
|
||
|
|
||
|
// Close finalizes termui library,
|
||
|
// should be called after successful initialization when termui's functionality isn't required anymore.
|
||
|
func Close() {
|
||
|
tm.Close()
|
||
|
}
|
||
|
|
||
9 years ago
|
var renderLock sync.Mutex
|
||
|
|
||
|
func termSync() {
|
||
|
renderLock.Lock()
|
||
|
tm.Sync()
|
||
|
termWidth, termHeight = tm.Size()
|
||
|
renderLock.Unlock()
|
||
|
}
|
||
|
|
||
10 years ago
|
// TermWidth returns the current terminal's width.
|
||
|
func TermWidth() int {
|
||
9 years ago
|
termSync()
|
||
|
return termWidth
|
||
10 years ago
|
}
|
||
|
|
||
|
// TermHeight returns the current terminal's height.
|
||
|
func TermHeight() int {
|
||
9 years ago
|
termSync()
|
||
|
return termHeight
|
||
10 years ago
|
}
|
||
|
|
||
|
// Render renders all Bufferer in the given order from left to right,
|
||
|
// right could overlap on left ones.
|
||
9 years ago
|
func render(bs ...Bufferer) {
|
||
|
|
||
|
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))
|
||
|
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
renderLock.Lock()
|
||
|
// render
|
||
10 years ago
|
tm.Flush()
|
||
9 years ago
|
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
|
||
10 years ago
|
}
|