diff --git a/filter/command.go b/filter/command.go index 746c504..8a00b7d 100644 --- a/filter/command.go +++ b/filter/command.go @@ -1,6 +1,7 @@ package filter import ( + "errors" "fmt" "os" "strings" @@ -9,6 +10,7 @@ import ( "github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" + "github.com/sahilm/fuzzy" "github.com/charmbracelet/gum/internal/exit" "github.com/charmbracelet/gum/internal/files" @@ -31,20 +33,35 @@ func (o Options) Run() error { var choices []string if input, _ := stdin.Read(); input != "" { - choices = strings.Split(strings.TrimSpace(input), "\n") + input = strings.TrimSpace(input) + if input != "" { + choices = strings.Split(input, "\n") + } } else { choices = files.List() } + if len(choices) == 0 { + return errors.New("no options provided, see `gum filter --help`") + } + options := []tea.ProgramOption{tea.WithOutput(os.Stderr)} if o.Height == 0 { options = append(options, tea.WithAltScreen()) } + var matches []fuzzy.Match + if o.Value != "" { + i.SetValue(o.Value) + matches = fuzzy.Find(o.Value, choices) + } else { + matches = matchAll(choices) + } + p := tea.NewProgram(model{ choices: choices, indicator: o.Indicator, - matches: matchAll(choices), + matches: matches, textinput: i, viewport: &v, indicatorStyle: o.IndicatorStyle.ToLipgloss(), diff --git a/filter/options.go b/filter/options.go index 0bd79d9..5173c84 100644 --- a/filter/options.go +++ b/filter/options.go @@ -13,4 +13,5 @@ type Options struct { PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=240" envprefix:"GUM_FILTER_PROMPT_"` Width int `help:"Input width" default:"20" env:"GUM_FILTER_WIDTH"` Height int `help:"Input height" default:"0" env:"GUM_FILTER_HEIGHT"` + Value string `help:"Initial filter value" default:"" env:"GUM_FILTER_VALUE"` }