From e1fc90615d75913c21eb0050e53d4452f9a3b0d0 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 21 May 2023 11:02:36 +1000 Subject: Apply strikethrough style to reserved keybindings in menus If a given menu item has an associated keybinding of 'enter', hitting enter won't actually execute that item unless your cursor is on it. This creates confusion, and so we're going to use a strikethrough style to communicate that the keybinding is reserved for something else. --- pkg/gui/context/menu_context.go | 30 ++++++++++++++++++++++++++---- pkg/gui/controllers/menu_controller.go | 2 ++ 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'pkg') diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go index b2685b2e4..667ea228b 100644 --- a/pkg/gui/context/menu_context.go +++ b/pkg/gui/context/menu_context.go @@ -5,9 +5,12 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) type MenuContext struct { + c *ContextCommon + *MenuViewModel *ListContextTrait } @@ -17,9 +20,10 @@ var _ types.IListContext = (*MenuContext)(nil) func NewMenuContext( c *ContextCommon, ) *MenuContext { - viewModel := NewMenuViewModel() + viewModel := NewMenuViewModel(c) return &MenuContext{ + c: c, MenuViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ @@ -48,13 +52,15 @@ func (self *MenuContext) GetSelectedItemId() string { } type MenuViewModel struct { + c *ContextCommon menuItems []*types.MenuItem *BasicViewModel[*types.MenuItem] } -func NewMenuViewModel() *MenuViewModel { +func NewMenuViewModel(c *ContextCommon) *MenuViewModel { self := &MenuViewModel{ menuItems: nil, + c: c, } self.BasicViewModel = NewBasicViewModel(func() []*types.MenuItem { return self.menuItems }) @@ -74,9 +80,25 @@ func (self *MenuViewModel) GetDisplayStrings(_startIdx int, _length int) [][]str return slices.Map(self.menuItems, func(item *types.MenuItem) []string { displayStrings := item.LabelColumns - if showKeys { - displayStrings = slices.Prepend(displayStrings, style.FgCyan.Sprint(keybindings.LabelFromKey(item.Key))) + + if !showKeys { + return displayStrings + } + + // These keys are used for general navigation so we'll strike them out to + // avoid confusion + reservedKeys := []string{ + self.c.UserConfig.Keybinding.Universal.Confirm, + self.c.UserConfig.Keybinding.Universal.Select, + self.c.UserConfig.Keybinding.Universal.Return, + } + keyLabel := keybindings.LabelFromKey(item.Key) + keyStyle := style.FgCyan + if lo.Contains(reservedKeys, keyLabel) { + keyStyle = style.FgDefault.SetStrikethrough() } + + displayStrings = slices.Prepend(displayStrings, keyStyle.Sprint(keyLabel)) return displayStrings }) } diff --git a/pkg/gui/controllers/menu_controller.go b/pkg/gui/controllers/menu_controller.go index b687778ac..58b0fcdd5 100644 --- a/pkg/gui/controllers/menu_controller.go +++ b/pkg/gui/controllers/menu_controller.go @@ -21,6 +21,8 @@ func NewMenuController( } } +// NOTE: if you add a new keybinding here, you'll also need to add it to +// `reservedKeys` in `pkg/gui/context/menu_context.go` func (self *MenuController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { bindings := []*types.Binding{ { -- cgit v1.2.3