265 lines
5.9 KiB
Go
265 lines
5.9 KiB
Go
package menu
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Menu struct {
|
|
Items []Item
|
|
|
|
// Current selected position.
|
|
Position int
|
|
|
|
// Number of items visible per page.
|
|
PageSize int
|
|
|
|
// Parent menu for this submenu.
|
|
Parent *Menu
|
|
|
|
// IsEditing indicates edit mode for an item.
|
|
IsEditing bool
|
|
}
|
|
|
|
func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
|
ref := reflect.ValueOf(data)
|
|
|
|
// if its a pointer, resolve its value
|
|
if ref.Kind() == reflect.Ptr {
|
|
ref = reflect.Indirect(ref)
|
|
}
|
|
|
|
if ref.Kind() == reflect.Interface {
|
|
ref = ref.Elem()
|
|
}
|
|
|
|
if ref.Kind() != reflect.Struct {
|
|
return nil, fmt.Errorf("menu: %T is not a struct", data)
|
|
}
|
|
|
|
menu := &Menu{
|
|
PageSize: pageSize,
|
|
Parent: parent,
|
|
}
|
|
|
|
typ := ref.Type()
|
|
for i, n := 0, ref.NumField(); i < n; i++ {
|
|
var (
|
|
field = typ.Field(i)
|
|
fieldRef = ref.Field(i)
|
|
value = ref.Field(i).Interface()
|
|
icon = field.Tag.Get("icon")
|
|
label = field.Tag.Get("menu")
|
|
options = strings.Split(field.Tag.Get("options"), ",")
|
|
step = 1
|
|
min, max int
|
|
err error
|
|
)
|
|
|
|
if len(label) == 0 {
|
|
label = field.Name
|
|
} else if label == "-" {
|
|
continue
|
|
}
|
|
|
|
if stepTag := field.Tag.Get("step"); stepTag != "" {
|
|
if step, err = strconv.Atoi(stepTag); err != nil {
|
|
return nil, fmt.Errorf("menu: invalid `step` setting on field %s: %w", field.Name, err)
|
|
}
|
|
}
|
|
if minTag := field.Tag.Get("min"); minTag != "" {
|
|
if min, err = strconv.Atoi(minTag); err != nil {
|
|
return nil, fmt.Errorf("menu: invalid `min` setting on field %s: %w", field.Name, err)
|
|
}
|
|
}
|
|
if maxTag := field.Tag.Get("max"); maxTag != "" {
|
|
if max, err = strconv.Atoi(maxTag); err != nil {
|
|
return nil, fmt.Errorf("menu: invalid `max` setting on field %s: %w", field.Name, err)
|
|
}
|
|
}
|
|
|
|
switch field.Type.Kind() {
|
|
case reflect.Struct:
|
|
subMenu, err := New(value, pageSize, menu)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
menu.Items = append(menu.Items, Item{
|
|
Label: label,
|
|
Value: nil,
|
|
Field: fieldRef,
|
|
Icon: icon,
|
|
SubMenu: subMenu,
|
|
})
|
|
|
|
case reflect.Bool, reflect.String:
|
|
menu.Items = append(menu.Items, Item{
|
|
Label: label,
|
|
Value: value,
|
|
Field: fieldRef,
|
|
Icon: icon,
|
|
Options: options,
|
|
})
|
|
|
|
case reflect.Uint8:
|
|
if min < 0 || min > math.MaxUint8 || max < 0 || max > math.MaxUint8 {
|
|
return nil, fmt.Errorf("menu: min/max %d/%d on field %s.%s is out of range for uint8", min, max, typ.Name(), field.Name)
|
|
}
|
|
if max == 0 {
|
|
max = math.MaxUint8
|
|
}
|
|
menu.Items = append(menu.Items, Item{
|
|
Label: label,
|
|
Value: value,
|
|
Field: fieldRef,
|
|
Icon: icon,
|
|
Min: min,
|
|
Max: max,
|
|
Step: step,
|
|
})
|
|
|
|
case reflect.Int8:
|
|
if min < math.MinInt8 || min > math.MaxInt8 || max < math.MinInt8 || max > math.MaxInt8 {
|
|
return nil, fmt.Errorf("menu: min/max %d/%d on field %s.%s is out of range for int8", min, max, typ.Name(), field.Name)
|
|
}
|
|
if min == 0 && max == 0 {
|
|
min, max = math.MinInt8, math.MaxInt8
|
|
}
|
|
if min == max {
|
|
return nil, fmt.Errorf("menu: no min/max range specified on field %s.%s", typ.Name(), field.Name)
|
|
}
|
|
menu.Items = append(menu.Items, Item{
|
|
Label: label,
|
|
Value: value,
|
|
Field: fieldRef,
|
|
Icon: icon,
|
|
Min: min,
|
|
Max: max,
|
|
Step: step,
|
|
})
|
|
|
|
case reflect.Int:
|
|
if min == max {
|
|
return nil, fmt.Errorf("menu: no min/max range specified for field %s.%s", typ.Name(), field.Name)
|
|
}
|
|
menu.Items = append(menu.Items, Item{
|
|
Label: label,
|
|
Value: value,
|
|
Field: fieldRef,
|
|
Icon: icon,
|
|
Min: min,
|
|
Max: max,
|
|
Step: step,
|
|
})
|
|
|
|
default:
|
|
return nil, fmt.Errorf("menu: can't render field %s.%s with type %s", typ.Name(), field.Name, field.Type.Kind())
|
|
}
|
|
}
|
|
|
|
return menu, nil
|
|
}
|
|
|
|
// Change is used for editing and navigating, delta is either positive or negative input.
|
|
func (m *Menu) Change(delta int) {
|
|
if m.IsEditing {
|
|
m.edit(delta)
|
|
} else {
|
|
m.move(delta)
|
|
}
|
|
}
|
|
|
|
// ToggleEdit switches between navigation and editing modes.
|
|
func (m *Menu) ToggleEdit() {
|
|
m.IsEditing = !m.IsEditing
|
|
}
|
|
|
|
// EnterSubmenu enters the selected submenu
|
|
func (m *Menu) EnterSubmenu() *Menu {
|
|
if m.Position < len(m.Items) && m.Items[m.Position].SubMenu != nil {
|
|
return m.Items[m.Position].SubMenu
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ExitSubmenu returns to the parent menu
|
|
func (m *Menu) ExitSubmenu() *Menu {
|
|
if m.Parent != nil {
|
|
return m.Parent
|
|
}
|
|
return m // If no parent, return itself (root menu)
|
|
}
|
|
|
|
func (m *Menu) edit(delta int) {
|
|
// Adjust the value if editing
|
|
item := &m.Items[m.Position]
|
|
switch v := item.Value.(type) {
|
|
case int:
|
|
newValue := v + delta
|
|
if newValue >= item.Min && newValue <= item.Max {
|
|
item.Value = newValue
|
|
item.Field.SetInt(int64(newValue)) // Update the original struct field
|
|
}
|
|
case bool:
|
|
if delta != 0 {
|
|
item.Value = !v
|
|
item.Field.SetBool(!v) // Update the original struct field
|
|
}
|
|
case string:
|
|
if len(item.Options) > 0 {
|
|
currentIndex := indexOf(v, item.Options)
|
|
if currentIndex != -1 {
|
|
newIndex := (currentIndex + delta + len(item.Options)) % len(item.Options)
|
|
item.Value = item.Options[newIndex]
|
|
item.Field.SetString(item.Options[newIndex]) // Update the original struct field
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *Menu) move(delta int) {
|
|
// Move the cursor if not editing
|
|
m.Position += delta
|
|
if m.Position < 0 {
|
|
m.Position = 0
|
|
} else if m.Position >= len(m.Items) {
|
|
m.Position = len(m.Items) - 1
|
|
}
|
|
}
|
|
|
|
type Item struct {
|
|
// Label on the menu.
|
|
Label string
|
|
|
|
// Icon on the menu.
|
|
Icon string
|
|
|
|
// Value is the original value.
|
|
Value interface{}
|
|
|
|
// Field is the original struct field.
|
|
Field reflect.Value
|
|
|
|
// SubMenu is the submenu item.
|
|
SubMenu *Menu
|
|
|
|
// Min, Max and Step provides the bounds for integers.
|
|
Min, Max, Step int
|
|
|
|
// Options for string values.
|
|
Options []string
|
|
}
|
|
|
|
// indexOf returns the index of a value in a slice, or -1 if not found
|
|
func indexOf(needle string, haystack []string) int {
|
|
for i, opt := range haystack {
|
|
if opt == needle {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|