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 {
|
||||
|
||||
+146
-4
@@ -49,10 +49,152 @@ func TestMenuNil(t *testing.T) {
|
||||
|
||||
func TestMenuNotPointer(t *testing.T) {
|
||||
var settings testSettings
|
||||
menu, err := New(settings, 4, nil)
|
||||
_, err := New(settings, 4, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for an unaddressable struct")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInformationalItemsRemainNavigable(t *testing.T) {
|
||||
data := struct {
|
||||
LoadAverage string
|
||||
LocalTime string
|
||||
}{LoadAverage: "0.12 0.10 0.08", LocalTime: "12:00"}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, position := range []int{1, 2} {
|
||||
m.Position = position
|
||||
m.ToggleEdit()
|
||||
if m.IsEditing() {
|
||||
t.Fatal("informational item entered edit mode")
|
||||
}
|
||||
m.Change(-position)
|
||||
if m.ActiveItem().Value != Back {
|
||||
t.Fatal("cannot navigate back after selecting information")
|
||||
}
|
||||
}
|
||||
if data.LoadAverage != "0.12 0.10 0.08" || data.LocalTime != "12:00" {
|
||||
t.Fatal("informational values changed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubmenuBackPreservesParentPosition(t *testing.T) {
|
||||
var data testSettings
|
||||
root, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root.Position = 2
|
||||
child := root.EnterSubMenu()
|
||||
if child == nil {
|
||||
t.Fatal("missing submenu")
|
||||
}
|
||||
if child.ActiveItem().Value != Back {
|
||||
t.Fatal("submenu should initially select Back")
|
||||
}
|
||||
if got := child.ExitSubMenu(); got != root || got.Position != 2 {
|
||||
t.Fatal("Back did not restore parent selection")
|
||||
}
|
||||
root.Position = 0
|
||||
root.ToggleEdit()
|
||||
if root.IsEditing() {
|
||||
t.Fatal("Back entered edit mode")
|
||||
}
|
||||
if root.ExitSubMenu() != nil {
|
||||
t.Fatal("root should exit settings")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringOptionsLargeNegativeDelta(t *testing.T) {
|
||||
data := struct {
|
||||
Sleep string `options:"5min,15min,hour,day"`
|
||||
}{Sleep: "hour"}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Position = 1
|
||||
m.ToggleEdit()
|
||||
if !m.IsEditing() {
|
||||
t.Fatal("option item should be editable")
|
||||
}
|
||||
m.Change(-11)
|
||||
if data.Sleep != "day" {
|
||||
t.Fatalf("got %q, want day", data.Sleep)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8DoesNotWrap(t *testing.T) {
|
||||
data := struct{ Contrast uint8 }{Contrast: 255}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Position = 1
|
||||
m.ToggleEdit()
|
||||
m.Change(1)
|
||||
if data.Contrast != 255 {
|
||||
t.Fatal("contrast wrapped above maximum")
|
||||
}
|
||||
data.Contrast = 0
|
||||
m.Change(-1)
|
||||
if data.Contrast != 0 {
|
||||
t.Fatal("contrast wrapped below minimum")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinimumPageSize(t *testing.T) {
|
||||
var data testSettings
|
||||
m, err := New(&data, 0, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.PageSize() != 1 {
|
||||
t.Fatal("page size must be positive")
|
||||
}
|
||||
m.SetPageSize(-1)
|
||||
if m.Items[1].SubMenu.PageSize() != 1 {
|
||||
t.Fatal("submenu page size must be positive")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBooleanEditingUsesDirection(t *testing.T) {
|
||||
for _, initial := range []bool{false, true} {
|
||||
data := struct{ Enabled bool }{Enabled: initial}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Position = 1
|
||||
m.ToggleEdit()
|
||||
for _, delta := range []int{1, 1, 3, 0, -1, -1, -4, 0, 1} {
|
||||
before := data.Enabled
|
||||
m.Change(delta)
|
||||
want := delta > 0
|
||||
if delta == 0 {
|
||||
want = before
|
||||
}
|
||||
if data.Enabled != want {
|
||||
t.Fatalf("initial=%v delta=%d: got %v, want %v", initial, delta, data.Enabled, want)
|
||||
}
|
||||
if m.Position != 1 || !m.IsEditing() {
|
||||
t.Fatal("rotating left boolean edit mode")
|
||||
}
|
||||
}
|
||||
m.ToggleEdit()
|
||||
if m.IsEditing() || !data.Enabled {
|
||||
t.Fatal("confirming did not preserve the selected value")
|
||||
}
|
||||
m.Change(-1)
|
||||
if m.Position != 0 || !data.Enabled {
|
||||
t.Fatal("navigation changed the confirmed value")
|
||||
}
|
||||
m.Change(1)
|
||||
m.ToggleEdit()
|
||||
if !data.Enabled {
|
||||
t.Fatal("reopening edit mode reset the value")
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("menu: %#+v", menu)
|
||||
}
|
||||
Reference in new issue
Block a user