From 08c34cfa2f99d5946dc183eb63740d2128cf44b7 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Thu, 13 Oct 2022 10:47:27 -0400 Subject: [PATCH] chore: use --[no-]fuzzy as flag --- filter/command.go | 2 +- filter/filter.go | 20 +++++++++----------- filter/options.go | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/filter/command.go b/filter/command.go index abf8555..df030c9 100644 --- a/filter/command.go +++ b/filter/command.go @@ -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() diff --git a/filter/filter.go b/filter/filter.go index 744ae36..dc688ce 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -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 diff --git a/filter/options.go b/filter/options.go index 889c3f3..3737e8f 100644 --- a/filter/options.go +++ b/filter/options.go @@ -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:""` }