Skip to content

Commit

Permalink
feat: case-insensitive search
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Aug 1, 2024
1 parent 0ecb377 commit ab09b14
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pkg/cy/api/docs-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions pkg/cy/api/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions pkg/input/fuzzy/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 21 additions & 2 deletions pkg/input/fuzzy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
),
}
}
}
6 changes: 5 additions & 1 deletion pkg/input/fuzzy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit ab09b14

Please sign in to comment.