From a08799ac152de70dfb5820f79da9c1c1ee192cdd Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 5 Aug 2025 15:17:08 +0200 Subject: [PATCH] Allow filterable contexts to customize the filter label --- pkg/gui/context/filtered_list_view_model.go | 7 +++++++ pkg/gui/context/menu_context.go | 9 +++++++++ pkg/gui/controllers/helpers/search_helper.go | 4 ++-- pkg/gui/controllers/helpers/window_arrangement_helper.go | 6 +++--- pkg/gui/types/context.go | 2 ++ pkg/i18n/english.go | 2 ++ 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pkg/gui/context/filtered_list_view_model.go b/pkg/gui/context/filtered_list_view_model.go index 2c2841964..ce2f8ac36 100644 --- a/pkg/gui/context/filtered_list_view_model.go +++ b/pkg/gui/context/filtered_list_view_model.go @@ -1,5 +1,7 @@ package context +import "github.com/jesseduffield/lazygit/pkg/i18n" + type FilteredListViewModel[T HasID] struct { *FilteredList[T] *ListViewModel[T] @@ -33,3 +35,8 @@ func (self *FilteredListViewModel[T]) ClearFilter() { self.SetSelection(unfilteredIndex) } + +// Default implementation of most filterable contexts. Can be overridden if needed. +func (self *FilteredListViewModel[T]) FilterPrefix(tr *i18n.TranslationSet) string { + return tr.FilterPrefix +} diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go index d3f1b9cc4..0d59f1fa9 100644 --- a/pkg/gui/context/menu_context.go +++ b/pkg/gui/context/menu_context.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" ) @@ -238,3 +239,11 @@ func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error { func (self *MenuContext) RangeSelectEnabled() bool { return false } + +func (self *MenuContext) FilterPrefix(tr *i18n.TranslationSet) string { + if self.allowFilteringKeybindings { + return tr.FilterPrefixMenu + } + + return self.FilteredListViewModel.FilterPrefix(tr) +} diff --git a/pkg/gui/controllers/helpers/search_helper.go b/pkg/gui/controllers/helpers/search_helper.go index cc9f22b22..67af0695b 100644 --- a/pkg/gui/controllers/helpers/search_helper.go +++ b/pkg/gui/controllers/helpers/search_helper.go @@ -35,7 +35,7 @@ func (self *SearchHelper) OpenFilterPrompt(context types.IFilterableContext) err state.Context = context - self.searchPrefixView().SetContent(self.c.Tr.FilterPrefix) + self.searchPrefixView().SetContent(context.FilterPrefix(self.c.Tr)) promptView := self.promptView() promptView.ClearTextArea() self.OnPromptContentChanged("") @@ -69,7 +69,7 @@ func (self *SearchHelper) DisplayFilterStatus(context types.IFilterableContext) state.Context = context searchString := context.GetFilter() - self.searchPrefixView().SetContent(self.c.Tr.FilterPrefix) + self.searchPrefixView().SetContent(context.FilterPrefix(self.c.Tr)) promptView := self.promptView() keybindingConfig := self.c.UserConfig().Keybinding diff --git a/pkg/gui/controllers/helpers/window_arrangement_helper.go b/pkg/gui/controllers/helpers/window_arrangement_helper.go index 51c5cf37a..accd61adc 100644 --- a/pkg/gui/controllers/helpers/window_arrangement_helper.go +++ b/pkg/gui/controllers/helpers/window_arrangement_helper.go @@ -79,10 +79,10 @@ func (self *WindowArrangementHelper) GetWindowDimensions(informationStr string, repoState := self.c.State().GetRepoState() var searchPrefix string - if repoState.GetSearchState().SearchType() == types.SearchTypeSearch { - searchPrefix = self.c.Tr.SearchPrefix + if filterableContext, ok := repoState.GetSearchState().Context.(types.IFilterableContext); ok { + searchPrefix = filterableContext.FilterPrefix(self.c.Tr) } else { - searchPrefix = self.c.Tr.FilterPrefix + searchPrefix = self.c.Tr.SearchPrefix } args := WindowArrangementArgs{ diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go index 6d5f5d573..917342776 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -4,6 +4,7 @@ import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui/patch_exploring" + "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sasha-s/go-deadlock" ) @@ -130,6 +131,7 @@ type IFilterableContext interface { ReApplyFilter(bool) IsFiltering() bool IsFilterableContext() + FilterPrefix(tr *i18n.TranslationSet) string } type ISearchableContext interface { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 3a8823b8b..39227d25d 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -816,6 +816,7 @@ type TranslationSet struct { SearchKeybindings string SearchPrefix string FilterPrefix string + FilterPrefixMenu string ExitSearchMode string ExitTextFilterMode string Switch string @@ -1881,6 +1882,7 @@ func EnglishTranslationSet() *TranslationSet { SearchKeybindings: "%s: Next match, %s: Previous match, %s: Exit search mode", SearchPrefix: "Search: ", FilterPrefix: "Filter: ", + FilterPrefixMenu: "Filter (prepend '@' to filter keybindings): ", WorktreesTitle: "Worktrees", WorktreeTitle: "Worktree", Switch: "Switch",