mirror of
https://github.com/charmbracelet/gum
synced 2026-03-14 21:55:45 +01:00
152 lines
3.8 KiB
Go
152 lines
3.8 KiB
Go
package filter
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/gum/internal/files"
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
|
"github.com/charmbracelet/gum/internal/timeout"
|
|
"github.com/charmbracelet/x/ansi"
|
|
"github.com/charmbracelet/x/term"
|
|
"github.com/sahilm/fuzzy"
|
|
)
|
|
|
|
// Run provides a shell script interface for filtering through options, powered
|
|
// by the textinput bubble.
|
|
func (o Options) Run() error {
|
|
i := textinput.New()
|
|
i.Focus()
|
|
|
|
i.Prompt = o.Prompt
|
|
i.PromptStyle = o.PromptStyle.ToLipgloss()
|
|
i.PlaceholderStyle = o.PlaceholderStyle.ToLipgloss()
|
|
i.Placeholder = o.Placeholder
|
|
i.Width = o.Width
|
|
|
|
v := viewport.New(o.Width, o.Height)
|
|
|
|
if len(o.Options) == 0 {
|
|
if input, _ := stdin.ReadStrip(); input != "" {
|
|
o.Options = strings.Split(input, "\n")
|
|
} else {
|
|
o.Options = files.List()
|
|
}
|
|
}
|
|
|
|
if len(o.Options) == 0 {
|
|
return errors.New("no options provided, see `gum filter --help`")
|
|
}
|
|
|
|
if o.SelectIfOne && len(o.Options) == 1 {
|
|
fmt.Println(o.Options[0])
|
|
return nil
|
|
}
|
|
|
|
ctx, cancel := timeout.Context(o.Timeout)
|
|
defer cancel()
|
|
|
|
options := []tea.ProgramOption{
|
|
tea.WithOutput(os.Stderr),
|
|
tea.WithReportFocus(),
|
|
tea.WithContext(ctx),
|
|
}
|
|
if o.Height == 0 {
|
|
options = append(options, tea.WithAltScreen())
|
|
}
|
|
|
|
var matches []fuzzy.Match
|
|
if o.Value != "" {
|
|
i.SetValue(o.Value)
|
|
}
|
|
switch {
|
|
case o.Value != "" && o.Fuzzy:
|
|
matches = fuzzy.Find(o.Value, o.Options)
|
|
case o.Value != "" && !o.Fuzzy:
|
|
matches = exactMatches(o.Value, o.Options)
|
|
default:
|
|
matches = matchAll(o.Options)
|
|
}
|
|
|
|
km := defaultKeymap()
|
|
|
|
if o.NoLimit {
|
|
o.Limit = len(o.Options)
|
|
}
|
|
if o.NoLimit || o.Limit > 1 {
|
|
km.Toggle.SetEnabled(true)
|
|
km.ToggleAndPrevious.SetEnabled(true)
|
|
km.ToggleAndNext.SetEnabled(true)
|
|
km.ToggleAll.SetEnabled(true)
|
|
}
|
|
|
|
p := tea.NewProgram(model{
|
|
choices: o.Options,
|
|
indicator: o.Indicator,
|
|
matches: matches,
|
|
header: o.Header,
|
|
textinput: i,
|
|
viewport: &v,
|
|
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
|
|
selectedPrefixStyle: o.SelectedPrefixStyle.ToLipgloss(),
|
|
selectedPrefix: o.SelectedPrefix,
|
|
unselectedPrefixStyle: o.UnselectedPrefixStyle.ToLipgloss(),
|
|
unselectedPrefix: o.UnselectedPrefix,
|
|
matchStyle: o.MatchStyle.ToLipgloss(),
|
|
headerStyle: o.HeaderStyle.ToLipgloss(),
|
|
textStyle: o.TextStyle.ToLipgloss(),
|
|
cursorTextStyle: o.CursorTextStyle.ToLipgloss(),
|
|
height: o.Height,
|
|
selected: make(map[string]struct{}),
|
|
limit: o.Limit,
|
|
reverse: o.Reverse,
|
|
fuzzy: o.Fuzzy,
|
|
sort: o.Sort && o.FuzzySort,
|
|
strict: o.Strict,
|
|
showHelp: o.ShowHelp,
|
|
keymap: km,
|
|
help: help.New(),
|
|
}, options...)
|
|
|
|
tm, err := p.Run()
|
|
if err != nil {
|
|
return fmt.Errorf("unable to run filter: %w", err)
|
|
}
|
|
|
|
m := tm.(model)
|
|
if !m.submitted {
|
|
return errors.New("nothing selected")
|
|
}
|
|
isTTY := term.IsTerminal(os.Stdout.Fd())
|
|
|
|
// allSelections contains values only if limit is greater
|
|
// than 1 or if flag --no-limit is passed, hence there is
|
|
// no need to further checks
|
|
if len(m.selected) > 0 {
|
|
o.checkSelected(m, isTTY)
|
|
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
|
|
if isTTY {
|
|
fmt.Println(m.matches[m.cursor].Str)
|
|
} else {
|
|
fmt.Println(ansi.Strip(m.matches[m.cursor].Str))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o Options) checkSelected(m model, isTTY bool) {
|
|
for k := range m.selected {
|
|
if isTTY {
|
|
fmt.Println(k)
|
|
} else {
|
|
fmt.Println(ansi.Strip(k))
|
|
}
|
|
}
|
|
}
|