gum/filter/command.go
Maas Lalani 7190822247
refactor(kong): Implement Run(...) error interface
Instead of needing to run the commands manually in main.go, we can implement the `Run(...) error` method to satisfy the command interface so that `kong` can Run our commands for us.
2022-07-12 22:33:52 -04:00

52 lines
1.1 KiB
Go

package filter
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/files"
"github.com/charmbracelet/gum/internal/stdin"
)
// 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.Placeholder = o.Placeholder
i.Width = o.Width
input, _ := stdin.Read()
var choices []string
if input != "" {
choices = strings.Split(string(input), "\n")
} else {
choices = files.List()
}
p := tea.NewProgram(model{
choices: choices,
indicator: o.Indicator,
matches: matchAll(choices),
textinput: i,
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
matchStyle: o.MatchStyle.ToLipgloss(),
textStyle: o.TextStyle.ToLipgloss(),
}, tea.WithOutput(os.Stderr))
tm, err := p.StartReturningModel()
m := tm.(model)
if len(m.matches) > m.selected && m.selected >= 0 {
fmt.Println(m.matches[m.selected].Str)
}
return err
}