Fix menu navigation, informational items, and boolean editing
This commit is contained in:
8 files changed
+355
-24
No files matched your search
+46
-14
@@ -26,6 +26,9 @@ type Menu struct {
|
||||
pageSize int
|
||||
}
|
||||
|
||||
// New builds a menu over the exported fields of a struct pointer. It returns an
|
||||
// error for non-struct or unaddressable data, unsupported fields, or invalid tags.
|
||||
// Values remain owned by the caller; menu edits and reads must be serialized.
|
||||
func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
ref := reflect.ValueOf(data)
|
||||
|
||||
@@ -41,6 +44,9 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
if ref.Kind() != reflect.Struct {
|
||||
return nil, fmt.Errorf("menu: %T is not a struct", data)
|
||||
}
|
||||
if !ref.CanAddr() {
|
||||
return nil, fmt.Errorf("menu: %T must be a pointer to a struct", data)
|
||||
}
|
||||
|
||||
menu := &Menu{
|
||||
Parent: parent,
|
||||
@@ -51,16 +57,20 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
Value: Back,
|
||||
}},
|
||||
}
|
||||
menu.SetPageSize(pageSize)
|
||||
|
||||
typ := ref.Type()
|
||||
for i, n := 0, ref.NumField(); i < n; i++ {
|
||||
field := typ.Field(i)
|
||||
if field.Tag.Get("menu") == "-" || !field.IsExported() {
|
||||
continue
|
||||
}
|
||||
var (
|
||||
field = typ.Field(i)
|
||||
fieldRef = ref.Field(i)
|
||||
value = ref.Field(i).Addr().Interface()
|
||||
icon = field.Tag.Get("icon")
|
||||
label = field.Tag.Get("menu")
|
||||
options = strings.Split(field.Tag.Get("options"), ",")
|
||||
options []string
|
||||
step = 1
|
||||
min, max int
|
||||
err error
|
||||
@@ -68,8 +78,9 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
|
||||
if len(label) == 0 {
|
||||
label = field.Name
|
||||
} else if label == "-" {
|
||||
continue
|
||||
}
|
||||
if tag := field.Tag.Get("options"); tag != "" {
|
||||
options = strings.Split(tag, ",")
|
||||
}
|
||||
|
||||
if stepTag := field.Tag.Get("step"); stepTag != "" {
|
||||
@@ -89,10 +100,10 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
}
|
||||
|
||||
if !fieldRef.CanAddr() {
|
||||
panic(fmt.Sprintf("fieldref %T for %T is not addressable", fieldRef, value))
|
||||
return nil, fmt.Errorf("menu: field %s is not addressable", field.Name)
|
||||
}
|
||||
if !fieldRef.CanSet() {
|
||||
panic(fmt.Sprintf("fieldref %T for %T is not settable", fieldRef, value))
|
||||
return nil, fmt.Errorf("menu: field %s is not settable", field.Name)
|
||||
}
|
||||
|
||||
switch field.Type.Kind() {
|
||||
@@ -198,9 +209,9 @@ func (m *Menu) edit(delta int) {
|
||||
// Adjust the value if editing
|
||||
switch v := item.Value.(type) {
|
||||
case *uint8:
|
||||
newValue := *v + uint8(delta*item.Step)
|
||||
if newValue >= uint8(item.Min) && newValue <= uint8(item.Max) {
|
||||
*v = newValue
|
||||
newValue := int(*v) + delta*item.Step
|
||||
if newValue >= item.Min && newValue <= item.Max {
|
||||
*v = uint8(newValue)
|
||||
}
|
||||
case *int:
|
||||
newValue := *v + (delta * item.Step)
|
||||
@@ -209,13 +220,13 @@ func (m *Menu) edit(delta int) {
|
||||
}
|
||||
case *bool:
|
||||
if delta != 0 {
|
||||
*v = !*v
|
||||
*v = delta > 0
|
||||
}
|
||||
case *string:
|
||||
if len(item.Options) > 0 {
|
||||
currentIndex := indexOf(*v, item.Options)
|
||||
if currentIndex != -1 {
|
||||
newIndex := (currentIndex + delta + len(item.Options)) % len(item.Options)
|
||||
newIndex := (currentIndex + delta%len(item.Options) + len(item.Options)) % len(item.Options)
|
||||
*v = item.Options[newIndex]
|
||||
}
|
||||
}
|
||||
@@ -254,8 +265,11 @@ func (m *Menu) PageSize() int {
|
||||
return m.pageSize
|
||||
}
|
||||
|
||||
// SetPageSize updates the page size.
|
||||
// SetPageSize updates this menu and its submenus, with a minimum of one item.
|
||||
func (m *Menu) SetPageSize(pageSize int) {
|
||||
if pageSize < 1 {
|
||||
pageSize = 1
|
||||
}
|
||||
m.pageSize = pageSize
|
||||
for _, item := range m.Items {
|
||||
if item.SubMenu != nil {
|
||||
@@ -269,9 +283,14 @@ func (m *Menu) IsEditing() bool {
|
||||
return m.Editing
|
||||
}
|
||||
|
||||
// ToggleEdit switches between navigation and editing modes.
|
||||
// ToggleEdit exits editing or starts editing an editable item. Informational
|
||||
// items, submenu entries and Back remain in navigation mode.
|
||||
func (m *Menu) ToggleEdit() {
|
||||
m.Editing = !m.Editing
|
||||
if m.Editing {
|
||||
m.Editing = false
|
||||
} else if m.ActiveItem().IsEditable() {
|
||||
m.Editing = true
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) IsSubMenu() bool {
|
||||
@@ -324,6 +343,19 @@ type Item struct {
|
||||
Options []string
|
||||
}
|
||||
|
||||
// IsEditable reports whether the item accepts rotary input. Strings without
|
||||
// enumerated options are informational and remain read-only.
|
||||
func (item Item) IsEditable() bool {
|
||||
switch item.Value.(type) {
|
||||
case *bool, *uint8, *int:
|
||||
return true
|
||||
case *string:
|
||||
return len(item.Options) > 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
Reference in new issue
Block a user