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-08-08 22:54:24 +02:00
|
|
|
input = strings.TrimSpace(input)
|
|
|
|
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)
|
|
|
|
matches = fuzzy.Find(o.Value, choices)
|
|
|
|
} else {
|
|
|
|
matches = matchAll(choices)
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
p := tea.NewProgram(model{
|
2022-07-07 23:46:22 +02:00
|
|
|
choices: choices,
|
|
|
|
indicator: o.Indicator,
|
2022-08-08 22:54:24 +02:00
|
|
|
matches: matches,
|
2022-07-12 22:05:40 +02:00
|
|
|
textinput: i,
|
2022-08-02 21:18:22 +02:00
|
|
|
viewport: &v,
|
2022-07-12 22:05:40 +02:00
|
|
|
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
|
|
|
|
matchStyle: o.MatchStyle.ToLipgloss(),
|
|
|
|
textStyle: o.TextStyle.ToLipgloss(),
|
2022-08-02 21:18:22 +02:00
|
|
|
height: o.Height,
|
2022-08-04 17:21:51 +02:00
|
|
|
}, options...)
|
2022-08-04 17:15:21 +02:00
|
|
|
|
2022-07-13 04:12:02 +02:00
|
|
|
tm, err := p.StartReturningModel()
|
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-07-06 18:07:18 +02:00
|
|
|
if len(m.matches) > m.selected && m.selected >= 0 {
|
|
|
|
fmt.Println(m.matches[m.selected].Str)
|
|
|
|
}
|
2022-07-13 04:12:02 +02:00
|
|
|
|
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
|
|
|
}
|