diff --git a/pkg/cy/api/docs-input.md b/pkg/cy/api/docs-input.md index b80aec31..ab1f781c 100644 --- a/pkg/cy/api/docs-input.md +++ b/pkg/cy/api/docs-input.md @@ -8,11 +8,12 @@ This function supports a range of named parameters that adjust its functionality: +- `:animated` (boolean): Enable and disable background animation. +- `:case-sensitive` (boolean): Whether the matching algorithm should respect differences in case. The default is `false`. - `:full` (boolean): If true, occupy the entire screen. +- `:headers` ([]string): Provide a title for each column. This mostly used for filtering tabular data. - `:prompt` (string): The text that will be shown beneath the search window. - `:reverse` (boolean): Display from the top of the screen (rather than the bottom.) -- `:animated` (boolean): Enable and disable background animation. -- `:headers` ([]string): Provide a title for each column. This mostly used for filtering tabular data. # doc: Text diff --git a/pkg/cy/api/input.go b/pkg/cy/api/input.go index 52da4647..58b054a7 100644 --- a/pkg/cy/api/input.go +++ b/pkg/cy/api/input.go @@ -22,11 +22,12 @@ type InputModule struct { } type FuzzyParams struct { - Prompt string - Full bool - Reverse bool - Animated *bool - Headers *[]string + Prompt string + Full bool + Reverse bool + CaseSensitive bool + Animated *bool + Headers *[]string } func (i *InputModule) Find( @@ -61,6 +62,7 @@ func (i *InputModule) Find( fuzzy.WithResult(result), fuzzy.WithPrompt(params.Prompt), fuzzy.WithInitial(initial), + fuzzy.WithCaseSensitive(params.CaseSensitive), } if !params.Full { diff --git a/pkg/input/fuzzy/filter.go b/pkg/input/fuzzy/filter.go index e1fd4af5..5198ee78 100644 --- a/pkg/input/fuzzy/filter.go +++ b/pkg/input/fuzzy/filter.go @@ -141,11 +141,15 @@ func UnmarshalOptions(input *janet.Value) (result []Option, err error) { return } -func Filter(options []Option, search string) []Option { +func Filter( + options []Option, + search string, + caseSensitive bool, +) []Option { matches := make([]Option, 0) for _, option := range options { result, pos := fzf.FuzzyMatchV2( - true, + caseSensitive, true, true, option.Chars, diff --git a/pkg/input/fuzzy/module.go b/pkg/input/fuzzy/module.go index 59796641..ccd03b1a 100644 --- a/pkg/input/fuzzy/module.go +++ b/pkg/input/fuzzy/module.go @@ -52,6 +52,9 @@ type Fuzzy struct { // shown before the number of items prompt string + // whether search should be case-sensitive + caseSensitive bool + // headers for the table headers []string @@ -239,6 +242,14 @@ func WithHeaders(headers ...string) Setting { } } +// WithCaseSensitive determines whether the matching algorithm will be +// case-sensitive. +func WithCaseSensitive(value bool) Setting { + return func(ctx context.Context, f *Fuzzy) { + f.caseSensitive = value + } +} + func newFuzzy( ctx context.Context, options []Option, @@ -277,10 +288,18 @@ type matchResult struct { Filtered []Option } -func queryOptions(options []Option, pattern string) tea.Cmd { +func queryOptions( + options []Option, + pattern string, + caseSensitive bool, +) tea.Cmd { return func() tea.Msg { return matchResult{ - Filtered: Filter(options, pattern), + Filtered: Filter( + options, + pattern, + caseSensitive, + ), } } } diff --git a/pkg/input/fuzzy/update.go b/pkg/input/fuzzy/update.go index ce6cafc2..93afa4a6 100644 --- a/pkg/input/fuzzy/update.go +++ b/pkg/input/fuzzy/update.go @@ -86,7 +86,11 @@ func (f *Fuzzy) Update(msg tea.Msg) (taro.Model, tea.Cmd) { value := f.textInput.Value() if f.pattern != value { f.pattern = value - cmds = append(cmds, queryOptions(f.options, value)) + cmds = append(cmds, queryOptions( + f.options, + value, + f.caseSensitive, + )) } return f, tea.Batch(cmds...)