1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Fix warning with type conversion from rune + int to string

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer 2020-09-25 06:47:28 -07:00
parent cae3ae7c4b
commit d672f57c14
3 changed files with 40 additions and 18 deletions

View File

@ -1,6 +1,7 @@
package app package app
import ( import (
"fmt"
"sort" "sort"
"github.com/olebedev/config" "github.com/olebedev/config"
@ -115,6 +116,7 @@ func (tracker *FocusTracker) Refocus() {
// AssignHotKeys assigns an alphabetic keyboard character to each focusable // AssignHotKeys assigns an alphabetic keyboard character to each focusable
// widget so that the widget can be brought into focus by pressing that keyboard key // 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() { func (tracker *FocusTracker) assignHotKeys() {
if !tracker.useNavShortcuts() { if !tracker.useNavShortcuts() {
return return
@ -122,30 +124,37 @@ func (tracker *FocusTracker) assignHotKeys() {
usedKeys := make(map[string]bool) usedKeys := make(map[string]bool)
focusables := tracker.focusables() 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 { for _, focusable := range focusables {
if focusable.FocusChar() != "" { if focusable.FocusChar() != "" {
usedKeys[focusable.FocusChar()] = true 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 { for _, focusable := range focusables {
if focusable.FocusChar() != "" { if focusable.FocusChar() != "" {
continue continue
} }
if _, foundKey := usedKeys[string('0'+i)]; foundKey {
for ; foundKey; _, foundKey = usedKeys[string('0'+i)] { if _, foundKey := usedKeys[fmt.Sprint(focusNum)]; foundKey {
i++ for ; foundKey; _, foundKey = usedKeys[fmt.Sprint(focusNum)] {
focusNum++
} }
} }
// Don't have nav characters > "9" // Don't allow focus characters > "9"
if i >= 10 { if focusNum >= 10 {
break break
} }
focusable.SetFocusChar(string('0' + i)) focusable.SetFocusChar(fmt.Sprint(focusNum))
i++ focusNum++
} }
} }

View File

@ -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 { func (common *Common) FocusChar() string {
if common.focusChar <= -1 { if common.focusChar <= 0 {
return "" return ""
} }
return string('0' + common.focusChar) if common.focusChar > 9 {
return ""
}
return fmt.Sprint(common.focusChar)
} }
func (common *Common) RowColor(idx int) string { func (common *Common) RowColor(idx int) string {

View File

@ -47,29 +47,36 @@ func Test_DefaultRowColor(t *testing.T) {
func Test_FocusChar(t *testing.T) { func Test_FocusChar(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
expectedChar string
before func(testCfg *Common) before func(testCfg *Common)
expectedChar string
}{ }{
{ {
name: "with no focus char specified", name: "with negative focus char",
expectedChar: "",
before: func(testCfg *Common) { before: func(testCfg *Common) {
testCfg.focusChar = -1 testCfg.focusChar = -1
}, },
expectedChar: "",
}, },
{ {
name: "with explicit focus char specified", name: "with positive focus char",
expectedChar: "3",
before: func(testCfg *Common) { before: func(testCfg *Common) {
testCfg.focusChar = 3 testCfg.focusChar = 3
}, },
expectedChar: "3",
}, },
{ {
name: "with ridiculous focus char specified", name: "with zero focus char",
expectedChar: "Q",
before: func(testCfg *Common) { before: func(testCfg *Common) {
testCfg.focusChar = 33 testCfg.focusChar = 0
}, },
expectedChar: "",
},
{
name: "with large focus char",
before: func(testCfg *Common) {
testCfg.focusChar = 10
},
expectedChar: "",
}, },
} }