diff options
Diffstat (limited to 'pkg/gui/keybindings/keybindings.go')
-rw-r--r-- | pkg/gui/keybindings/keybindings.go | 201 |
1 files changed, 66 insertions, 135 deletions
diff --git a/pkg/gui/keybindings/keybindings.go b/pkg/gui/keybindings/keybindings.go index 3099d880c..4204dc4e1 100644 --- a/pkg/gui/keybindings/keybindings.go +++ b/pkg/gui/keybindings/keybindings.go @@ -9,142 +9,73 @@ import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) -var keyMapReversed = map[gocui.Key]string{ - gocui.KeyF1: "f1", - gocui.KeyF2: "f2", - gocui.KeyF3: "f3", - gocui.KeyF4: "f4", - gocui.KeyF5: "f5", - gocui.KeyF6: "f6", - gocui.KeyF7: "f7", - gocui.KeyF8: "f8", - gocui.KeyF9: "f9", - gocui.KeyF10: "f10", - gocui.KeyF11: "f11", - gocui.KeyF12: "f12", - gocui.KeyInsert: "insert", - gocui.KeyDelete: "delete", - gocui.KeyHome: "home", - gocui.KeyEnd: "end", - gocui.KeyPgup: "pgup", - gocui.KeyPgdn: "pgdown", - gocui.KeyArrowUp: "▲", - gocui.KeyArrowDown: "▼", - gocui.KeyArrowLeft: "◀", - gocui.KeyArrowRight: "▶", - gocui.KeyTab: "tab", // ctrl+i - gocui.KeyBacktab: "shift+tab", - gocui.KeyEnter: "enter", // ctrl+m - gocui.KeyAltEnter: "alt+enter", - gocui.KeyEsc: "esc", // ctrl+[, ctrl+3 - gocui.KeyBackspace: "backspace", // ctrl+h - gocui.KeyCtrlSpace: "ctrl+space", // ctrl+~, ctrl+2 - gocui.KeyCtrlSlash: "ctrl+/", // ctrl+_ - gocui.KeySpace: "space", - gocui.KeyCtrlA: "ctrl+a", - gocui.KeyCtrlB: "ctrl+b", - gocui.KeyCtrlC: "ctrl+c", - gocui.KeyCtrlD: "ctrl+d", - gocui.KeyCtrlE: "ctrl+e", - gocui.KeyCtrlF: "ctrl+f", - gocui.KeyCtrlG: "ctrl+g", - gocui.KeyCtrlJ: "ctrl+j", - gocui.KeyCtrlK: "ctrl+k", - gocui.KeyCtrlL: "ctrl+l", - gocui.KeyCtrlN: "ctrl+n", - gocui.KeyCtrlO: "ctrl+o", - gocui.KeyCtrlP: "ctrl+p", - gocui.KeyCtrlQ: "ctrl+q", - gocui.KeyCtrlR: "ctrl+r", - gocui.KeyCtrlS: "ctrl+s", - gocui.KeyCtrlT: "ctrl+t", - gocui.KeyCtrlU: "ctrl+u", - gocui.KeyCtrlV: "ctrl+v", - gocui.KeyCtrlW: "ctrl+w", - gocui.KeyCtrlX: "ctrl+x", - gocui.KeyCtrlY: "ctrl+y", - gocui.KeyCtrlZ: "ctrl+z", - gocui.KeyCtrl4: "ctrl+4", // ctrl+\ - gocui.KeyCtrl5: "ctrl+5", // ctrl+] - gocui.KeyCtrl6: "ctrl+6", - gocui.KeyCtrl8: "ctrl+8", - gocui.MouseWheelUp: "mouse wheel ▲", - gocui.MouseWheelDown: "mouse wheel ▼", +var labelByKey = map[gocui.Key]string{ + gocui.KeyF1: "<f1>", + gocui.KeyF2: "<f2>", + gocui.KeyF3: "<f3>", + gocui.KeyF4: "<f4>", + gocui.KeyF5: "<f5>", + gocui.KeyF6: "<f6>", + gocui.KeyF7: "<f7>", + gocui.KeyF8: "<f8>", + gocui.KeyF9: "<f9>", + gocui.KeyF10: "<f10>", + gocui.KeyF11: "<f11>", + gocui.KeyF12: "<f12>", + gocui.KeyInsert: "<insert>", + gocui.KeyDelete: "<delete>", + gocui.KeyHome: "<home>", + gocui.KeyEnd: "<end>", + gocui.KeyPgup: "<pgup>", + gocui.KeyPgdn: "<pgdown>", + gocui.KeyArrowUp: "<up>", + gocui.KeyArrowDown: "<down>", + gocui.KeyArrowLeft: "<left>", + gocui.KeyArrowRight: "<right>", + gocui.KeyTab: "<tab>", // <c-i> + gocui.KeyBacktab: "<backtab>", + gocui.KeyEnter: "<enter>", // <c-m> + gocui.KeyAltEnter: "<a-enter>", + gocui.KeyEsc: "<esc>", // <c-[>, <c-3> + gocui.KeyBackspace: "<backspace>", // <c-h> + gocui.KeyCtrlSpace: "<c-space>", // <c-~>, <c-2> + gocui.KeyCtrlSlash: "<c-/>", // <c-_> + gocui.KeySpace: "<space>", + gocui.KeyCtrlA: "<c-a>", + gocui.KeyCtrlB: "<c-b>", + gocui.KeyCtrlC: "<c-c>", + gocui.KeyCtrlD: "<c-d>", + gocui.KeyCtrlE: "<c-e>", + gocui.KeyCtrlF: "<c-f>", + gocui.KeyCtrlG: "<c-g>", + gocui.KeyCtrlJ: "<c-j>", + gocui.KeyCtrlK: "<c-k>", + gocui.KeyCtrlL: "<c-l>", + gocui.KeyCtrlN: "<c-n>", + gocui.KeyCtrlO: "<c-o>", + gocui.KeyCtrlP: "<c-p>", + gocui.KeyCtrlQ: "<c-q>", + gocui.KeyCtrlR: "<c-r>", + gocui.KeyCtrlS: "<c-s>", + gocui.KeyCtrlT: "<c-t>", + gocui.KeyCtrlU: "<c-u>", + gocui.KeyCtrlV: "<c-v>", + gocui.KeyCtrlW: "<c-w>", + gocui.KeyCtrlX: "<c-x>", + gocui.KeyCtrlY: "<c-y>", + gocui.KeyCtrlZ: "<c-z>", + gocui.KeyCtrl4: "<c-4>", // <c-\> + gocui.KeyCtrl5: "<c-5>", // <c-]> + gocui.KeyCtrl6: "<c-6>", + gocui.KeyCtrl8: "<c-8>", + gocui.MouseWheelUp: "mouse wheel up", + gocui.MouseWheelDown: "mouse wheel down", } -var keyMap = map[string]types.Key{ - "<c-a>": gocui.KeyCtrlA, - "<c-b>": gocui.KeyCtrlB, - "<c-c>": gocui.KeyCtrlC, - "<c-d>": gocui.KeyCtrlD, - "<c-e>": gocui.KeyCtrlE, - "<c-f>": gocui.KeyCtrlF, - "<c-g>": gocui.KeyCtrlG, - "<c-h>": gocui.KeyCtrlH, - "<c-i>": gocui.KeyCtrlI, - "<c-j>": gocui.KeyCtrlJ, - "<c-k>": gocui.KeyCtrlK, - "<c-l>": gocui.KeyCtrlL, - "<c-m>": gocui.KeyCtrlM, - "<c-n>": gocui.KeyCtrlN, - "<c-o>": gocui.KeyCtrlO, - "<c-p>": gocui.KeyCtrlP, - "<c-q>": gocui.KeyCtrlQ, - "<c-r>": gocui.KeyCtrlR, - "<c-s>": gocui.KeyCtrlS, - "<c-t>": gocui.KeyCtrlT, - "<c-u>": gocui.KeyCtrlU, - "<c-v>": gocui.KeyCtrlV, - "<c-w>": gocui.KeyCtrlW, - "<c-x>": gocui.KeyCtrlX, - "<c-y>": gocui.KeyCtrlY, - "<c-z>": gocui.KeyCtrlZ, - "<c-~>": gocui.KeyCtrlTilde, - "<c-2>": gocui.KeyCtrl2, - "<c-3>": gocui.KeyCtrl3, - "<c-4>": gocui.KeyCtrl4, - "<c-5>": gocui.KeyCtrl5, - "<c-6>": gocui.KeyCtrl6, - "<c-7>": gocui.KeyCtrl7, - "<c-8>": gocui.KeyCtrl8, - "<c-space>": gocui.KeyCtrlSpace, - "<c-\\>": gocui.KeyCtrlBackslash, - "<c-[>": gocui.KeyCtrlLsqBracket, - "<c-]>": gocui.KeyCtrlRsqBracket, - "<c-/>": gocui.KeyCtrlSlash, - "<c-_>": gocui.KeyCtrlUnderscore, - "<backspace>": gocui.KeyBackspace, - "<tab>": gocui.KeyTab, - "<backtab>": gocui.KeyBacktab, - "<enter>": gocui.KeyEnter, - "<a-enter>": gocui.KeyAltEnter, - "<esc>": gocui.KeyEsc, - "<space>": gocui.KeySpace, - "<f1>": gocui.KeyF1, - "<f2>": gocui.KeyF2, - "<f3>": gocui.KeyF3, - "<f4>": gocui.KeyF4, - "<f5>": gocui.KeyF5, - "<f6>": gocui.KeyF6, - "<f7>": gocui.KeyF7, - "<f8>": gocui.KeyF8, - "<f9>": gocui.KeyF9, - "<f10>": gocui.KeyF10, - "<f11>": gocui.KeyF11, - "<f12>": gocui.KeyF12, - "<insert>": gocui.KeyInsert, - "<delete>": gocui.KeyDelete, - "<home>": gocui.KeyHome, - "<end>": gocui.KeyEnd, - "<pgup>": gocui.KeyPgup, - "<pgdown>": gocui.KeyPgdn, - "<up>": gocui.KeyArrowUp, - "<down>": gocui.KeyArrowDown, - "<left>": gocui.KeyArrowLeft, - "<right>": gocui.KeyArrowRight, -} +var keyByLabel = lo.Invert(labelByKey) func Label(name string) string { return LabelFromKey(GetKey(name)) @@ -157,7 +88,7 @@ func LabelFromKey(key types.Key) string { case rune: keyInt = int(key) case gocui.Key: - value, ok := keyMapReversed[key] + value, ok := labelByKey[key] if ok { return value } @@ -170,8 +101,8 @@ func LabelFromKey(key types.Key) string { func GetKey(key string) types.Key { runeCount := utf8.RuneCountInString(key) if runeCount > 1 { - binding := keyMap[strings.ToLower(key)] - if binding == nil { + binding, ok := 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) } else { return binding |