jesseduffield.lazygit/pkg/gui/services/custom_commands/client.go
Stefan Haller 3d18ee8f91 Use a slice of keys for each binding
This is a pure refactor in preparation for letting users configure multiple
alternate bindings for a single command. Every Binding still has exactly one
key, so nothing changes visibly: the cheatsheet, the on-screen options bar,
and the keybindings menu all render identically.

When a Binding ends up with multiple keys, the on-screen options bar will
show only the first (to avoid clutter); the cheatsheet will show all of them (in
a later commit). For now both paths take Key[0].

MenuItem.Key is changed in the same way, it also has a slice of keys now.

In this commit we keep the name `Key` in Binding, KeybindingOpts and MenuItem,
instead of renaming them to `Keys` right away, in order to keep the diff a bit
more readable. We'll do the rename separately in the next commit.
2026-05-25 15:18:18 +02:00

120 lines
3.7 KiB
Go

package custom_commands
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/types"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/samber/lo"
)
// Client is the entry point to this package. It returns a list of keybindings based on the config's user-defined custom commands.
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_Command_Keybindings.md for more info.
type Client struct {
c *helpers.HelperCommon
handlerCreator *HandlerCreator
keybindingCreator *KeybindingCreator
}
func NewClient(
c *helpers.HelperCommon,
helpers *helpers.Helpers,
) *Client {
sessionStateLoader := NewSessionStateLoader(c, helpers.Refs)
handlerCreator := NewHandlerCreator(
c,
sessionStateLoader,
helpers.Suggestions,
helpers.MergeAndRebase,
)
keybindingCreator := NewKeybindingCreator(c)
return &Client{
c: c,
keybindingCreator: keybindingCreator,
handlerCreator: handlerCreator,
}
}
func (self *Client) GetCustomCommandKeybindings() ([]*types.Binding, error) {
bindings := []*types.Binding{}
for _, customCommand := range self.c.UserConfig().CustomCommands {
if len(customCommand.CommandMenu) > 0 {
handler := func() error {
return self.showCustomCommandsMenu(customCommand)
}
bindings = append(bindings, &types.Binding{
ViewName: "", // custom commands menus are global; we filter the commands inside by context
Key: []gocui.Key{config.GetValidatedKeyBindingKey(customCommand.Key)},
Handler: handler,
Description: getCustomCommandsMenuDescription(customCommand, self.c.Tr),
OpensMenu: true,
})
} else {
handler := self.handlerCreator.call(customCommand)
compoundBindings, err := self.keybindingCreator.call(customCommand, handler)
if err != nil {
return nil, err
}
bindings = append(bindings, compoundBindings...)
}
}
return bindings, nil
}
func (self *Client) showCustomCommandsMenu(customCommand config.CustomCommand) error {
menuItems := make([]*types.MenuItem, 0, len(customCommand.CommandMenu))
for _, subCommand := range customCommand.CommandMenu {
if len(subCommand.CommandMenu) > 0 {
handler := func() error {
return self.showCustomCommandsMenu(subCommand)
}
menuItems = append(menuItems, &types.MenuItem{
Label: subCommand.GetDescription(),
Key: []gocui.Key{config.GetValidatedKeyBindingKey(subCommand.Key)},
OnPress: handler,
OpensMenu: true,
})
} else {
if subCommand.Context != "" && subCommand.Context != "global" {
viewNames, err := self.keybindingCreator.getViewNamesAndContexts(subCommand)
if err != nil {
return err
}
currentView := self.c.GocuiGui().CurrentView()
enabled := currentView != nil && lo.Contains(viewNames, currentView.Name())
if !enabled {
continue
}
}
menuItems = append(menuItems, &types.MenuItem{
Label: subCommand.GetDescription(),
Key: []gocui.Key{config.GetValidatedKeyBindingKey(subCommand.Key)},
OnPress: self.handlerCreator.call(subCommand),
})
}
}
if len(menuItems) == 0 {
menuItems = append(menuItems, &types.MenuItem{
Label: self.c.Tr.NoApplicableCommandsInThisContext,
OnPress: func() error { return nil },
})
}
title := getCustomCommandsMenuDescription(customCommand, self.c.Tr)
return self.c.Menu(types.CreateMenuOptions{Title: title, Items: menuItems, HideCancel: true})
}
func getCustomCommandsMenuDescription(customCommand config.CustomCommand, tr *i18n.TranslationSet) string {
if customCommand.Description != "" {
return customCommand.Description
}
return tr.CustomCommands
}