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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
||||||
@@ -48,11 +49,12 @@ func NewMenuContext(
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MenuViewModel struct {
|
type MenuViewModel struct {
|
||||||
c *ContextCommon
|
c *ContextCommon
|
||||||
menuItems []*types.MenuItem
|
menuItems []*types.MenuItem
|
||||||
prompt string
|
prompt string
|
||||||
promptLines []string
|
promptLines []string
|
||||||
columnAlignment []utils.Alignment
|
columnAlignment []utils.Alignment
|
||||||
|
allowFilteringKeybindings bool
|
||||||
*FilteredListViewModel[*types.MenuItem]
|
*FilteredListViewModel[*types.MenuItem]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,11 +64,29 @@ func NewMenuViewModel(c *ContextCommon) *MenuViewModel {
|
|||||||
c: c,
|
c: c,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filterKeybindings := false
|
||||||
|
|
||||||
self.FilteredListViewModel = NewFilteredListViewModel(
|
self.FilteredListViewModel = NewFilteredListViewModel(
|
||||||
func() []*types.MenuItem { return self.menuItems },
|
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
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,6 +112,10 @@ func (self *MenuViewModel) SetPromptLines(promptLines []string) {
|
|||||||
self.promptLines = promptLines
|
self.promptLines = promptLines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *MenuViewModel) SetAllowFilteringKeybindings(allow bool) {
|
||||||
|
self.allowFilteringKeybindings = allow
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: move into presentation package
|
// TODO: move into presentation package
|
||||||
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
|
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
|
||||||
menuItems := self.FilteredListViewModel.GetItems()
|
menuItems := self.FilteredListViewModel.GetItems()
|
||||||
|
@@ -46,10 +46,11 @@ func (self *OptionsMenuAction) Call() error {
|
|||||||
appendBindings(navigation, &types.MenuSection{Title: self.c.Tr.KeybindingsMenuSectionNavigation, Column: 1})
|
appendBindings(navigation, &types.MenuSection{Title: self.c.Tr.KeybindingsMenuSectionNavigation, Column: 1})
|
||||||
|
|
||||||
return self.c.Menu(types.CreateMenuOptions{
|
return self.c.Menu(types.CreateMenuOptions{
|
||||||
Title: self.c.Tr.Keybindings,
|
Title: self.c.Tr.Keybindings,
|
||||||
Items: menuItems,
|
Items: menuItems,
|
||||||
HideCancel: true,
|
HideCancel: true,
|
||||||
ColumnAlignment: []utils.Alignment{utils.AlignRight, utils.AlignLeft},
|
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.SetMenuItems(opts.Items, opts.ColumnAlignment)
|
||||||
gui.State.Contexts.Menu.SetPrompt(opts.Prompt)
|
gui.State.Contexts.Menu.SetPrompt(opts.Prompt)
|
||||||
|
gui.State.Contexts.Menu.SetAllowFilteringKeybindings(opts.AllowFilteringKeybindings)
|
||||||
gui.State.Contexts.Menu.SetSelection(0)
|
gui.State.Contexts.Menu.SetSelection(0)
|
||||||
|
|
||||||
gui.Views.Menu.Title = opts.Title
|
gui.Views.Menu.Title = opts.Title
|
||||||
|
@@ -147,11 +147,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CreateMenuOptions struct {
|
type CreateMenuOptions struct {
|
||||||
Title string
|
Title string
|
||||||
Prompt string // a message that will be displayed above the menu options
|
Prompt string // a message that will be displayed above the menu options
|
||||||
Items []*MenuItem
|
Items []*MenuItem
|
||||||
HideCancel bool
|
HideCancel bool
|
||||||
ColumnAlignment []utils.Alignment
|
ColumnAlignment []utils.Alignment
|
||||||
|
AllowFilteringKeybindings bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreatePopupPanelOpts struct {
|
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.FilterFiles,
|
||||||
filter_and_search.FilterFuzzy,
|
filter_and_search.FilterFuzzy,
|
||||||
filter_and_search.FilterMenu,
|
filter_and_search.FilterMenu,
|
||||||
|
filter_and_search.FilterMenuByKeybinding,
|
||||||
filter_and_search.FilterMenuCancelFilterWithEscape,
|
filter_and_search.FilterMenuCancelFilterWithEscape,
|
||||||
filter_and_search.FilterMenuWithNoKeybindings,
|
filter_and_search.FilterMenuWithNoKeybindings,
|
||||||
filter_and_search.FilterRemoteBranches,
|
filter_and_search.FilterRemoteBranches,
|
||||||
|
Reference in New Issue
Block a user