2022-07-08 19:58:14 +02:00
|
|
|
package filter
|
2022-07-06 18:07:18 +02:00
|
|
|
|
|
|
|
import (
|
2022-08-08 22:54:24 +02:00
|
|
|
"errors"
|
2022-07-06 18:07:18 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-20 19:39:22 +02:00
|
|
|
"github.com/alecthomas/kong"
|
2022-07-06 18:07:18 +02:00
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
2022-08-02 21:18:22 +02:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2022-07-06 18:07:18 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-08-08 22:54:24 +02:00
|
|
|
"github.com/sahilm/fuzzy"
|
2022-08-05 00:45:19 +02:00
|
|
|
|
2022-07-31 03:21:00 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
2022-07-08 04:10:29 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/files"
|
2022-07-07 19:26:35 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
2022-07-20 19:39:22 +02:00
|
|
|
"github.com/charmbracelet/gum/style"
|
2022-07-06 18:07:18 +02:00
|
|
|
)
|
|
|
|
|
2022-07-08 19:58:14 +02:00
|
|
|
// Run provides a shell script interface for filtering through options, powered
|
|
|
|
// by the textinput bubble.
|
2022-07-13 04:12:02 +02:00
|
|
|
func (o Options) Run() error {
|
2022-07-06 18:07:18 +02:00
|
|
|
i := textinput.New()
|
|
|
|
i.Focus()
|
|
|
|
|
|
|
|
i.Prompt = o.Prompt
|
2022-07-12 22:05:40 +02:00
|
|
|
i.PromptStyle = o.PromptStyle.ToLipgloss()
|
2022-07-06 18:07:18 +02:00
|
|
|
i.Placeholder = o.Placeholder
|
|
|
|
i.Width = o.Width
|
|
|
|
|
2022-08-02 21:18:22 +02:00
|
|
|
v := viewport.New(o.Width, o.Height)
|
|
|
|
|
2022-07-08 04:10:29 +02:00
|
|
|
var choices []string
|
2022-07-30 23:40:33 +02:00
|
|
|
if input, _ := stdin.Read(); input != "" {
|
2022-12-13 21:41:01 +01:00
|
|
|
input = strings.TrimSuffix(input, "\n")
|
2022-08-08 22:54:24 +02:00
|
|
|
if input != "" {
|
|
|
|
choices = strings.Split(input, "\n")
|
|
|
|
}
|
2022-07-08 04:10:29 +02:00
|
|
|
} else {
|
|
|
|
choices = files.List()
|
|
|
|
}
|
2022-07-06 18:07:18 +02:00
|
|
|
|
2022-08-08 22:54:24 +02:00
|
|
|
if len(choices) == 0 {
|
|
|
|
return errors.New("no options provided, see `gum filter --help`")
|
|
|
|
}
|
|
|
|
|
2022-08-04 17:21:51 +02:00
|
|
|
options := []tea.ProgramOption{tea.WithOutput(os.Stderr)}
|
|
|
|
if o.Height == 0 {
|
|
|
|
options = append(options, tea.WithAltScreen())
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:54:24 +02:00
|
|
|
var matches []fuzzy.Match
|
|
|
|
if o.Value != "" {
|
|
|
|
i.SetValue(o.Value)
|
2023-03-07 22:59:37 +01:00
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case o.Value != "" && o.Fuzzy:
|
2022-08-08 22:54:24 +02:00
|
|
|
matches = fuzzy.Find(o.Value, choices)
|
2023-03-07 22:59:37 +01:00
|
|
|
case o.Value != "" && !o.Fuzzy:
|
|
|
|
matches = exactMatches(o.Value, choices)
|
|
|
|
default:
|
2022-08-08 22:54:24 +02:00
|
|
|
matches = matchAll(choices)
|
|
|
|
}
|
|
|
|
|
2022-09-03 03:21:31 +02:00
|
|
|
if o.NoLimit {
|
|
|
|
o.Limit = len(choices)
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
p := tea.NewProgram(model{
|
2022-09-03 03:21:31 +02:00
|
|
|
choices: choices,
|
|
|
|
indicator: o.Indicator,
|
|
|
|
matches: matches,
|
|
|
|
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(),
|
|
|
|
textStyle: o.TextStyle.ToLipgloss(),
|
|
|
|
height: o.Height,
|
|
|
|
selected: make(map[string]struct{}),
|
|
|
|
limit: o.Limit,
|
2022-10-07 21:23:28 +02:00
|
|
|
reverse: o.Reverse,
|
2022-10-13 16:47:27 +02:00
|
|
|
fuzzy: o.Fuzzy,
|
2022-08-04 17:21:51 +02:00
|
|
|
}, options...)
|
2022-08-04 17:15:21 +02:00
|
|
|
|
2022-10-18 02:23:59 +02:00
|
|
|
tm, err := p.Run()
|
2022-08-05 00:45:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to run filter: %w", err)
|
|
|
|
}
|
2022-07-06 18:07:18 +02:00
|
|
|
m := tm.(model)
|
2022-07-31 03:10:36 +02:00
|
|
|
if m.aborted {
|
2022-07-31 03:41:18 +02:00
|
|
|
return exit.ErrAborted
|
2022-07-31 03:10:36 +02:00
|
|
|
}
|
2022-09-03 03:21:31 +02:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
for k := range m.selected {
|
|
|
|
fmt.Println(k)
|
|
|
|
}
|
|
|
|
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
|
|
|
|
fmt.Println(m.matches[m.cursor].Str)
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
2022-07-13 04:12:02 +02:00
|
|
|
|
2022-11-09 19:54:47 +01:00
|
|
|
if !o.Strict && len(m.textinput.Value()) != 0 && len(m.matches) == 0 {
|
|
|
|
fmt.Println(m.textinput.Value())
|
|
|
|
}
|
2022-08-05 00:45:19 +02:00
|
|
|
return nil
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
2022-07-20 19:39:22 +02:00
|
|
|
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
2022-08-05 00:45:19 +02:00
|
|
|
style.HideFlags(ctx)
|
|
|
|
return nil
|
2022-07-20 19:39:22 +02:00
|
|
|
}
|