diff --git a/pkg/config/keynames.go b/pkg/config/keynames.go index 075e8006e..2f88908ec 100644 --- a/pkg/config/keynames.go +++ b/pkg/config/keynames.go @@ -73,16 +73,24 @@ var LabelByKey = map[gocui.KeyName]string{ var KeyByLabel = lo.Invert(LabelByKey) -func isValidKeybindingKey(key string) bool { - runeCount := utf8.RuneCountInString(key) - if key == "" { - return true +func KeyFromLabel(label string) (gocui.Key, bool) { + if label == "" || label == "" { + return gocui.Key{}, true } + runeCount := utf8.RuneCountInString(label) if runeCount > 1 { - _, ok := KeyByLabel[strings.ToLower(key)] - return ok + keyName, ok := KeyByLabel[strings.ToLower(label)] + if !ok { + return gocui.Key{}, false + } + return gocui.NewKeyName(keyName), true } - return true + return gocui.NewKeyRune([]rune(label)[0]), true +} + +func isValidKeybindingKey(key string) bool { + _, ok := KeyFromLabel(key) + return ok } diff --git a/pkg/gui/keybindings/keybindings.go b/pkg/gui/keybindings/keybindings.go index 4b67e23fe..9ed420f3b 100644 --- a/pkg/gui/keybindings/keybindings.go +++ b/pkg/gui/keybindings/keybindings.go @@ -2,12 +2,9 @@ package keybindings import ( "log" - "strings" - "unicode/utf8" "github.com/gdamore/tcell/v3" "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/gocui" ) @@ -28,23 +25,11 @@ func LabelFromKey(key gocui.Key) string { return "unknown" } -func GetKey(key string) gocui.Key { - if key == "" { - return gocui.Key{} +func GetKey(label string) gocui.Key { + key, ok := config.KeyFromLabel(label) + if !ok { + log.Fatalf("Unrecognized key %s, this should have been caught by user config validation", label) } - runeCount := utf8.RuneCountInString(key) - if runeCount > 1 { - keyName, ok := config.KeyByLabel[strings.ToLower(key)] - if !ok { - log.Fatalf("Unrecognized key %s for keybinding. For permitted values see %s", strings.ToLower(key), constants.Links.Docs.CustomKeybindings) - } - return gocui.NewKeyName(keyName) - } - - if runeCount == 1 { - return gocui.NewKeyRune([]rune(key)[0]) - } - - return gocui.Key{} + return key }