feat(choose): --no-limit flag alias

Adds a --no-limit flag as an alias to set the limit to the number of options available.
This commit is contained in:
Maas Lalani 2022-07-13 12:10:38 -04:00
parent 01404ef586
commit 54f85439fa
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000
2 changed files with 13 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package choose
import (
"errors"
"fmt"
"os"
"strings"
@ -14,6 +15,9 @@ import (
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")
}
@ -24,12 +28,18 @@ func (o Options) Run() error {
// We don't need to display prefixes if we are only picking one option.
// Simply displaying the cursor is enough.
if o.Limit == 1 {
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)
}
m, err := tea.NewProgram(model{
height: o.Height,
cursor: o.Cursor,

View file

@ -6,7 +6,8 @@ import "github.com/charmbracelet/gum/style"
type Options struct {
Options []string `arg:"" optional:"" help:"Options to choose from."`
Limit int `help:"Maximum number of options to pick" default:"1"`
Limit int `help:"Maximum number of options to pick" default:"1" group:"Selection"`
NoLimit bool `help:"Pick unlimited number of options (ignores limit)" group:"Selection"`
Height int `help:"Height of the list" default:"10"`
Cursor string `help:"Prefix to show on item that corresponds to the cursor position" default:"> "`
CursorPrefix string `help:"Prefix to show on the cursor item (hidden if limit is 1)" default:"• "`