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
|
|
|
|
2024-03-28 21:22:03 +03:00
|
|
|
"github.com/charmbracelet/huh"
|
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-13 05:12:02 +03:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
2022-07-13 22:49:14 +03:00
|
|
|
)
|
|
|
|
|
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 {
|
2024-03-28 21:22:03 +03:00
|
|
|
if len(o.Options) <= 0 {
|
2022-07-13 05:12:02 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-28 21:22:03 +03:00
|
|
|
theme := huh.ThemeCharm()
|
|
|
|
options := huh.NewOptions(o.Options...)
|
|
|
|
|
|
|
|
theme.Focused.Base.Border(lipgloss.Border{})
|
|
|
|
theme.Focused.Title = o.HeaderStyle.ToLipgloss()
|
|
|
|
theme.Focused.SelectSelector = o.CursorStyle.ToLipgloss().SetString(o.Cursor)
|
|
|
|
theme.Focused.MultiSelectSelector = o.CursorStyle.ToLipgloss().SetString(o.Cursor)
|
|
|
|
theme.Focused.SelectedOption = o.SelectedItemStyle.ToLipgloss()
|
|
|
|
theme.Focused.UnselectedOption = o.ItemStyle.ToLipgloss()
|
|
|
|
theme.Focused.SelectedPrefix = o.SelectedItemStyle.ToLipgloss().SetString(o.SelectedPrefix)
|
|
|
|
theme.Focused.UnselectedPrefix = o.ItemStyle.ToLipgloss().SetString(o.UnselectedPrefix)
|
|
|
|
|
|
|
|
for _, s := range o.Selected {
|
|
|
|
for i, opt := range options {
|
|
|
|
if s == opt.Key || s == opt.Value {
|
|
|
|
options[i] = opt.Selected(true)
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 18:45:52 +03:00
|
|
|
}
|
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 o.NoLimit {
|
2024-03-28 21:22:03 +03:00
|
|
|
o.Limit = len(o.Options)
|
2022-07-13 19:10:38 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 21:22:03 +03:00
|
|
|
width := max(widest(o.Options)+
|
|
|
|
max(lipgloss.Width(o.SelectedPrefix), lipgloss.Width(o.UnselectedPrefix))+
|
|
|
|
lipgloss.Width(o.Cursor)+1, lipgloss.Width(o.Header)+1)
|
|
|
|
|
|
|
|
if o.Limit > 1 {
|
|
|
|
var choices []string
|
|
|
|
err := huh.NewForm(
|
|
|
|
huh.NewGroup(
|
|
|
|
huh.NewMultiSelect[string]().
|
|
|
|
Options(options...).
|
|
|
|
Title(o.Header).
|
|
|
|
Filterable(false).
|
|
|
|
Height(o.Height).
|
|
|
|
Limit(o.Limit).
|
|
|
|
Value(&choices),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
WithWidth(width).
|
|
|
|
WithShowHelp(false).
|
|
|
|
WithTheme(theme).
|
|
|
|
Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(choices) > 0 {
|
|
|
|
s := strings.Join(choices, "\n")
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Println(s)
|
2022-10-08 01:57:26 +03:00
|
|
|
} else {
|
2024-03-28 21:22:03 +03:00
|
|
|
fmt.Print(ansi.Strip(s))
|
2022-10-02 19:51:35 +03:00
|
|
|
}
|
2022-08-08 21:55:08 +03:00
|
|
|
}
|
2024-03-28 21:22:03 +03:00
|
|
|
return nil
|
2022-08-08 21:55:08 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 21:22:03 +03:00
|
|
|
var choice string
|
|
|
|
|
|
|
|
err := huh.NewForm(
|
|
|
|
huh.NewGroup(
|
|
|
|
huh.NewSelect[string]().
|
|
|
|
Options(options...).
|
|
|
|
Title(o.Header).
|
|
|
|
Height(o.Height).
|
|
|
|
Value(&choice),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
WithWidth(width).
|
|
|
|
WithTheme(theme).
|
|
|
|
WithShowHelp(false).
|
|
|
|
Run()
|
2022-07-13 18:45:52 +03:00
|
|
|
|
2022-08-05 01:45:19 +03:00
|
|
|
if err != nil {
|
2024-03-28 21:22:03 +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
|
|
|
}
|
|
|
|
|
2023-04-07 00:31:30 +03:00
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
2024-03-28 21:22:03 +03:00
|
|
|
fmt.Println(choice)
|
2023-04-07 00:31:30 +03:00
|
|
|
} else {
|
2024-03-28 21:22:03 +03:00
|
|
|
fmt.Print(ansi.Strip(choice))
|
2023-04-07 00:31:30 +03:00
|
|
|
}
|
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
|
|
|
|
2024-03-28 21:22:03 +03:00
|
|
|
func widest(options []string) int {
|
|
|
|
var max int
|
|
|
|
for _, o := range options {
|
|
|
|
w := lipgloss.Width(o)
|
|
|
|
if w > max {
|
|
|
|
max = w
|
2022-08-08 21:55:08 +03:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 21:22:03 +03:00
|
|
|
return max
|
2022-08-08 21:55:08 +03:00
|
|
|
}
|