1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/utils/search.go

49 lines
916 B
Go
Raw Normal View History

package utils
import (
"sort"
2023-05-27 12:38:37 +02:00
"strings"
2022-03-19 10:12:58 +02:00
"github.com/jesseduffield/generics/slices"
"github.com/sahilm/fuzzy"
)
func FuzzySearch(needle string, haystack []string) []string {
if needle == "" {
return []string{}
}
matches := fuzzy.Find(needle, haystack)
sort.Sort(matches)
2022-03-19 10:12:58 +02:00
return slices.Map(matches, func(match fuzzy.Match) string {
return match.Str
})
}
2023-05-27 12:38:37 +02:00
2023-06-03 06:56:15 +02:00
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 {
2023-05-27 12:38:37 +02:00
return strings.Contains(
2023-06-03 06:56:15 +02:00
strings.ToLower(haystack),
strings.ToLower(needle),
2023-05-27 12:38:37 +02:00
)
}