1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-02 09:21:40 +02:00

Case insensitive string comparison

This commit is contained in:
Jesse Duffield 2023-05-27 20:38:37 +10:00
parent 13326344f0
commit bf5871cc4f
5 changed files with 97 additions and 57 deletions

View File

@ -1,8 +1,6 @@
package context package context
import ( import (
"strings"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -35,6 +33,10 @@ func (self *FilteredList[T]) ClearFilter() {
self.SetFilter("") self.SetFilter("")
} }
func (self *FilteredList[T]) IsFiltering() bool {
return self.filter != ""
}
func (self *FilteredList[T]) GetFilteredList() []T { func (self *FilteredList[T]) GetFilteredList() []T {
if self.filteredIndices == nil { if self.filteredIndices == nil {
return self.getList() return self.getList()
@ -54,7 +56,7 @@ func (self *FilteredList[T]) applyFilter() {
self.filteredIndices = []int{} self.filteredIndices = []int{}
for i, item := range self.getList() { for i, item := range self.getList() {
for _, field := range self.getFilterFields(item) { for _, field := range self.getFilterFields(item) {
if strings.Contains(field, self.filter) { if self.match(field, self.filter) {
self.filteredIndices = append(self.filteredIndices, i) self.filteredIndices = append(self.filteredIndices, i)
break break
} }
@ -62,3 +64,7 @@ func (self *FilteredList[T]) applyFilter() {
} }
} }
} }
func (self *FilteredList[T]) match(haystack string, needle string) bool {
return utils.CaseInsensitiveContains(haystack, needle)
}

View File

@ -32,7 +32,6 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
) )
getDisplayStrings := func(startIdx int, length int) [][]string { getDisplayStrings := func(startIdx int, length int) [][]string {
c.Log.Warn("in get display strings")
lines := presentation.RenderFileTree(viewModel, c.Modes().Diffing.Ref, c.Model().Submodules) lines := presentation.RenderFileTree(viewModel, c.Modes().Diffing.Ref, c.Model().Submodules)
return slices.Map(lines, func(line string) []string { return slices.Map(lines, func(line string) []string {
return []string{line} return []string{line}

View File

@ -1,53 +0,0 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestFuzzySearch is a function.
func TestFuzzySearch(t *testing.T) {
type scenario struct {
needle string
haystack []string
expected []string
}
scenarios := []scenario{
{
needle: "",
haystack: []string{"test"},
expected: []string{},
},
{
needle: "test",
haystack: []string{"test"},
expected: []string{"test"},
},
{
needle: "o",
haystack: []string{"a", "o", "e"},
expected: []string{"o"},
},
{
needle: "mybranch",
haystack: []string{"my_branch", "mybranch", "branch", "this is my branch"},
expected: []string{"mybranch", "my_branch", "this is my branch"},
},
{
needle: "test",
haystack: []string{"not a good match", "this 'test' is a good match", "test"},
expected: []string{"test", "this 'test' is a good match"},
},
{
needle: "test",
haystack: []string{"Test"},
expected: []string{"Test"},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, FuzzySearch(s.needle, s.haystack))
}
}

View File

@ -2,6 +2,7 @@ package utils
import ( import (
"sort" "sort"
"strings"
"github.com/jesseduffield/generics/slices" "github.com/jesseduffield/generics/slices"
"github.com/sahilm/fuzzy" "github.com/sahilm/fuzzy"
@ -19,3 +20,10 @@ func FuzzySearch(needle string, haystack []string) []string {
return match.Str return match.Str
}) })
} }
func CaseInsensitiveContains(a, b string) bool {
return strings.Contains(
strings.ToLower(a),
strings.ToLower(b),
)
}

80
pkg/utils/search_test.go Normal file
View File

@ -0,0 +1,80 @@
package utils
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// TestFuzzySearch is a function.
func TestFuzzySearch(t *testing.T) {
type scenario struct {
needle string
haystack []string
expected []string
}
scenarios := []scenario{
{
needle: "",
haystack: []string{"test"},
expected: []string{},
},
{
needle: "test",
haystack: []string{"test"},
expected: []string{"test"},
},
{
needle: "o",
haystack: []string{"a", "o", "e"},
expected: []string{"o"},
},
{
needle: "mybranch",
haystack: []string{"my_branch", "mybranch", "branch", "this is my branch"},
expected: []string{"mybranch", "my_branch", "this is my branch"},
},
{
needle: "test",
haystack: []string{"not a good match", "this 'test' is a good match", "test"},
expected: []string{"test", "this 'test' is a good match"},
},
{
needle: "test",
haystack: []string{"Test"},
expected: []string{"Test"},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, FuzzySearch(s.needle, s.haystack))
}
}
func TestCaseInsensitiveContains(t *testing.T) {
testCases := []struct {
haystack string
needle string
expected bool
}{
{"Hello, World!", "world", true}, // Case-insensitive match
{"Hello, World!", "WORLD", true}, // Case-insensitive match
{"Hello, World!", "orl", true}, // Case-insensitive match
{"Hello, World!", "o, W", true}, // Case-insensitive match
{"Hello, World!", "hello", true}, // Case-insensitive match
{"Hello, World!", "Foo", false}, // No match
{"Hello, World!", "Hello, World!!", false}, // No match
{"Hello, World!", "", true}, // Empty needle matches
{"", "Hello", false}, // Empty haystack doesn't match
{"", "", true}, // Empty strings match
{"", " ", false}, // Empty haystack, non-empty needle
{" ", "", true}, // Non-empty haystack, empty needle
}
for i, testCase := range testCases {
result := CaseInsensitiveContains(testCase.haystack, testCase.needle)
assert.Equal(t, testCase.expected, result, fmt.Sprintf("Test case %d failed. Expected '%v', got '%v' for '%s' in '%s'", i, testCase.expected, result, testCase.needle, testCase.haystack))
}
}