fix(filter): made filter work with lists as choose (#424)

* made filter work with lists as choose

* lint fix

* response to code review
This commit is contained in:
Mikael Fangel 2023-10-03 20:33:57 +00:00 committed by GitHub
parent 77aa8640f2
commit 971b6cf16f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View file

@ -33,17 +33,15 @@ func (o Options) Run() error {
v := viewport.New(o.Width, o.Height) v := viewport.New(o.Width, o.Height)
var choices []string if len(o.Options) == 0 {
if input, _ := stdin.Read(); input != "" { if input, _ := stdin.Read(); input != "" {
input = strings.TrimSuffix(input, "\n") o.Options = strings.Split(strings.TrimSuffix(input, "\n"), "\n")
if input != "" { } else {
choices = strings.Split(input, "\n") 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`") return errors.New("no options provided, see `gum filter --help`")
} }
@ -58,19 +56,19 @@ func (o Options) Run() error {
} }
switch { switch {
case o.Value != "" && o.Fuzzy: case o.Value != "" && o.Fuzzy:
matches = fuzzy.Find(o.Value, choices) matches = fuzzy.Find(o.Value, o.Options)
case o.Value != "" && !o.Fuzzy: case o.Value != "" && !o.Fuzzy:
matches = exactMatches(o.Value, choices) matches = exactMatches(o.Value, o.Options)
default: default:
matches = matchAll(choices) matches = matchAll(o.Options)
} }
if o.NoLimit { if o.NoLimit {
o.Limit = len(choices) o.Limit = len(o.Options)
} }
p := tea.NewProgram(model{ p := tea.NewProgram(model{
choices: choices, choices: o.Options,
indicator: o.Indicator, indicator: o.Indicator,
matches: matches, matches: matches,
header: o.Header, header: o.Header,

View file

@ -8,6 +8,8 @@ import (
// Options is the customization options for the filter command. // Options is the customization options for the filter command.
type Options struct { type Options struct {
Options []string `arg:"" optional:"" help:"Options to filter."`
Indicator string `help:"Character for selection" default:"•" env:"GUM_FILTER_INDICATOR"` Indicator string `help:"Character for selection" default:"•" env:"GUM_FILTER_INDICATOR"`
IndicatorStyle style.Styles `embed:"" prefix:"indicator." set:"defaultForeground=212" envprefix:"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"` Limit int `help:"Maximum number of options to pick" default:"1" group:"Selection"`