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"
|
|
|
|
|
|
|
|
"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"
|
2023-04-06 23:35:01 +02:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-08-08 22:54:24 +02:00
|
|
|
"github.com/sahilm/fuzzy"
|
2022-08-05 00:45:19 +02:00
|
|
|
|
2023-04-06 23:35:01 +02:00
|
|
|
"github.com/charmbracelet/gum/ansi"
|
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-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()
|
2024-01-13 21:30:15 +01:00
|
|
|
i.PlaceholderStyle = o.PlaceholderStyle.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)
|
|
|
|
|
2023-10-03 22:33:57 +02:00
|
|
|
if len(o.Options) == 0 {
|
|
|
|
if input, _ := stdin.Read(); input != "" {
|
|
|
|
o.Options = strings.Split(strings.TrimSuffix(input, "\n"), "\n")
|
|
|
|
} else {
|
|
|
|
o.Options = files.List()
|
2022-08-08 22:54:24 +02:00
|
|
|
}
|
2022-07-08 04:10:29 +02:00
|
|
|
}
|
2022-07-06 18:07:18 +02:00
|
|
|
|
2023-10-03 22:33:57 +02:00
|
|
|
if len(o.Options) == 0 {
|
2022-08-08 22:54:24 +02:00
|
|
|
return errors.New("no options provided, see `gum filter --help`")
|
|
|
|
}
|
|
|
|
|
2023-11-28 18:34:50 +01:00
|
|
|
if o.SelectIfOne && len(o.Options) == 1 {
|
2023-12-10 19:52:11 +01:00
|
|
|
fmt.Println(o.Options[0])
|
2023-11-28 18:34:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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:
|
2023-10-03 22:33:57 +02:00
|
|
|
matches = fuzzy.Find(o.Value, o.Options)
|
2023-03-07 22:59:37 +01:00
|
|
|
case o.Value != "" && !o.Fuzzy:
|
2023-10-03 22:33:57 +02:00
|
|
|
matches = exactMatches(o.Value, o.Options)
|
2023-03-07 22:59:37 +01:00
|
|
|
default:
|
2023-10-03 22:33:57 +02:00
|
|
|
matches = matchAll(o.Options)
|
2022-08-08 22:54:24 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 03:21:31 +02:00
|
|
|
if o.NoLimit {
|
2023-10-03 22:33:57 +02:00
|
|
|
o.Limit = len(o.Options)
|
2022-09-03 03:21:31 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
p := tea.NewProgram(model{
|
2023-10-03 22:33:57 +02:00
|
|
|
choices: o.Options,
|
2022-09-03 03:21:31 +02:00
|
|
|
indicator: o.Indicator,
|
|
|
|
matches: matches,
|
2023-03-14 20:58:48 +01:00
|
|
|
header: o.Header,
|
2022-09-03 03:21:31 +02:00
|
|
|
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(),
|
2023-03-14 20:58:48 +01:00
|
|
|
headerStyle: o.HeaderStyle.ToLipgloss(),
|
2022-09-03 03:21:31 +02:00
|
|
|
textStyle: o.TextStyle.ToLipgloss(),
|
2023-05-24 14:11:20 +02:00
|
|
|
cursorTextStyle: o.CursorTextStyle.ToLipgloss(),
|
2022-09-03 03:21:31 +02:00
|
|
|
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,
|
2023-06-29 23:29:46 +02:00
|
|
|
timeout: o.Timeout,
|
|
|
|
hasTimeout: o.Timeout > 0,
|
2023-05-30 16:19:53 +02:00
|
|
|
sort: o.Sort,
|
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
|
|
|
|
2023-04-06 23:35:01 +02:00
|
|
|
isTTY := isatty.IsTerminal(os.Stdout.Fd())
|
|
|
|
|
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 {
|
2023-06-29 23:29:46 +02:00
|
|
|
o.checkSelected(m, isTTY)
|
2022-09-03 03:21:31 +02:00
|
|
|
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
|
2023-04-06 23:35:01 +02:00
|
|
|
if isTTY {
|
|
|
|
fmt.Println(m.matches[m.cursor].Str)
|
|
|
|
} else {
|
|
|
|
fmt.Println(ansi.Strip(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
|
|
|
|
2023-06-29 23:29:46 +02:00
|
|
|
func (o Options) checkSelected(m model, isTTY bool) {
|
|
|
|
for k := range m.selected {
|
|
|
|
if isTTY {
|
|
|
|
fmt.Println(k)
|
|
|
|
} else {
|
|
|
|
fmt.Println(ansi.Strip(k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|