Get rid of pkg/gui/keybindings package

Move keybindings.GetKey to config.GetValidatedKeyBindingKey
This commit is contained in:
Stefan Haller 2026-04-02 10:21:30 +02:00
parent c41fae9fc4
commit 30b619b3db
9 changed files with 31 additions and 40 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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),

View file

@ -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,
}
}

View file

@ -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
}

View file

@ -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 {

View file

@ -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),
})
}

View file

@ -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),
}
})

View file

@ -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(),