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
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// 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()
|
2022-07-13 17:45:52 +02:00
|
|
|
o.Options = strings.Split(strings.TrimSpace(input), "\n")
|
2022-07-13 04:12:02 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 17:45:52 +02:00
|
|
|
var items []item
|
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
|
|
|
for _, option := range o.Options {
|
2022-07-13 17:45:52 +02:00
|
|
|
items = append(items, item{text: option, selected: false})
|
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
|
|
|
}
|
|
|
|
|
2022-07-13 17:45:52 +02:00
|
|
|
// We don't need to display prefixes if we are only picking one option.
|
|
|
|
// Simply displaying the cursor is enough.
|
|
|
|
if o.Limit == 1 {
|
|
|
|
o.SelectedPrefix = ""
|
|
|
|
o.UnselectedPrefix = ""
|
|
|
|
o.CursorPrefix = ""
|
|
|
|
}
|
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
|
|
|
|
2022-07-13 17:45:52 +02:00
|
|
|
m, err := tea.NewProgram(model{
|
|
|
|
height: o.Height,
|
|
|
|
cursor: o.Cursor,
|
|
|
|
selectedPrefix: o.SelectedPrefix,
|
|
|
|
unselectedPrefix: o.UnselectedPrefix,
|
|
|
|
cursorPrefix: o.CursorPrefix,
|
|
|
|
items: items,
|
|
|
|
limit: o.Limit,
|
|
|
|
cursorStyle: o.CursorStyle.ToLipgloss(),
|
|
|
|
itemStyle: o.ItemStyle.ToLipgloss(),
|
|
|
|
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
|
|
|
|
}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
|
|
|
|
|
|
var s strings.Builder
|
|
|
|
|
|
|
|
for _, item := range m.(model).items {
|
|
|
|
if item.selected {
|
|
|
|
s.WriteString(item.text)
|
|
|
|
s.WriteRune('\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
|
|
|
}
|
|
|
|
|
2022-07-13 17:45:52 +02:00
|
|
|
fmt.Println(strings.TrimSuffix(s.String(), "\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
|
|
|
|
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
|
|
|
}
|