1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-29 22:48:24 +02:00

Default to substring filtering, add option to go back to fuzzy filtering

By default we now search for substrings; you can search for multiple substrings
by separating them with spaces. Add a config option gui.filterMode that can be
set to 'fuzzy' to switch back to the previous behavior.
This commit is contained in:
Stefan Haller
2024-03-10 10:58:19 +01:00
parent a82e26d11e
commit a8797c7261
12 changed files with 164 additions and 52 deletions

View File

@@ -7,18 +7,67 @@ import (
"github.com/samber/lo"
)
func FuzzySearch(needle string, haystack []string) []string {
func FuzzySearch(needle string, haystack []string, useFuzzySearch bool) []string {
if needle == "" {
return []string{}
}
matches := fuzzy.Find(needle, haystack)
matches := Find(needle, haystack, useFuzzySearch)
return lo.Map(matches, func(match fuzzy.Match, _ int) string {
return match.Str
})
}
// Duplicated from the fuzzy package because it's private there
type stringSource []string
func (ss stringSource) String(i int) string {
return ss[i]
}
func (ss stringSource) Len() int { return len(ss) }
// Drop-in replacement for fuzzy.Find (except that it doesn't fill out
// MatchedIndexes or Score, but we are not using these)
func FindSubstrings(pattern string, data []string) fuzzy.Matches {
return FindSubstringsFrom(pattern, stringSource(data))
}
// Drop-in replacement for fuzzy.FindFrom (except that it doesn't fill out
// MatchedIndexes or Score, but we are not using these)
func FindSubstringsFrom(pattern string, data fuzzy.Source) fuzzy.Matches {
substrings := strings.Fields(pattern)
result := fuzzy.Matches{}
outer:
for i := 0; i < data.Len(); i++ {
s := data.String(i)
for _, sub := range substrings {
if !CaseAwareContains(s, sub) {
continue outer
}
}
result = append(result, fuzzy.Match{Str: s, Index: i})
}
return result
}
func Find(pattern string, data []string, useFuzzySearch bool) fuzzy.Matches {
if useFuzzySearch {
return fuzzy.Find(pattern, data)
}
return FindSubstrings(pattern, data)
}
func FindFrom(pattern string, data fuzzy.Source, useFuzzySearch bool) fuzzy.Matches {
if useFuzzySearch {
return fuzzy.FindFrom(pattern, data)
}
return FindSubstringsFrom(pattern, data)
}
func CaseAwareContains(haystack, needle string) bool {
// if needle contains an uppercase letter, we'll do a case sensitive search
if ContainsUppercase(needle) {