Skip to content

Commit

Permalink
feat(filter): allow to pre-select items with --selected (#777)
Browse files Browse the repository at this point in the history
closes #593
  • Loading branch information
caarlos0 authored Dec 13, 2024
1 parent f230a3d commit 966237b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 19 additions & 4 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (o Options) Run() error {
km.ToggleAll.SetEnabled(true)
}

p := tea.NewProgram(model{
m := model{
choices: o.Options,
indicator: o.Indicator,
matches: matches,
Expand All @@ -112,14 +112,29 @@ func (o Options) Run() error {
showHelp: o.ShowHelp,
keymap: km,
help: help.New(),
}, options...)
}

for _, s := range o.Selected {
if o.NoLimit || o.Limit > 1 {
m.selected[s] = struct{}{}
}
}

if len(o.Selected) > 0 {
for i, match := range matches {
if match.Str == o.Selected[0] {
m.cursor = i
break
}
}
}

tm, err := p.Run()
tm, err := tea.NewProgram(m, options...).Run()
if err != nil {
return fmt.Errorf("unable to run filter: %w", err)
}

m := tm.(model)
m = tm.(model)
if !m.submitted {
return errors.New("nothing selected")
}
Expand Down
1 change: 1 addition & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Options struct {
Limit int `help:"Maximum number of options to pick" default:"1" group:"Selection"`
NoLimit bool `help:"Pick unlimited number of options (ignores limit)" group:"Selection"`
SelectIfOne bool `help:"Select the given option if there is only one" group:"Selection"`
Selected []string `help:"Options that should start as selected (selects all if given '*')" default:"" env:"GUM_FILTER_SELECTED"`
ShowHelp bool `help:"Show help keybinds" default:"true" negatable:"" env:"GUM_FILTER_SHOW_HELP"`
Strict bool `help:"Only returns if anything matched. Otherwise return Filter" negatable:"" default:"true" group:"Selection"`
SelectedPrefix string `help:"Character to indicate selected items (hidden if limit is 1)" default:" ◉ " env:"GUM_FILTER_SELECTED_PREFIX"`
Expand Down

0 comments on commit 966237b

Please sign in to comment.