feat: `gum choose`, pick from a list of choices
gum choose allows the user to be prompted for a choice from a list of choices.
For example, let's ask the user to pick a card from a deck.
gum choose --height 15 {Ace,King,Queen,Jack,Ten,Nine,Eight,Seven,Six,Five,Four,Three,Two}" of "{Spades,Hearts,Clubs,Diamonds}
2022-07-11 22:17:47 +02:00
|
|
|
package choose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run provides a shell script interface for choosing between different through
|
|
|
|
// options.
|
|
|
|
func (o Options) Run() {
|
|
|
|
items := []list.Item{}
|
|
|
|
for _, option := range o.Options {
|
|
|
|
if option == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
items = append(items, item(option))
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultWidth = 20
|
|
|
|
|
|
|
|
id := itemDelegate{
|
|
|
|
indicator: o.Indicator,
|
2022-07-12 22:05:40 +02:00
|
|
|
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
|
|
|
|
selectedItemStyle: o.SelectedStyle.ToLipgloss(),
|
|
|
|
itemStyle: o.ItemStyle.ToLipgloss().MarginLeft(runewidth.StringWidth(o.Indicator)),
|
feat: `gum choose`, pick from a list of choices
gum choose allows the user to be prompted for a choice from a list of choices.
For example, let's ask the user to pick a card from a deck.
gum choose --height 15 {Ace,King,Queen,Jack,Ten,Nine,Eight,Seven,Six,Five,Four,Three,Two}" of "{Spades,Hearts,Clubs,Diamonds}
2022-07-11 22:17:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error running program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println(m.(model).choice)
|
|
|
|
}
|