mirror of
https://github.com/charmbracelet/gum
synced 2024-06-01 05:12:51 +02:00
46ddc28ae5
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}
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package choose
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/mattn/go-runewidth"
|
|
)
|
|
|
|
// Run provides a shell script interface for choosing between different through
|
|
// options.
|
|
func (o Options) Run() {
|
|
items := []list.Item{}
|
|
for _, option := range o.Options {
|
|
if option == "" {
|
|
continue
|
|
}
|
|
items = append(items, item(option))
|
|
}
|
|
|
|
const defaultWidth = 20
|
|
|
|
id := itemDelegate{
|
|
indicator: o.Indicator,
|
|
indicatorStyle: lipgloss.NewStyle().Foreground(lipgloss.Color(o.IndicatorColor)),
|
|
itemStyle: lipgloss.NewStyle().Padding(0, runewidth.StringWidth(o.Indicator)).Foreground(lipgloss.Color(o.UnselectedColor)),
|
|
selectedItemStyle: lipgloss.NewStyle().Foreground(lipgloss.Color(o.SelectedColor)),
|
|
}
|
|
|
|
l := list.New(items, id, defaultWidth, o.Height)
|
|
l.SetShowTitle(false)
|
|
l.SetShowStatusBar(false)
|
|
l.SetFilteringEnabled(false)
|
|
l.SetShowHelp(false)
|
|
l.SetShowPagination(!o.HidePagination)
|
|
|
|
m, err := tea.NewProgram(model{list: l}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
|
|
if err != nil {
|
|
fmt.Println("Error running program:", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println(m.(model).choice)
|
|
}
|