mirror of
https://github.com/charmbracelet/gum
synced 2024-06-26 17:20:16 +02:00
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package choose
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/alecthomas/kong"
|
|
"github.com/charmbracelet/bubbles/paginator"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
|
"github.com/charmbracelet/gum/style"
|
|
"github.com/charmbracelet/lipgloss"
|
|
)
|
|
|
|
var (
|
|
subduedStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"})
|
|
verySubduedStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"})
|
|
)
|
|
|
|
// Run provides a shell script interface for choosing between different through
|
|
// options.
|
|
func (o Options) Run() error {
|
|
if len(o.Options) == 0 {
|
|
input, _ := stdin.Read()
|
|
if input == "" {
|
|
return errors.New("no options provided, see `gum choose --help`")
|
|
}
|
|
o.Options = strings.Split(strings.TrimSpace(input), "\n")
|
|
}
|
|
|
|
var items []item
|
|
for _, option := range o.Options {
|
|
items = append(items, item{text: option, selected: false})
|
|
}
|
|
|
|
// We don't need to display prefixes if we are only picking one option.
|
|
// Simply displaying the cursor is enough.
|
|
if o.Limit == 1 && !o.NoLimit {
|
|
o.SelectedPrefix = ""
|
|
o.UnselectedPrefix = ""
|
|
o.CursorPrefix = ""
|
|
}
|
|
|
|
// 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 {
|
|
o.Limit = len(o.Options)
|
|
}
|
|
|
|
// 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("•")
|
|
|
|
// Disable Keybindings since we will control it ourselves.
|
|
pager.UseHLKeys = false
|
|
pager.UseLeftRightKeys = false
|
|
pager.UseJKKeys = false
|
|
pager.UsePgUpPgDownKeys = false
|
|
|
|
m, err := tea.NewProgram(model{
|
|
height: o.Height,
|
|
cursor: o.Cursor,
|
|
selectedPrefix: o.SelectedPrefix,
|
|
unselectedPrefix: o.UnselectedPrefix,
|
|
cursorPrefix: o.CursorPrefix,
|
|
items: items,
|
|
limit: o.Limit,
|
|
paginator: pager,
|
|
cursorStyle: o.CursorStyle.ToLipgloss(),
|
|
itemStyle: o.ItemStyle.ToLipgloss(),
|
|
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
|
|
}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
|
|
var s strings.Builder
|
|
|
|
for _, item := range m.(model).items {
|
|
if item.selected {
|
|
s.WriteString(item.text)
|
|
s.WriteRune('\n')
|
|
}
|
|
}
|
|
|
|
fmt.Println(strings.TrimSuffix(s.String(), "\n"))
|
|
|
|
return err
|
|
}
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
|
return style.HideFlags(ctx)
|
|
}
|