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"
|
2023-01-12 19:36:11 +01:00
|
|
|
"sort"
|
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-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"
|
2023-04-06 23:31:30 +02:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-08-05 00:45:19 +02:00
|
|
|
|
2023-04-06 23:31:30 +02:00
|
|
|
"github.com/charmbracelet/gum/ansi"
|
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-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-12-13 21:41:01 +01:00
|
|
|
o.Options = strings.Split(strings.TrimSuffix(input, "\n"), "\n")
|
2022-07-13 04:12:02 +02:00
|
|
|
}
|
|
|
|
|
2023-11-28 18:34:50 +01:00
|
|
|
if o.SelectIfOne && len(o.Options) == 1 {
|
|
|
|
print(o.Options[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-07-25 18:38:55 +02:00
|
|
|
o.Limit = len(o.Options) + 1
|
2022-07-13 18:10:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 06:43:55 +02:00
|
|
|
if len(o.Selected) > o.Limit {
|
|
|
|
return errors.New("number of selected options cannot be greater than the limit")
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-12 19:36:11 +01:00
|
|
|
currentOrder := 0
|
2022-08-08 20:55:08 +02:00
|
|
|
|
2023-01-12 19:36:11 +01:00
|
|
|
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 {
|
2023-01-12 19:36:11 +01:00
|
|
|
var order int
|
2022-08-08 20:55:08 +02:00
|
|
|
// 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++
|
2023-01-12 19:36:11 +01:00
|
|
|
order = currentOrder
|
|
|
|
currentOrder++
|
2022-10-02 18:51:35 +02:00
|
|
|
}
|
2022-08-08 20:55:08 +02:00
|
|
|
}
|
2022-10-02 18:51:35 +02:00
|
|
|
|
2023-01-12 19:36:11 +01:00
|
|
|
items[i] = item{text: option, selected: isSelected, order: order}
|
2022-08-08 20:55:08 +02:00
|
|
|
}
|
|
|
|
|
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("•")
|
2023-03-03 19:15:50 +01:00
|
|
|
pager.KeyMap = paginator.KeyMap{}
|
2023-07-25 18:34:00 +02:00
|
|
|
pager.Page = startingIndex / o.Height
|
2022-07-13 21:49:14 +02:00
|
|
|
|
|
|
|
// Disable Keybindings since we will control it ourselves.
|
2022-07-31 04:12:59 +02:00
|
|
|
tm, err := tea.NewProgram(model{
|
2022-10-02 18:51:35 +02:00
|
|
|
index: startingIndex,
|
2023-01-12 19:36:11 +01:00
|
|
|
currentOrder: currentOrder,
|
2022-07-13 17:45:52 +02:00
|
|
|
height: o.Height,
|
|
|
|
cursor: o.Cursor,
|
2023-03-14 20:58:48 +01:00
|
|
|
header: o.Header,
|
2022-07-13 17:45:52 +02:00
|
|
|
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(),
|
2023-03-14 20:58:48 +01:00
|
|
|
headerStyle: o.HeaderStyle.ToLipgloss(),
|
2022-07-13 17:45:52 +02:00
|
|
|
itemStyle: o.ItemStyle.ToLipgloss(),
|
|
|
|
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
|
2022-08-08 20:55:08 +02:00
|
|
|
numSelected: currentSelected,
|
2023-06-30 15:28:46 +02:00
|
|
|
hasTimeout: o.Timeout > 0,
|
|
|
|
timeout: o.Timeout,
|
2022-10-18 02:23:59 +02:00
|
|
|
}, tea.WithOutput(os.Stderr)).Run()
|
2022-07-13 17:45:52 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-12 19:36:11 +01:00
|
|
|
if o.Ordered && o.Limit > 1 {
|
|
|
|
sort.Slice(m.items, func(i, j int) bool {
|
|
|
|
return m.items[i].order < m.items[j].order
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-06 23:31:30 +02:00
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Print(s.String())
|
|
|
|
} else {
|
|
|
|
fmt.Print(ansi.Strip(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
|
|
|
|
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
|
|
|
|
}
|