Skip to content

Commit d52d5e6

Browse files
committed
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.
1 parent f1c07b3 commit d52d5e6

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pkg/gui/context/filtered_list.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
type FilteredList[T any] struct {
1313
filteredIndices []int // if nil, we are not filtering
1414

15-
getList func() []T
16-
getFilterFields func(T) []string
17-
filter string
15+
getList func() []T
16+
getFilterFields func(T) []string
17+
preprocessFilter func(string) string
18+
filter string
1819

1920
mutex deadlock.Mutex
2021
}
@@ -26,6 +27,10 @@ func NewFilteredList[T any](getList func() []T, getFilterFields func(T) []string
2627
}
2728
}
2829

30+
func (self *FilteredList[T]) SetPreprocessFilterFunc(preprocessFilter func(string) string) {
31+
self.preprocessFilter = preprocessFilter
32+
}
33+
2934
func (self *FilteredList[T]) GetFilter() string {
3035
return self.filter
3136
}
@@ -79,15 +84,20 @@ func (self *FilteredList[T]) applyFilter(useFuzzySearch bool) {
7984
self.mutex.Lock()
8085
defer self.mutex.Unlock()
8186

82-
if self.filter == "" {
87+
filter := self.filter
88+
if self.preprocessFilter != nil {
89+
filter = self.preprocessFilter(filter)
90+
}
91+
92+
if filter == "" {
8393
self.filteredIndices = nil
8494
} else {
8595
source := &fuzzySource[T]{
8696
list: self.getList(),
8797
getFilterFields: self.getFilterFields,
8898
}
8999

90-
matches := utils.FindFrom(self.filter, source, useFuzzySearch)
100+
matches := utils.FindFrom(filter, source, useFuzzySearch)
91101
self.filteredIndices = lo.Map(matches, func(match fuzzy.Match, _ int) int {
92102
return match.Index
93103
})

0 commit comments

Comments
 (0)