/
ui.go
99 lines (82 loc) 路 1.94 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ui
import (
"github.com/miguelmota/gocui"
)
// UI is the UI view struct
type UI struct {
g *gocui.Gui
}
// NewUI returns a new UI instance
func NewUI() (*UI, error) {
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
return nil, err
}
return &UI{
g: g,
}, nil
}
// GetGocui returns the underlying gocui instance
func (ui *UI) GetGocui() *gocui.Gui {
return ui.g
}
// SetFgColor sets the foreground color
func (ui *UI) SetFgColor(fgColor gocui.Attribute) {
ui.g.FgColor = fgColor
}
// SetBgColor sets the background color
func (ui *UI) SetBgColor(bgColor gocui.Attribute) {
ui.g.BgColor = bgColor
}
// SetInputEsc enables the escape key
func (ui *UI) SetInputEsc(enabled bool) {
ui.g.InputEsc = enabled
}
// SetMouse enables the mouse
func (ui *UI) SetMouse(enabled bool) {
ui.g.Mouse = enabled
}
// SetCursor enables the input field cursor
func (ui *UI) SetCursor(enabled bool) {
ui.g.Cursor = enabled
}
// SetHighlight enables the highlight active state
func (ui *UI) SetHighlight(enabled bool) {
ui.g.Highlight = enabled
}
// SetManagerFunc sets the function to call for rendering UI
func (ui *UI) SetManagerFunc(fn func() error) {
ui.g.SetManagerFunc(func(*gocui.Gui) error {
return fn()
})
}
// MainLoop starts the UI render loop
func (ui *UI) MainLoop() error {
return ui.g.MainLoop()
}
// Close ...
func (ui *UI) Close() {
ui.g.Close()
}
// SetView sets the view layout
func (ui *UI) SetView(view interface{}, x, y, w, h int) error {
if v, ok := view.(*View); ok {
gv, err := ui.g.SetView(v.Name(), x, y, w, h)
v.SetBacking(gv)
return err
}
return nil
}
// SetViewOnBottom sets the view to the bottom layer
func (ui *UI) SetViewOnBottom(view interface{}) error {
if v, ok := view.(*View); ok {
if _, err := ui.g.SetViewOnBottom(v.Name()); err != nil {
return err
}
}
return nil
}
// ActiveViewName returns the name of the active view
func (ui *UI) ActiveViewName() string {
return ui.g.CurrentView().Name()
}