From 30b619b3db76e7fe095e267f5b1f6ed655a41309 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 2 Apr 2026 10:21:30 +0200 Subject: [PATCH] Get rid of pkg/gui/keybindings package Move keybindings.GetKey to config.GetValidatedKeyBindingKey --- pkg/config/keynames.go | 10 ++++++++++ pkg/gui/gui.go | 7 +++---- pkg/gui/gui_driver.go | 6 ++++-- pkg/gui/keybindings.go | 8 ++++---- pkg/gui/keybindings/keybindings.go | 17 ----------------- pkg/gui/menu_panel.go | 10 +++++----- pkg/gui/services/custom_commands/client.go | 7 +++---- .../services/custom_commands/handler_creator.go | 3 +-- .../custom_commands/keybinding_creator.go | 3 +-- 9 files changed, 31 insertions(+), 40 deletions(-) delete mode 100644 pkg/gui/keybindings/keybindings.go diff --git a/pkg/config/keynames.go b/pkg/config/keynames.go index f96fa68ba..a6fe9e74e 100644 --- a/pkg/config/keynames.go +++ b/pkg/config/keynames.go @@ -1,6 +1,7 @@ package config import ( + "log" "strings" "unicode/utf8" @@ -112,3 +113,12 @@ func isValidKeybindingKey(key string) bool { _, ok := KeyFromLabel(key) return ok } + +func GetValidatedKeyBindingKey(label string) gocui.Key { + key, ok := KeyFromLabel(label) + if !ok { + log.Fatalf("Unrecognized key %s, this should have been caught by user config validation", label) + } + + return key +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index d74c8e2a4..cf024a728 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -26,7 +26,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking" "github.com/jesseduffield/lazygit/pkg/gui/modes/diffing" "github.com/jesseduffield/lazygit/pkg/gui/modes/filtering" @@ -471,9 +470,9 @@ func (gui *Gui) onUserConfigLoaded() error { gui.setColorScheme() gui.configureViewProperties() - gui.g.SearchEscapeKey = keybindings.GetKey(userConfig.Keybinding.Universal.Return) - gui.g.NextSearchMatchKey = keybindings.GetKey(userConfig.Keybinding.Universal.NextMatch) - gui.g.PrevSearchMatchKey = keybindings.GetKey(userConfig.Keybinding.Universal.PrevMatch) + gui.g.SearchEscapeKey = config.GetValidatedKeyBindingKey(userConfig.Keybinding.Universal.Return) + gui.g.NextSearchMatchKey = config.GetValidatedKeyBindingKey(userConfig.Keybinding.Universal.NextMatch) + gui.g.PrevSearchMatchKey = config.GetValidatedKeyBindingKey(userConfig.Keybinding.Universal.PrevMatch) gui.g.ShowListFooter = userConfig.Gui.ShowListFooter diff --git a/pkg/gui/gui_driver.go b/pkg/gui/gui_driver.go index 18f59152b..f70e8c9b3 100644 --- a/pkg/gui/gui_driver.go +++ b/pkg/gui/gui_driver.go @@ -10,7 +10,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" ) @@ -29,7 +28,10 @@ var _ integrationTypes.GuiDriver = &GuiDriver{} func (self *GuiDriver) PressKey(keyStr string) { self.CheckAllToastsAcknowledged() - key := keybindings.GetKey(keyStr) + key, ok := config.KeyFromLabel(keyStr) + if !ok { + self.Fail("Unrecognized key: " + keyStr) + } self.gui.g.ReplayedEvents.Keys <- gocui.NewTcellKeyEventWrapper( tcell.NewEventKey(tcell.Key(key.KeyName()), key.Str(), tcell.ModNone), diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 7780f74b0..74e68d818 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -4,10 +4,10 @@ import ( "errors" "log" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -61,7 +61,7 @@ func (gui *Gui) GetCheatsheetKeybindings() []*types.Binding { } func (gui *Gui) keybindingOpts() types.KeybindingsOpts { - config := gui.c.UserConfig().Keybinding + keybindingConfig := gui.c.UserConfig().Keybinding guards := types.KeybindingGuards{ OutsideFilterMode: gui.outsideFilterMode, @@ -69,8 +69,8 @@ func (gui *Gui) keybindingOpts() types.KeybindingsOpts { } return types.KeybindingsOpts{ - GetKey: keybindings.GetKey, - Config: config, + GetKey: config.GetValidatedKeyBindingKey, + Config: keybindingConfig, Guards: guards, } } diff --git a/pkg/gui/keybindings/keybindings.go b/pkg/gui/keybindings/keybindings.go deleted file mode 100644 index 977e00f33..000000000 --- a/pkg/gui/keybindings/keybindings.go +++ /dev/null @@ -1,17 +0,0 @@ -package keybindings - -import ( - "log" - - "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/gocui" -) - -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) - } - - return key -} diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index e410f934f..248da40fb 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -3,8 +3,8 @@ package gui import ( "fmt" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/samber/lo" @@ -28,10 +28,10 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error { maxColumnSize := 1 essentialKeys := []gocui.Key{ - keybindings.GetKey(gui.c.UserConfig().Keybinding.Universal.ConfirmMenu), - keybindings.GetKey(gui.c.UserConfig().Keybinding.Universal.Return), - keybindings.GetKey(gui.c.UserConfig().Keybinding.Universal.PrevItem), - keybindings.GetKey(gui.c.UserConfig().Keybinding.Universal.NextItem), + config.GetValidatedKeyBindingKey(gui.c.UserConfig().Keybinding.Universal.ConfirmMenu), + config.GetValidatedKeyBindingKey(gui.c.UserConfig().Keybinding.Universal.Return), + config.GetValidatedKeyBindingKey(gui.c.UserConfig().Keybinding.Universal.PrevItem), + config.GetValidatedKeyBindingKey(gui.c.UserConfig().Keybinding.Universal.NextItem), } for _, item := range opts.Items { diff --git a/pkg/gui/services/custom_commands/client.go b/pkg/gui/services/custom_commands/client.go index aceaef2dd..d1479d7c8 100644 --- a/pkg/gui/services/custom_commands/client.go +++ b/pkg/gui/services/custom_commands/client.go @@ -4,7 +4,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/samber/lo" @@ -47,7 +46,7 @@ func (self *Client) GetCustomCommandKeybindings() ([]*types.Binding, error) { } bindings = append(bindings, &types.Binding{ ViewName: "", // custom commands menus are global; we filter the commands inside by context - Key: keybindings.GetKey(customCommand.Key), + Key: config.GetValidatedKeyBindingKey(customCommand.Key), Modifier: gocui.ModNone, Handler: handler, Description: getCustomCommandsMenuDescription(customCommand, self.c.Tr), @@ -75,7 +74,7 @@ func (self *Client) showCustomCommandsMenu(customCommand config.CustomCommand) e } menuItems = append(menuItems, &types.MenuItem{ Label: subCommand.GetDescription(), - Key: keybindings.GetKey(subCommand.Key), + Key: config.GetValidatedKeyBindingKey(subCommand.Key), OnPress: handler, OpensMenu: true, }) @@ -95,7 +94,7 @@ func (self *Client) showCustomCommandsMenu(customCommand config.CustomCommand) e menuItems = append(menuItems, &types.MenuItem{ Label: subCommand.GetDescription(), - Key: keybindings.GetKey(subCommand.Key), + Key: config.GetValidatedKeyBindingKey(subCommand.Key), OnPress: self.handlerCreator.call(subCommand), }) } diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index 816435588..412fc7de6 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -9,7 +9,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" @@ -233,7 +232,7 @@ func (self *HandlerCreator) menuPrompt(prompt *config.CustomCommandPrompt, wrapp OnPress: func() error { return wrappedF(option.Value) }, - Key: keybindings.GetKey(option.Key), + Key: config.GetValidatedKeyBindingKey(option.Key), } }) diff --git a/pkg/gui/services/custom_commands/keybinding_creator.go b/pkg/gui/services/custom_commands/keybinding_creator.go index d7acbcacc..a89beb287 100644 --- a/pkg/gui/services/custom_commands/keybinding_creator.go +++ b/pkg/gui/services/custom_commands/keybinding_creator.go @@ -8,7 +8,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" - "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/samber/lo" ) @@ -37,7 +36,7 @@ func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler return lo.Map(viewNames, func(viewName string, _ int) *types.Binding { return &types.Binding{ ViewName: viewName, - Key: keybindings.GetKey(customCommand.Key), + Key: config.GetValidatedKeyBindingKey(customCommand.Key), Modifier: gocui.ModNone, Handler: handler, Description: customCommand.GetDescription(),