Add config.KeyFromLabel

This commit is contained in:
Stefan Haller 2026-04-02 09:58:07 +02:00
parent a5b760f8d1
commit c455fca062
2 changed files with 20 additions and 27 deletions

View file

@ -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 == "<disabled>" {
return true
func KeyFromLabel(label string) (gocui.Key, bool) {
if label == "" || label == "<disabled>" {
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
}

View file

@ -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 == "<disabled>" {
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
}