talc/pkg/menu/menu_model.go
2020-03-18 02:41:33 +01:00

90 lines
1.4 KiB
Go

package menu
import (
"github.com/gdamore/tcell"
)
type MenuModel interface {
GetBounds() (int, int)
GetCurrent() Option
//GetParent() *Option
GetCursor() (int, bool, bool)
GetOption(int) Option
MoveCursor(y int)
SetCurrent(Option)
//SetParent(*Option)
SetCursor(int)
}
type menuModel struct {
//parent *Option
current Option
width int
height int
y int
hide bool
cursor bool
style tcell.Style
}
func (m *menuModel) GetBounds() (int, int) {
return m.width, m.height
}
// func (m *menuModel) GetParent() *Option {
// return m.parent
// }
// func (m *menuModel) SetParent(opt *Option) {
// m.parent = opt
// }
func (m *menuModel) GetCurrent() Option {
return m.current
}
func (m *menuModel) SetCurrent(opt Option) {
m.current = opt
m.y = opt.y
m.height = len(m.current.Options)
m.width = 0
for _, opt := range m.current.Options {
if len(opt.Name) > m.width {
m.width = len(opt.Name)
}
}
return
}
func (m *menuModel) SetCursor(y int) {
m.y = y
m.limitCursor()
}
func (m *menuModel) GetOption(y int) Option {
if y < 0 || y >= len(m.current.Options) {
return Option{}
}
return m.current.Options[y]
}
func (m *menuModel) MoveCursor(y int) {
m.y += y
m.limitCursor()
}
func (m *menuModel) limitCursor() {
if m.y > m.height-1 {
m.y = m.height - 1
}
if m.y < 0 {
m.y = 0
}
}
func (m *menuModel) GetCursor() (int, bool, bool) {
return m.y, m.cursor, !m.hide
}