mirror of
https://github.com/charmbracelet/gum.git
synced 2024-09-11 06:16:03 +03:00
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
|
|
}
|