mirror of
https://github.com/charmbracelet/gum
synced 2024-06-05 23:32:19 +02:00
Allow --value
to pre-populate gum filter
(#113)
* Add --value option to filter command Modeled after fzf's --query option this allows the inital model to be prefiltered. Additionally, if the initial list, before being filtered by --value, is empty then treat this as an error. Filtering an empty list doesn't make sense. Co-authored-by: David Morgan <david_a_morgan@icloud.com>
This commit is contained in:
parent
c1a1a73a0e
commit
9b3c8c0db0
|
@ -1,6 +1,7 @@
|
||||||
package filter
|
package filter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/sahilm/fuzzy"
|
||||||
|
|
||||||
"github.com/charmbracelet/gum/internal/exit"
|
"github.com/charmbracelet/gum/internal/exit"
|
||||||
"github.com/charmbracelet/gum/internal/files"
|
"github.com/charmbracelet/gum/internal/files"
|
||||||
|
@ -31,20 +33,35 @@ func (o Options) Run() error {
|
||||||
|
|
||||||
var choices []string
|
var choices []string
|
||||||
if input, _ := stdin.Read(); input != "" {
|
if input, _ := stdin.Read(); input != "" {
|
||||||
choices = strings.Split(strings.TrimSpace(input), "\n")
|
input = strings.TrimSpace(input)
|
||||||
|
if input != "" {
|
||||||
|
choices = strings.Split(input, "\n")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
choices = files.List()
|
choices = files.List()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(choices) == 0 {
|
||||||
|
return errors.New("no options provided, see `gum filter --help`")
|
||||||
|
}
|
||||||
|
|
||||||
options := []tea.ProgramOption{tea.WithOutput(os.Stderr)}
|
options := []tea.ProgramOption{tea.WithOutput(os.Stderr)}
|
||||||
if o.Height == 0 {
|
if o.Height == 0 {
|
||||||
options = append(options, tea.WithAltScreen())
|
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{
|
p := tea.NewProgram(model{
|
||||||
choices: choices,
|
choices: choices,
|
||||||
indicator: o.Indicator,
|
indicator: o.Indicator,
|
||||||
matches: matchAll(choices),
|
matches: matches,
|
||||||
textinput: i,
|
textinput: i,
|
||||||
viewport: &v,
|
viewport: &v,
|
||||||
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
|
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
|
||||||
|
|
|
@ -13,4 +13,5 @@ type Options struct {
|
||||||
PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=240" envprefix:"GUM_FILTER_PROMPT_"`
|
PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=240" envprefix:"GUM_FILTER_PROMPT_"`
|
||||||
Width int `help:"Input width" default:"20" env:"GUM_FILTER_WIDTH"`
|
Width int `help:"Input width" default:"20" env:"GUM_FILTER_WIDTH"`
|
||||||
Height int `help:"Input height" default:"0" env:"GUM_FILTER_HEIGHT"`
|
Height int `help:"Input height" default:"0" env:"GUM_FILTER_HEIGHT"`
|
||||||
|
Value string `help:"Initial filter value" default:"" env:"GUM_FILTER_VALUE"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue