1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00
lazygit/pkg/utils/search.go
Stefan Haller a82e26d11e Don't sort the results of fuzzy.Find
It sorts them already, so it's unnecessary. In the next commit we use this same
code for substring searching too, and in that case we don't want to sort because
sorting is by Score, but we don't even fill in the score for substring
searching.
2024-03-17 07:57:19 +01:00

47 lines
871 B
Go

package utils
import (
"strings"
"github.com/sahilm/fuzzy"
"github.com/samber/lo"
)
func FuzzySearch(needle string, haystack []string) []string {
if needle == "" {
return []string{}
}
matches := fuzzy.Find(needle, haystack)
return lo.Map(matches, func(match fuzzy.Match, _ int) string {
return match.Str
})
}
func CaseAwareContains(haystack, needle string) bool {
// if needle contains an uppercase letter, we'll do a case sensitive search
if ContainsUppercase(needle) {
return strings.Contains(haystack, needle)
}
return CaseInsensitiveContains(haystack, needle)
}
func ContainsUppercase(s string) bool {
for _, r := range s {
if r >= 'A' && r <= 'Z' {
return true
}
}
return false
}
func CaseInsensitiveContains(haystack, needle string) bool {
return strings.Contains(
strings.ToLower(haystack),
strings.ToLower(needle),
)
}