Skip to content

Commit

Permalink
fix(filter): made filter work with lists as choose (#424)
Browse files Browse the repository at this point in the history
* made filter work with lists as choose

* lint fix

* response to code review
  • Loading branch information
MikaelFangel authored Oct 3, 2023
1 parent 77aa864 commit 971b6cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 11 additions & 13 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ func (o Options) Run() error {

v := viewport.New(o.Width, o.Height)

var choices []string
if input, _ := stdin.Read(); input != "" {
input = strings.TrimSuffix(input, "\n")
if input != "" {
choices = strings.Split(input, "\n")
if len(o.Options) == 0 {
if input, _ := stdin.Read(); input != "" {
o.Options = strings.Split(strings.TrimSuffix(input, "\n"), "\n")
} else {
o.Options = files.List()
}
} else {
choices = files.List()
}

if len(choices) == 0 {
if len(o.Options) == 0 {
return errors.New("no options provided, see `gum filter --help`")
}

Expand All @@ -58,19 +56,19 @@ func (o Options) Run() error {
}
switch {
case o.Value != "" && o.Fuzzy:
matches = fuzzy.Find(o.Value, choices)
matches = fuzzy.Find(o.Value, o.Options)
case o.Value != "" && !o.Fuzzy:
matches = exactMatches(o.Value, choices)
matches = exactMatches(o.Value, o.Options)
default:
matches = matchAll(choices)
matches = matchAll(o.Options)
}

if o.NoLimit {
o.Limit = len(choices)
o.Limit = len(o.Options)
}

p := tea.NewProgram(model{
choices: choices,
choices: o.Options,
indicator: o.Indicator,
matches: matches,
header: o.Header,
Expand Down
2 changes: 2 additions & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

// Options is the customization options for the filter command.
type Options struct {
Options []string `arg:"" optional:"" help:"Options to filter."`

Indicator string `help:"Character for selection" default:"•" env:"GUM_FILTER_INDICATOR"`
IndicatorStyle style.Styles `embed:"" prefix:"indicator." set:"defaultForeground=212" envprefix:"GUM_FILTER_INDICATOR_"`
Limit int `help:"Maximum number of options to pick" default:"1" group:"Selection"`
Expand Down

0 comments on commit 971b6cf

Please sign in to comment.