diff --git a/app/focus_tracker.go b/app/focus_tracker.go index c647fc3d..1a2c1c27 100644 --- a/app/focus_tracker.go +++ b/app/focus_tracker.go @@ -1,6 +1,7 @@ package app import ( + "fmt" "sort" "github.com/olebedev/config" @@ -115,6 +116,7 @@ func (tracker *FocusTracker) Refocus() { // AssignHotKeys assigns an alphabetic keyboard character to each focusable // widget so that the widget can be brought into focus by pressing that keyboard key +// Valid numbers are between 1 and 9, inclusive func (tracker *FocusTracker) assignHotKeys() { if !tracker.useNavShortcuts() { return @@ -122,30 +124,37 @@ func (tracker *FocusTracker) assignHotKeys() { usedKeys := make(map[string]bool) focusables := tracker.focusables() - i := 1 + // First, block out the explicitly-defined characters so they can't be automatically + // assigned to other modules for _, focusable := range focusables { if focusable.FocusChar() != "" { usedKeys[focusable.FocusChar()] = true } } + + focusNum := 1 + + // Range over all the modules and assign focus characters to any that are focusable + // and don't have explicitly-defined focus characters for _, focusable := range focusables { if focusable.FocusChar() != "" { continue } - if _, foundKey := usedKeys[string('0'+i)]; foundKey { - for ; foundKey; _, foundKey = usedKeys[string('0'+i)] { - i++ + + if _, foundKey := usedKeys[fmt.Sprint(focusNum)]; foundKey { + for ; foundKey; _, foundKey = usedKeys[fmt.Sprint(focusNum)] { + focusNum++ } } - // Don't have nav characters > "9" - if i >= 10 { + // Don't allow focus characters > "9" + if focusNum >= 10 { break } - focusable.SetFocusChar(string('0' + i)) - i++ + focusable.SetFocusChar(fmt.Sprint(focusNum)) + focusNum++ } } diff --git a/cfg/common_settings.go b/cfg/common_settings.go index b6a6a729..1d8b6e8e 100644 --- a/cfg/common_settings.go +++ b/cfg/common_settings.go @@ -124,12 +124,18 @@ func (common *Common) DefaultRowColor() string { ) } +// FocusChar returns the keyboard number assigned to the widget used to give onscreen +// focus to this widget, as a string. Focus characters can be a range between 1 and 9 func (common *Common) FocusChar() string { - if common.focusChar <= -1 { + if common.focusChar <= 0 { return "" } - return string('0' + common.focusChar) + if common.focusChar > 9 { + return "" + } + + return fmt.Sprint(common.focusChar) } func (common *Common) RowColor(idx int) string { diff --git a/cfg/common_settings_test.go b/cfg/common_settings_test.go index f9d188ce..db1e82b5 100644 --- a/cfg/common_settings_test.go +++ b/cfg/common_settings_test.go @@ -47,29 +47,36 @@ func Test_DefaultRowColor(t *testing.T) { func Test_FocusChar(t *testing.T) { tests := []struct { name string - expectedChar string before func(testCfg *Common) + expectedChar string }{ { - name: "with no focus char specified", - expectedChar: "", + name: "with negative focus char", before: func(testCfg *Common) { testCfg.focusChar = -1 }, + expectedChar: "", }, { - name: "with explicit focus char specified", - expectedChar: "3", + name: "with positive focus char", before: func(testCfg *Common) { testCfg.focusChar = 3 }, + expectedChar: "3", }, { - name: "with ridiculous focus char specified", - expectedChar: "Q", + name: "with zero focus char", before: func(testCfg *Common) { - testCfg.focusChar = 33 + testCfg.focusChar = 0 }, + expectedChar: "", + }, + { + name: "with large focus char", + before: func(testCfg *Common) { + testCfg.focusChar = 10 + }, + expectedChar: "", }, }