2020-11-17 03:12:24 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/miguelmota/gocui"
|
|
|
|
)
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// UI is the UI view struct
|
2020-11-17 03:12:24 +00:00
|
|
|
type UI struct {
|
|
|
|
g *gocui.Gui
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// NewUI returns a new UI instance
|
2020-11-17 03:12:24 +00:00
|
|
|
func NewUI() (*UI, error) {
|
|
|
|
g, err := gocui.NewGui(gocui.Output256)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &UI{
|
|
|
|
g: g,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// GetGocui returns the underlying gocui instance
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) GetGocui() *gocui.Gui {
|
|
|
|
return ui.g
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetFgColor sets the foreground color
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) SetFgColor(fgColor gocui.Attribute) {
|
|
|
|
ui.g.FgColor = fgColor
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetBgColor sets the background color
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) SetBgColor(bgColor gocui.Attribute) {
|
|
|
|
ui.g.BgColor = bgColor
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetInputEsc enables the escape key
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) SetInputEsc(enabled bool) {
|
2021-10-20 20:55:06 +00:00
|
|
|
ui.g.InputEsc = enabled
|
2020-11-17 03:12:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetMouse enables the mouse
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) SetMouse(enabled bool) {
|
2021-10-20 20:55:06 +00:00
|
|
|
ui.g.Mouse = enabled
|
2020-11-17 03:12:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 03:27:09 +00:00
|
|
|
// SetCursor enables the input field cursor
|
|
|
|
func (ui *UI) SetCursor(enabled bool) {
|
|
|
|
ui.g.Cursor = enabled
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetHighlight enables the highlight active state
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) SetHighlight(enabled bool) {
|
2021-10-20 20:55:06 +00:00
|
|
|
ui.g.Highlight = enabled
|
2020-11-17 03:12:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetManagerFunc sets the function to call for rendering UI
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) SetManagerFunc(fn func() error) {
|
|
|
|
ui.g.SetManagerFunc(func(*gocui.Gui) error {
|
|
|
|
return fn()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// MainLoop starts the UI render loop
|
2020-11-17 03:12:24 +00:00
|
|
|
func (ui *UI) MainLoop() error {
|
|
|
|
return ui.g.MainLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close ...
|
|
|
|
func (ui *UI) Close() {
|
|
|
|
ui.g.Close()
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:22:28 +00:00
|
|
|
// SetView sets the view layout
|
2020-11-17 03:12:24 +00:00
|
|
|
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()
|
|
|
|
}
|