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"
|
2022-07-13 04:12:02 +02:00
|
|
|
"strings"
|
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
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-07-13 04:12:02 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
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
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run provides a shell script interface for choosing between different through
|
|
|
|
// options.
|
2022-07-13 04:12:02 +02:00
|
|
|
func (o Options) Run() error {
|
|
|
|
if len(o.Options) == 0 {
|
|
|
|
input, _ := stdin.Read()
|
|
|
|
o.Options = strings.Split(input, "\n")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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()
|
|
|
|
fmt.Println(m.(model).choice)
|
2022-07-13 04:12:02 +02:00
|
|
|
return err
|
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
|
|
|
}
|