avoid Fuzzy search

This commit is contained in:
Enrico Candino 2022-10-09 09:38:48 +02:00 committed by Maas Lalani
parent d45b728b4d
commit 8dec822e75

View file

@ -11,7 +11,6 @@
package filter package filter
import ( import (
"sort"
"strings" "strings"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
@ -179,12 +178,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// A character was entered, this likely means that the text input // A character was entered, this likely means that the text input
// has changed. This suggests that the matches are outdated, so // has changed. This suggests that the matches are outdated, so
// update them, with a fuzzy finding algorithm provided by // update them, with a fuzzy finding algorithm provided by
// https://github.com/sahilm/fuzzy // https://github.com/sahilm/fuzzy or with an exact match search
m.matches = fuzzy.Find(m.textinput.Value(), m.choices)
// For exact search return only the exact matches
if m.exact { if m.exact {
m.matches = exactMatches(m.textinput.Value(), m.matches) m.matches = exactMatches(m.textinput.Value(), m.choices)
} else {
m.matches = fuzzy.Find(m.textinput.Value(), m.choices)
} }
// If the search field is empty, let's not display the matches // If the search field is empty, let's not display the matches
@ -254,34 +252,26 @@ func matchAll(options []string) []fuzzy.Match {
return matches return matches
} }
func exactMatches(search string, matches []fuzzy.Match) []fuzzy.Match { func exactMatches(search string, choices []string) []fuzzy.Match {
if len(search) < 2 {
return matches
}
exactMatches := fuzzy.Matches{} exactMatches := fuzzy.Matches{}
for _, m := range matches { for i, choice := range choices {
search = strings.ToLower(search) search = strings.ToLower(search)
matchedString := strings.ToLower(m.Str) matchedString := strings.ToLower(choice)
index := strings.Index(matchedString, search) index := strings.Index(matchedString, search)
if index >= 0 { if index >= 0 {
// we need to override the MatchedIndexes because sometimes matchedIndexes := []int{}
// they are not consecutive, but they have to be in the exact search for s := range search {
m.MatchedIndexes = []int{} matchedIndexes = append(matchedIndexes, index+s)
for i := range search {
m.MatchedIndexes = append(m.MatchedIndexes, index+i)
} }
exactMatches = append(exactMatches, m) exactMatches = append(exactMatches, fuzzy.Match{
Str: choice,
Index: i,
MatchedIndexes: matchedIndexes,
})
} }
} }
// we need to sort by name the matches because
// they are sorted with the fuzzy ranking
sort.Slice(exactMatches, func(i, j int) bool {
return exactMatches[i].Str > exactMatches[j].Str
})
return exactMatches return exactMatches
} }