diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-06-03 14:56:15 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-03 12:54:14 +1000 |
commit | 7d7399a89f18fe58389ecdcd1f69f4494c6567e4 (patch) | |
tree | 0e3188df72ee77244ba31c5776ed2e7d92753c9c | |
parent | 9df634f13f6dd8f1e766e81c8ad2ff036df8fe20 (diff) |
Support case sensitive filtering
-rw-r--r-- | pkg/gui/context/filtered_list.go | 2 | ||||
-rw-r--r-- | pkg/utils/search.go | 25 |
2 files changed, 23 insertions, 4 deletions
diff --git a/pkg/gui/context/filtered_list.go b/pkg/gui/context/filtered_list.go index bffd0eddb..ce2e12590 100644 --- a/pkg/gui/context/filtered_list.go +++ b/pkg/gui/context/filtered_list.go @@ -66,7 +66,7 @@ func (self *FilteredList[T]) applyFilter() { } func (self *FilteredList[T]) match(haystack string, needle string) bool { - return utils.CaseInsensitiveContains(haystack, needle) + return utils.CaseAwareContains(haystack, needle) } func (self *FilteredList[T]) UnfilteredIndex(index int) int { diff --git a/pkg/utils/search.go b/pkg/utils/search.go index e90d2b5b0..14b3d7b3e 100644 --- a/pkg/utils/search.go +++ b/pkg/utils/search.go @@ -21,9 +21,28 @@ func FuzzySearch(needle string, haystack []string) []string { }) } -func CaseInsensitiveContains(a, b string) bool { +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(a), - strings.ToLower(b), + strings.ToLower(haystack), + strings.ToLower(needle), ) } |