chore: use --[no-]fuzzy as flag

This commit is contained in:
Maas Lalani 2022-10-13 10:47:27 -04:00
parent 8dec822e75
commit 08c34cfa2f
3 changed files with 11 additions and 13 deletions

View file

@ -79,7 +79,7 @@ func (o Options) Run() error {
selected: make(map[string]struct{}),
limit: o.Limit,
reverse: o.Reverse,
exact: o.Exact,
fuzzy: o.Fuzzy,
}, options...)
tm, err := p.StartReturningModel()

View file

@ -42,7 +42,7 @@ type model struct {
selectedPrefixStyle lipgloss.Style
unselectedPrefixStyle lipgloss.Style
reverse bool
exact bool
fuzzy bool
}
func (m model) Init() tea.Cmd { return nil }
@ -175,14 +175,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
yOffsetFromBottom = max(0, len(m.matches)-m.viewport.YOffset)
}
// A character was entered, this likely means that the text input
// has changed. This suggests that the matches are outdated, so
// update them, with a fuzzy finding algorithm provided by
// https://github.com/sahilm/fuzzy or with an exact match search
if m.exact {
m.matches = exactMatches(m.textinput.Value(), m.choices)
} else {
// A character was entered, this likely means that the text input has
// changed. This suggests that the matches are outdated, so update them.
if m.fuzzy {
m.matches = fuzzy.Find(m.textinput.Value(), m.choices)
} else {
m.matches = exactMatches(m.textinput.Value(), m.choices)
}
// If the search field is empty, let's not display the matches
@ -253,7 +251,7 @@ func matchAll(options []string) []fuzzy.Match {
}
func exactMatches(search string, choices []string) []fuzzy.Match {
exactMatches := fuzzy.Matches{}
matches := fuzzy.Matches{}
for i, choice := range choices {
search = strings.ToLower(search)
matchedString := strings.ToLower(choice)
@ -264,7 +262,7 @@ func exactMatches(search string, choices []string) []fuzzy.Match {
for s := range search {
matchedIndexes = append(matchedIndexes, index+s)
}
exactMatches = append(exactMatches, fuzzy.Match{
matches = append(matches, fuzzy.Match{
Str: choice,
Index: i,
MatchedIndexes: matchedIndexes,
@ -272,7 +270,7 @@ func exactMatches(search string, choices []string) []fuzzy.Match {
}
}
return exactMatches
return matches
}
//nolint:unparam

View file

@ -21,5 +21,5 @@ type Options struct {
Height int `help:"Input height" default:"0" env:"GUM_FILTER_HEIGHT"`
Value string `help:"Initial filter value" default:"" env:"GUM_FILTER_VALUE"`
Reverse bool `help:"Display from the bottom of the screen" env:"GUM_FILTER_REVERSE"`
Exact bool `help:"Enable exact-match" env:"GUM_FILTER_EXACT"`
Fuzzy bool `help:"Enable fuzzy matching" default:"true" env:"GUM_FILTER_FUZZY" negatable:""`
}