1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-24 19:39:16 +02:00

Allow filtered lists to preprocess the filter string

An example for this can be seen in the next commit.

We make this a setter rather than an added constructor argument so that we don't
have to change all those contexts that don't want to make use of this.
This commit is contained in:
Stefan Haller
2025-08-05 15:15:14 +02:00
parent f1c07b3aed
commit d52d5e6a9e

View File

@@ -12,9 +12,10 @@ import (
type FilteredList[T any] struct { type FilteredList[T any] struct {
filteredIndices []int // if nil, we are not filtering filteredIndices []int // if nil, we are not filtering
getList func() []T getList func() []T
getFilterFields func(T) []string getFilterFields func(T) []string
filter string preprocessFilter func(string) string
filter string
mutex deadlock.Mutex mutex deadlock.Mutex
} }
@@ -26,6 +27,10 @@ func NewFilteredList[T any](getList func() []T, getFilterFields func(T) []string
} }
} }
func (self *FilteredList[T]) SetPreprocessFilterFunc(preprocessFilter func(string) string) {
self.preprocessFilter = preprocessFilter
}
func (self *FilteredList[T]) GetFilter() string { func (self *FilteredList[T]) GetFilter() string {
return self.filter return self.filter
} }
@@ -79,7 +84,12 @@ func (self *FilteredList[T]) applyFilter(useFuzzySearch bool) {
self.mutex.Lock() self.mutex.Lock()
defer self.mutex.Unlock() defer self.mutex.Unlock()
if self.filter == "" { filter := self.filter
if self.preprocessFilter != nil {
filter = self.preprocessFilter(filter)
}
if filter == "" {
self.filteredIndices = nil self.filteredIndices = nil
} else { } else {
source := &fuzzySource[T]{ source := &fuzzySource[T]{
@@ -87,7 +97,7 @@ func (self *FilteredList[T]) applyFilter(useFuzzySearch bool) {
getFilterFields: self.getFilterFields, getFilterFields: self.getFilterFields,
} }
matches := utils.FindFrom(self.filter, source, useFuzzySearch) matches := utils.FindFrom(filter, source, useFuzzySearch)
self.filteredIndices = lo.Map(matches, func(match fuzzy.Match, _ int) int { self.filteredIndices = lo.Map(matches, func(match fuzzy.Match, _ int) int {
return match.Index return match.Index
}) })