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"
|
2023-01-12 21:36:11 +03:00
|
|
|
"sort"
|
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-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-08-05 01:45:19 +03:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2023-04-07 00:31:30 +03:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-08-05 01:45:19 +03:00
|
|
|
|
2023-04-07 00:31:30 +03:00
|
|
|
"github.com/charmbracelet/gum/ansi"
|
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-13 22:49:14 +03: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 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-12-13 23:41:01 +03:00
|
|
|
o.Options = strings.Split(strings.TrimSuffix(input, "\n"), "\n")
|
2022-07-13 05:12:02 +03:00
|
|
|
}
|
|
|
|
|
2023-11-28 20:34:50 +03:00
|
|
|
if o.SelectIfOne && len(o.Options) == 1 {
|
2023-12-10 21:52:11 +03:00
|
|
|
fmt.Println(o.Options[0])
|
2023-11-28 20:34:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-07-25 19:38:55 +03:00
|
|
|
o.Limit = len(o.Options) + 1
|
2022-07-13 19:10:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-08 07:43:55 +03:00
|
|
|
if len(o.Selected) > o.Limit {
|
|
|
|
return errors.New("number of selected options cannot be greater than the limit")
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:55:08 +03:00
|
|
|
// Keep track of the selected items.
|
|
|
|
currentSelected := 0
|
|
|
|
// Check if selected items should be used.
|
2022-10-02 19:51:35 +03:00
|
|
|
hasSelectedItems := len(o.Selected) > 0
|
|
|
|
|
|
|
|
startingIndex := 0
|
2023-01-12 21:36:11 +03:00
|
|
|
currentOrder := 0
|
2022-08-08 21:55:08 +03:00
|
|
|
|
2023-01-12 21:36:11 +03:00
|
|
|
items := make([]item, len(o.Options))
|
2022-10-02 19:51:35 +03:00
|
|
|
|
2022-08-08 21:55:08 +03:00
|
|
|
for i, option := range o.Options {
|
2023-01-12 21:36:11 +03:00
|
|
|
var order int
|
2022-08-08 21:55:08 +03:00
|
|
|
// Check if the option should be selected.
|
2022-09-06 22:02:32 +03:00
|
|
|
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
|
2022-08-08 21:55:08 +03:00
|
|
|
// If the option is selected then increment the current selected count.
|
|
|
|
if isSelected {
|
2022-10-02 19:51:35 +03:00
|
|
|
if o.Limit == 1 {
|
2022-10-08 01:57:26 +03: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 19:51:35 +03:00
|
|
|
startingIndex = i
|
2022-10-08 01:57:26 +03:00
|
|
|
isSelected = false
|
|
|
|
} else {
|
|
|
|
currentSelected++
|
2023-01-12 21:36:11 +03:00
|
|
|
order = currentOrder
|
|
|
|
currentOrder++
|
2022-10-02 19:51:35 +03:00
|
|
|
}
|
2022-08-08 21:55:08 +03:00
|
|
|
}
|
2022-10-02 19:51:35 +03:00
|
|
|
|
2023-01-12 21:36:11 +03:00
|
|
|
items[i] = item{text: option, selected: isSelected, order: order}
|
2022-08-08 21:55:08 +03:00
|
|
|
}
|
|
|
|
|
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("•")
|
2023-03-03 21:15:50 +03:00
|
|
|
pager.KeyMap = paginator.KeyMap{}
|
2023-07-25 19:34:00 +03:00
|
|
|
pager.Page = startingIndex / o.Height
|
2022-07-13 22:49:14 +03:00
|
|
|
|
|
|
|
// Disable Keybindings since we will control it ourselves.
|
2022-07-31 05:12:59 +03:00
|
|
|
tm, err := tea.NewProgram(model{
|
2022-10-02 19:51:35 +03:00
|
|
|
index: startingIndex,
|
2023-01-12 21:36:11 +03:00
|
|
|
currentOrder: currentOrder,
|
2022-07-13 18:45:52 +03:00
|
|
|
height: o.Height,
|
|
|
|
cursor: o.Cursor,
|
2023-03-14 22:58:48 +03:00
|
|
|
header: o.Header,
|
2022-07-13 18:45:52 +03:00
|
|
|
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(),
|
2023-03-14 22:58:48 +03:00
|
|
|
headerStyle: o.HeaderStyle.ToLipgloss(),
|
2022-07-13 18:45:52 +03:00
|
|
|
itemStyle: o.ItemStyle.ToLipgloss(),
|
|
|
|
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
|
2022-08-08 21:55:08 +03:00
|
|
|
numSelected: currentSelected,
|
2023-06-30 16:28:46 +03:00
|
|
|
hasTimeout: o.Timeout > 0,
|
|
|
|
timeout: o.Timeout,
|
2022-10-18 03:23:59 +03:00
|
|
|
}, tea.WithOutput(os.Stderr)).Run()
|
2022-07-13 18:45:52 +03:00
|
|
|
|
2022-08-05 01:45:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start tea program: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-07-31 05:12:59 +03:00
|
|
|
m := tm.(model)
|
|
|
|
if m.aborted {
|
|
|
|
return exit.ErrAborted
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:36:11 +03: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 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
|
|
|
}
|
|
|
|
|
2023-04-07 00:31:30 +03: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 23:17:47 +03:00
|
|
|
|
2022-08-05 01:45:19 +03: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 23:17:47 +03:00
|
|
|
}
|
2022-07-20 20:39:22 +03:00
|
|
|
|
2022-08-08 21:55:08 +03: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
|
|
|
|
}
|