mirror of
https://github.com/charmbracelet/gum
synced 2024-06-08 08:42:16 +02:00
7190822247
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.
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package choose
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
|
"github.com/mattn/go-runewidth"
|
|
)
|
|
|
|
// Run provides a shell script interface for choosing between different through
|
|
// options.
|
|
func (o Options) Run() error {
|
|
if len(o.Options) == 0 {
|
|
input, _ := stdin.Read()
|
|
o.Options = strings.Split(input, "\n")
|
|
}
|
|
|
|
items := []list.Item{}
|
|
for _, option := range o.Options {
|
|
if option == "" {
|
|
continue
|
|
}
|
|
items = append(items, item(option))
|
|
}
|
|
|
|
const defaultWidth = 20
|
|
|
|
id := itemDelegate{
|
|
indicator: o.Indicator,
|
|
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
|
|
selectedItemStyle: o.SelectedStyle.ToLipgloss(),
|
|
itemStyle: o.ItemStyle.ToLipgloss().MarginLeft(runewidth.StringWidth(o.Indicator)),
|
|
}
|
|
|
|
l := list.New(items, id, defaultWidth, o.Height)
|
|
l.SetShowTitle(false)
|
|
l.SetShowStatusBar(false)
|
|
l.SetFilteringEnabled(false)
|
|
l.SetShowHelp(false)
|
|
l.SetShowPagination(!o.HidePagination)
|
|
|
|
m, err := tea.NewProgram(model{list: l}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
fmt.Println(m.(model).choice)
|
|
return err
|
|
}
|