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
|
|
|
)
|
|
|
|
|
2024-05-25 21:51:20 +03:00
|
|
|
const widthBuffer = 2
|
|
|
|
|
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`")
|
|
|
|
}
|
2024-05-25 21:44:01 +03:00
|
|
|
o.Options = strings.Split(input, "\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...)
|
|
|
|
|
2024-06-02 14:51:03 +03:00
|
|
|
theme.Focused.Base = lipgloss.NewStyle()
|
2024-03-28 21:22:03 +03:00
|
|
|
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)+
|
2024-05-25 21:51:20 +03:00
|
|
|
max(lipgloss.Width(o.SelectedPrefix)+lipgloss.Width(o.UnselectedPrefix))+
|
|
|
|
lipgloss.Width(o.Cursor)+1, lipgloss.Width(o.Header)+widthBuffer)
|
2024-03-28 21:22:03 +03:00
|
|
|
|
|
|
|
if o.Limit > 1 {
|
|
|
|
var choices []string
|
2024-05-25 21:51:20 +03:00
|
|
|
|
|
|
|
field := huh.NewMultiSelect[string]().
|
|
|
|
Options(options...).
|
|
|
|
Title(o.Header).
|
|
|
|
Height(o.Height).
|
|
|
|
Limit(o.Limit).
|
|
|
|
Value(&choices)
|
|
|
|
|
|
|
|
form := huh.NewForm(huh.NewGroup(field))
|
|
|
|
|
|
|
|
err := form.
|
2024-03-28 21:22:03 +03:00
|
|
|
WithWidth(width).
|
2024-05-25 21:51:20 +03:00
|
|
|
WithShowHelp(o.ShowHelp).
|
2024-03-28 21:22:03 +03:00
|
|
|
WithTheme(theme).
|
|
|
|
Run()
|
2024-05-25 21:51:20 +03:00
|
|
|
|
2024-03-28 21:22:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(choices) > 0 {
|
|
|
|
s := strings.Join(choices, "\n")
|
2024-05-25 21:51:20 +03:00
|
|
|
ansiprint(s)
|
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).
|
2024-05-25 21:51:20 +03:00
|
|
|
WithShowHelp(o.ShowHelp).
|
2024-03-28 21:22:03 +03:00
|
|
|
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
|
|
|
}
|
2024-05-25 21:51:20 +03:00
|
|
|
|
|
|
|
func ansiprint(s string) {
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Println(s)
|
|
|
|
} else {
|
|
|
|
fmt.Print(ansi.Strip(s))
|
|
|
|
}
|
|
|
|
}
|