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 (
|
2022-07-13 18:10:38 +02: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 22:17:47 +02:00
|
|
|
"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
|
|
|
|
2022-07-20 19:39:22 +02:00
|
|
|
"github.com/alecthomas/kong"
|
2022-07-13 21:49:14 +02: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 22:17:47 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-08-05 00:45:19 +02:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
|
2022-07-31 04:12:59 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
2022-07-13 04:12:02 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
2022-07-20 19:39:22 +02:00
|
|
|
"github.com/charmbracelet/gum/style"
|
2022-07-13 21:49:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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 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 18:10:38 +02:00
|
|
|
if input == "" {
|
|
|
|
return errors.New("no options provided, see `gum choose --help`")
|
|
|
|
}
|
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
|
|
|
// We don't need to display prefixes if we are only picking one option.
|
|
|
|
// Simply displaying the cursor is enough.
|
2022-07-13 18:10:38 +02:00
|
|
|
if o.Limit == 1 && !o.NoLimit {
|
2022-07-13 17:45:52 +02: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 22:17:47 +02:00
|
|
|
|
2022-07-13 18:10:38 +02: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-08-08 20:55:08 +02:00
|
|
|
// Keep track of the selected items.
|
|
|
|
currentSelected := 0
|
|
|
|
// Check if selected items should be used.
|
2022-10-02 18:51:35 +02:00
|
|
|
hasSelectedItems := len(o.Selected) > 0
|
|
|
|
|
|
|
|
startingIndex := 0
|
2022-08-08 20:55:08 +02:00
|
|
|
|
|
|
|
var items = make([]item, len(o.Options))
|
2022-10-02 18:51:35 +02:00
|
|
|
|
2022-08-08 20:55:08 +02:00
|
|
|
for i, option := range o.Options {
|
|
|
|
// Check if the option should be selected.
|
2022-09-06 21:02:32 +02:00
|
|
|
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
|
2022-08-08 20:55:08 +02:00
|
|
|
// If the option is selected then increment the current selected count.
|
|
|
|
if isSelected {
|
2022-10-02 18:51:35 +02:00
|
|
|
if o.Limit == 1 {
|
2022-10-08 00:57:26 +02:00
|
|
|
// When the user can choose only one option don't select the option but
|
|
|
|
// start with the cursor hovering over it.
|
2022-10-02 18:51:35 +02:00
|
|
|
startingIndex = i
|
2022-10-08 00:57:26 +02:00
|
|
|
isSelected = false
|
|
|
|
} else {
|
|
|
|
currentSelected++
|
2022-10-02 18:51:35 +02:00
|
|
|
}
|
2022-08-08 20:55:08 +02:00
|
|
|
}
|
2022-10-02 18:51:35 +02:00
|
|
|
|
2022-08-08 20:55:08 +02:00
|
|
|
items[i] = item{text: option, selected: isSelected}
|
|
|
|
}
|
|
|
|
|
2022-07-13 21:49:14 +02: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 04:12:59 +02:00
|
|
|
tm, err := tea.NewProgram(model{
|
2022-10-02 18:51:35 +02:00
|
|
|
index: startingIndex,
|
2022-07-13 17:45:52 +02:00
|
|
|
height: o.Height,
|
|
|
|
cursor: o.Cursor,
|
|
|
|
selectedPrefix: o.SelectedPrefix,
|
|
|
|
unselectedPrefix: o.UnselectedPrefix,
|
|
|
|
cursorPrefix: o.CursorPrefix,
|
|
|
|
items: items,
|
|
|
|
limit: o.Limit,
|
2022-07-13 21:49:14 +02:00
|
|
|
paginator: pager,
|
2022-07-13 17:45:52 +02:00
|
|
|
cursorStyle: o.CursorStyle.ToLipgloss(),
|
|
|
|
itemStyle: o.ItemStyle.ToLipgloss(),
|
|
|
|
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
|
2022-08-08 20:55:08 +02:00
|
|
|
numSelected: currentSelected,
|
2022-07-13 17:45:52 +02:00
|
|
|
}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
|
|
|
2022-08-05 00:45:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start tea program: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-07-31 04:12:59 +02:00
|
|
|
m := tm.(model)
|
|
|
|
if m.aborted {
|
|
|
|
return exit.ErrAborted
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:45:52 +02:00
|
|
|
var s strings.Builder
|
|
|
|
|
2022-07-31 04:12:59 +02:00
|
|
|
for _, item := range m.items {
|
2022-07-13 17:45:52 +02: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 22:17:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 20:37:26 +02:00
|
|
|
fmt.Print(s.String())
|
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-08-05 00:45:19 +02:00
|
|
|
return nil
|
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-20 19:39:22 +02:00
|
|
|
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
2022-08-05 00:45:19 +02:00
|
|
|
style.HideFlags(ctx)
|
|
|
|
return nil
|
2022-07-20 19:39:22 +02:00
|
|
|
}
|
2022-08-08 20:55:08 +02:00
|
|
|
|
|
|
|
// Check if an array contains a value.
|
|
|
|
func arrayContains(strArray []string, value string) bool {
|
|
|
|
for _, str := range strArray {
|
|
|
|
if str == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|