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 23:17:47 +03:00
|
|
|
package choose
|
|
|
|
|
|
|
|
import (
|
2022-07-13 19:10:38 +03:00
|
|
|
"errors"
|
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 23:17:47 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-07-13 05:12:02 +03: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 23:17:47 +03:00
|
|
|
|
2022-07-20 20:39:22 +03:00
|
|
|
"github.com/alecthomas/kong"
|
2022-07-13 22:49:14 +03:00
|
|
|
"github.com/charmbracelet/bubbles/paginator"
|
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 23:17:47 +03:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-07-31 05:12:59 +03:00
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
2022-07-13 05:12:02 +03:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
2022-07-20 20:39:22 +03:00
|
|
|
"github.com/charmbracelet/gum/style"
|
2022-07-13 22:49:14 +03:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
subduedStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"})
|
|
|
|
verySubduedStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"})
|
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 23:17:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run provides a shell script interface for choosing between different through
|
|
|
|
// options.
|
2022-07-13 05:12:02 +03:00
|
|
|
func (o Options) Run() error {
|
|
|
|
if len(o.Options) == 0 {
|
|
|
|
input, _ := stdin.Read()
|
2022-07-13 19:10:38 +03:00
|
|
|
if input == "" {
|
|
|
|
return errors.New("no options provided, see `gum choose --help`")
|
|
|
|
}
|
2022-07-13 18:45:52 +03:00
|
|
|
o.Options = strings.Split(strings.TrimSpace(input), "\n")
|
2022-07-13 05:12:02 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 18:45:52 +03: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 23:17:47 +03:00
|
|
|
for _, option := range o.Options {
|
2022-07-13 18:45:52 +03: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 23:17:47 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 18:45:52 +03:00
|
|
|
// We don't need to display prefixes if we are only picking one option.
|
|
|
|
// Simply displaying the cursor is enough.
|
2022-07-13 19:10:38 +03:00
|
|
|
if o.Limit == 1 && !o.NoLimit {
|
2022-07-13 18:45:52 +03:00
|
|
|
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 23:17:47 +03:00
|
|
|
|
2022-07-13 19:10:38 +03:00
|
|
|
// If we've set no limit then we can simply select as many options as there
|
|
|
|
// are so let's set the limit to the number of options.
|
|
|
|
if o.NoLimit {
|
|
|
|
o.Limit = len(o.Options)
|
|
|
|
}
|
|
|
|
|
2022-07-13 22:49:14 +03:00
|
|
|
// Use the pagination model to display the current and total number of
|
|
|
|
// pages.
|
|
|
|
pager := paginator.New()
|
|
|
|
pager.SetTotalPages((len(items) + o.Height - 1) / o.Height)
|
|
|
|
pager.PerPage = o.Height
|
|
|
|
pager.Type = paginator.Dots
|
|
|
|
pager.ActiveDot = subduedStyle.Render("•")
|
|
|
|
pager.InactiveDot = verySubduedStyle.Render("•")
|
|
|
|
|
|
|
|
// Disable Keybindings since we will control it ourselves.
|
|
|
|
pager.UseHLKeys = false
|
|
|
|
pager.UseLeftRightKeys = false
|
|
|
|
pager.UseJKKeys = false
|
|
|
|
pager.UsePgUpPgDownKeys = false
|
|
|
|
|
2022-07-31 05:12:59 +03:00
|
|
|
tm, err := tea.NewProgram(model{
|
2022-07-13 18:45:52 +03:00
|
|
|
height: o.Height,
|
|
|
|
cursor: o.Cursor,
|
|
|
|
selectedPrefix: o.SelectedPrefix,
|
|
|
|
unselectedPrefix: o.UnselectedPrefix,
|
|
|
|
cursorPrefix: o.CursorPrefix,
|
|
|
|
items: items,
|
|
|
|
limit: o.Limit,
|
2022-07-13 22:49:14 +03:00
|
|
|
paginator: pager,
|
2022-07-13 18:45:52 +03:00
|
|
|
cursorStyle: o.CursorStyle.ToLipgloss(),
|
|
|
|
itemStyle: o.ItemStyle.ToLipgloss(),
|
|
|
|
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
|
|
|
|
}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
|
|
|
2022-07-31 05:12:59 +03:00
|
|
|
m := tm.(model)
|
|
|
|
if m.aborted {
|
|
|
|
return exit.ErrAborted
|
|
|
|
}
|
|
|
|
|
2022-07-13 18:45:52 +03:00
|
|
|
var s strings.Builder
|
|
|
|
|
2022-07-31 05:12:59 +03:00
|
|
|
for _, item := range m.items {
|
2022-07-13 18:45:52 +03:00
|
|
|
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 23:17:47 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 18:45:52 +03: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 23:17:47 +03:00
|
|
|
|
2022-07-13 05:12:02 +03: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 23:17:47 +03:00
|
|
|
}
|
2022-07-20 20:39:22 +03:00
|
|
|
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
|
|
|
return style.HideFlags(ctx)
|
|
|
|
}
|