Skip to content

Commit 2ed1133

Browse files
committed
Allow filtering for keybindings by prepending filter string with '@'
1 parent d52d5e6 commit 2ed1133

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed

pkg/gui/context/menu_context.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package context
22

33
import (
44
"errors"
5+
"strings"
56

67
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
78
"github.com/jesseduffield/lazygit/pkg/gui/style"
@@ -48,11 +49,12 @@ func NewMenuContext(
4849
}
4950

5051
type MenuViewModel struct {
51-
c *ContextCommon
52-
menuItems []*types.MenuItem
53-
prompt string
54-
promptLines []string
55-
columnAlignment []utils.Alignment
52+
c *ContextCommon
53+
menuItems []*types.MenuItem
54+
prompt string
55+
promptLines []string
56+
columnAlignment []utils.Alignment
57+
allowFilteringKeybindings bool
5658
*FilteredListViewModel[*types.MenuItem]
5759
}
5860

@@ -62,11 +64,29 @@ func NewMenuViewModel(c *ContextCommon) *MenuViewModel {
6264
c: c,
6365
}
6466

67+
filterKeybindings := false
68+
6569
self.FilteredListViewModel = NewFilteredListViewModel(
6670
func() []*types.MenuItem { return self.menuItems },
67-
func(item *types.MenuItem) []string { return item.LabelColumns },
71+
func(item *types.MenuItem) []string {
72+
if filterKeybindings {
73+
return []string{keybindings.LabelFromKey(item.Key)}
74+
}
75+
76+
return item.LabelColumns
77+
},
6878
)
6979

80+
self.FilteredListViewModel.SetPreprocessFilterFunc(func(filter string) string {
81+
if self.allowFilteringKeybindings && strings.HasPrefix(filter, "@") {
82+
filterKeybindings = true
83+
return filter[1:]
84+
}
85+
86+
filterKeybindings = false
87+
return filter
88+
})
89+
7090
return self
7191
}
7292

@@ -92,6 +112,10 @@ func (self *MenuViewModel) SetPromptLines(promptLines []string) {
92112
self.promptLines = promptLines
93113
}
94114

115+
func (self *MenuViewModel) SetAllowFilteringKeybindings(allow bool) {
116+
self.allowFilteringKeybindings = allow
117+
}
118+
95119
// TODO: move into presentation package
96120
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
97121
menuItems := self.FilteredListViewModel.GetItems()

pkg/gui/controllers/options_menu_action.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ func (self *OptionsMenuAction) Call() error {
4646
appendBindings(navigation, &types.MenuSection{Title: self.c.Tr.KeybindingsMenuSectionNavigation, Column: 1})
4747

4848
return self.c.Menu(types.CreateMenuOptions{
49-
Title: self.c.Tr.Keybindings,
50-
Items: menuItems,
51-
HideCancel: true,
52-
ColumnAlignment: []utils.Alignment{utils.AlignRight, utils.AlignLeft},
49+
Title: self.c.Tr.Keybindings,
50+
Items: menuItems,
51+
HideCancel: true,
52+
ColumnAlignment: []utils.Alignment{utils.AlignRight, utils.AlignLeft},
53+
AllowFilteringKeybindings: true,
5354
})
5455
}
5556

pkg/gui/menu_panel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
4343

4444
gui.State.Contexts.Menu.SetMenuItems(opts.Items, opts.ColumnAlignment)
4545
gui.State.Contexts.Menu.SetPrompt(opts.Prompt)
46+
gui.State.Contexts.Menu.SetAllowFilteringKeybindings(opts.AllowFilteringKeybindings)
4647
gui.State.Contexts.Menu.SetSelection(0)
4748

4849
gui.Views.Menu.Title = opts.Title

pkg/gui/types/common.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,12 @@ const (
147147
)
148148

149149
type CreateMenuOptions struct {
150-
Title string
151-
Prompt string // a message that will be displayed above the menu options
152-
Items []*MenuItem
153-
HideCancel bool
154-
ColumnAlignment []utils.Alignment
150+
Title string
151+
Prompt string // a message that will be displayed above the menu options
152+
Items []*MenuItem
153+
HideCancel bool
154+
ColumnAlignment []utils.Alignment
155+
AllowFilteringKeybindings bool
155156
}
156157

157158
type CreatePopupPanelOpts struct {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package filter_and_search
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var FilterMenuByKeybinding = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Filtering the keybindings menu by keybinding",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
},
15+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
16+
t.Views().Files().
17+
Press(keys.Universal.OptionMenu).
18+
Tap(func() {
19+
t.ExpectPopup().Menu().
20+
Title(Equals("Keybindings")).
21+
Filter("@+").
22+
Lines(
23+
// menu has filtered down to the one item that matches the filter
24+
Contains("--- Global ---"),
25+
Contains("+ Next screen mode").IsSelected(),
26+
).
27+
Confirm()
28+
}).
29+
30+
// Upon opening the menu again, the filter should have been reset
31+
Press(keys.Universal.OptionMenu).
32+
Tap(func() {
33+
t.ExpectPopup().Menu().
34+
Title(Equals("Keybindings")).
35+
LineCount(GreaterThan(1))
36+
})
37+
},
38+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ var tests = []*components.IntegrationTest{
220220
filter_and_search.FilterFiles,
221221
filter_and_search.FilterFuzzy,
222222
filter_and_search.FilterMenu,
223+
filter_and_search.FilterMenuByKeybinding,
223224
filter_and_search.FilterMenuCancelFilterWithEscape,
224225
filter_and_search.FilterMenuWithNoKeybindings,
225226
filter_and_search.FilterRemoteBranches,

0 commit comments

Comments
 (0)