mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-24 19:39:16 +02:00
Allow filtering for keybindings by prepending filter string with '@'
This commit is contained in:
@@ -2,6 +2,7 @@ package context
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
||||
@@ -53,6 +54,7 @@ type MenuViewModel struct {
|
||||
prompt string
|
||||
promptLines []string
|
||||
columnAlignment []utils.Alignment
|
||||
allowFilteringKeybindings bool
|
||||
*FilteredListViewModel[*types.MenuItem]
|
||||
}
|
||||
|
||||
@@ -62,11 +64,29 @@ func NewMenuViewModel(c *ContextCommon) *MenuViewModel {
|
||||
c: c,
|
||||
}
|
||||
|
||||
filterKeybindings := false
|
||||
|
||||
self.FilteredListViewModel = NewFilteredListViewModel(
|
||||
func() []*types.MenuItem { return self.menuItems },
|
||||
func(item *types.MenuItem) []string { return item.LabelColumns },
|
||||
func(item *types.MenuItem) []string {
|
||||
if filterKeybindings {
|
||||
return []string{keybindings.LabelFromKey(item.Key)}
|
||||
}
|
||||
|
||||
return item.LabelColumns
|
||||
},
|
||||
)
|
||||
|
||||
self.FilteredListViewModel.SetPreprocessFilterFunc(func(filter string) string {
|
||||
if self.allowFilteringKeybindings && strings.HasPrefix(filter, "@") {
|
||||
filterKeybindings = true
|
||||
return filter[1:]
|
||||
}
|
||||
|
||||
filterKeybindings = false
|
||||
return filter
|
||||
})
|
||||
|
||||
return self
|
||||
}
|
||||
|
||||
@@ -92,6 +112,10 @@ func (self *MenuViewModel) SetPromptLines(promptLines []string) {
|
||||
self.promptLines = promptLines
|
||||
}
|
||||
|
||||
func (self *MenuViewModel) SetAllowFilteringKeybindings(allow bool) {
|
||||
self.allowFilteringKeybindings = allow
|
||||
}
|
||||
|
||||
// TODO: move into presentation package
|
||||
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
|
||||
menuItems := self.FilteredListViewModel.GetItems()
|
||||
|
@@ -50,6 +50,7 @@ func (self *OptionsMenuAction) Call() error {
|
||||
Items: menuItems,
|
||||
HideCancel: true,
|
||||
ColumnAlignment: []utils.Alignment{utils.AlignRight, utils.AlignLeft},
|
||||
AllowFilteringKeybindings: true,
|
||||
})
|
||||
}
|
||||
|
||||
|
@@ -43,6 +43,7 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
|
||||
|
||||
gui.State.Contexts.Menu.SetMenuItems(opts.Items, opts.ColumnAlignment)
|
||||
gui.State.Contexts.Menu.SetPrompt(opts.Prompt)
|
||||
gui.State.Contexts.Menu.SetAllowFilteringKeybindings(opts.AllowFilteringKeybindings)
|
||||
gui.State.Contexts.Menu.SetSelection(0)
|
||||
|
||||
gui.Views.Menu.Title = opts.Title
|
||||
|
@@ -152,6 +152,7 @@ type CreateMenuOptions struct {
|
||||
Items []*MenuItem
|
||||
HideCancel bool
|
||||
ColumnAlignment []utils.Alignment
|
||||
AllowFilteringKeybindings bool
|
||||
}
|
||||
|
||||
type CreatePopupPanelOpts struct {
|
||||
|
@@ -0,0 +1,38 @@
|
||||
package filter_and_search
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||
)
|
||||
|
||||
var FilterMenuByKeybinding = NewIntegrationTest(NewIntegrationTestArgs{
|
||||
Description: "Filtering the keybindings menu by keybinding",
|
||||
ExtraCmdArgs: []string{},
|
||||
Skip: false,
|
||||
SetupConfig: func(config *config.AppConfig) {},
|
||||
SetupRepo: func(shell *Shell) {
|
||||
},
|
||||
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||
t.Views().Files().
|
||||
Press(keys.Universal.OptionMenu).
|
||||
Tap(func() {
|
||||
t.ExpectPopup().Menu().
|
||||
Title(Equals("Keybindings")).
|
||||
Filter("@+").
|
||||
Lines(
|
||||
// menu has filtered down to the one item that matches the filter
|
||||
Contains("--- Global ---"),
|
||||
Contains("+ Next screen mode").IsSelected(),
|
||||
).
|
||||
Confirm()
|
||||
}).
|
||||
|
||||
// Upon opening the menu again, the filter should have been reset
|
||||
Press(keys.Universal.OptionMenu).
|
||||
Tap(func() {
|
||||
t.ExpectPopup().Menu().
|
||||
Title(Equals("Keybindings")).
|
||||
LineCount(GreaterThan(1))
|
||||
})
|
||||
},
|
||||
})
|
@@ -220,6 +220,7 @@ var tests = []*components.IntegrationTest{
|
||||
filter_and_search.FilterFiles,
|
||||
filter_and_search.FilterFuzzy,
|
||||
filter_and_search.FilterMenu,
|
||||
filter_and_search.FilterMenuByKeybinding,
|
||||
filter_and_search.FilterMenuCancelFilterWithEscape,
|
||||
filter_and_search.FilterMenuWithNoKeybindings,
|
||||
filter_and_search.FilterRemoteBranches,
|
||||
|
Reference in New Issue
Block a user