diff --git a/filter/command.go b/filter/command.go index dfc827465..c933a05b9 100644 --- a/filter/command.go +++ b/filter/command.go @@ -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, @@ -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") } diff --git a/filter/options.go b/filter/options.go index 3cdefe8fe..7fab9009b 100644 --- a/filter/options.go +++ b/filter/options.go @@ -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"`