Skip to content

Commit

Permalink
Add --select-if-one flag to choose/filter. (#398)
Browse files Browse the repository at this point in the history
* Add `--select-if-one` flag to `choose`/`filter`.

* Remove accidental commit of other changes.

* fix: use o.Options

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
  • Loading branch information
kennyp and maaslalani committed Nov 28, 2023
1 parent c5aa973 commit fb6849c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions choose/command.go
Expand Up @@ -35,6 +35,11 @@ func (o Options) Run() error {
o.Options = strings.Split(strings.TrimSuffix(input, "\n"), "\n")
}

if o.SelectIfOne && len(o.Options) == 1 {
print(o.Options[0])
return nil
}

// 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 {
Expand Down
1 change: 1 addition & 0 deletions choose/options.go
Expand Up @@ -19,6 +19,7 @@ type Options struct {
SelectedPrefix string `help:"Prefix to show on selected items (hidden if limit is 1)" default:"◉ " env:"GUM_CHOOSE_SELECTED_PREFIX"`
UnselectedPrefix string `help:"Prefix to show on unselected items (hidden if limit is 1)" default:"○ " env:"GUM_CHOOSE_UNSELECTED_PREFIX"`
Selected []string `help:"Options that should start as selected" default:"" env:"GUM_CHOOSE_SELECTED"`
SelectIfOne bool `help:"Select the given option if there is only one" group:"Selection"`
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_CHOOSE_HEADER_"`
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
Expand Down
9 changes: 9 additions & 0 deletions filter/command.go
Expand Up @@ -45,6 +45,15 @@ func (o Options) Run() error {
return errors.New("no options provided, see `gum filter --help`")
}

if o.SelectIfOne && len(o.Options) == 1 {
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Print(o.Options[0])
} else {
fmt.Print(ansi.Strip(o.Options[0]))
}
return nil
}

options := []tea.ProgramOption{tea.WithOutput(os.Stderr)}
if o.Height == 0 {
options = append(options, tea.WithAltScreen())
Expand Down
1 change: 1 addition & 0 deletions filter/options.go
Expand Up @@ -14,6 +14,7 @@ type Options struct {
IndicatorStyle style.Styles `embed:"" prefix:"indicator." set:"defaultForeground=212" envprefix:"GUM_FILTER_INDICATOR_"`
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"`
SelectIfOne bool `help:"Select the given option if there is only one" group:"Selection"`
Strict bool `help:"Only returns if anything matched. Otherwise return Filter" negatable:"true" default:"true" group:"Selection"`
SelectedPrefix string `help:"Character to indicate selected items (hidden if limit is 1)" default:" ◉ " env:"GUM_FILTER_SELECTED_PREFIX"`
SelectedPrefixStyle style.Styles `embed:"" prefix:"selected-indicator." set:"defaultForeground=212" envprefix:"GUM_FILTER_SELECTED_PREFIX_"`
Expand Down
2 changes: 1 addition & 1 deletion format/formats.go
Expand Up @@ -54,7 +54,7 @@ func markdown(input string, theme string) (string, error) {
}

func template(input string) (string, error) {
f := termenv.TemplateFuncs(termenv.EnvColorProfile())
f := termenv.TemplateFuncs(termenv.ColorProfile())
t, err := tpl.New("tpl").Funcs(f).Parse(input)
if err != nil {
return "", fmt.Errorf("unable to parse template: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -28,7 +28,7 @@ var (
var bubbleGumPink = lipgloss.NewStyle().Foreground(lipgloss.Color("212"))

func main() {
lipgloss.SetColorProfile(termenv.NewOutput(os.Stderr).EnvColorProfile())
lipgloss.SetColorProfile(termenv.NewOutput(os.Stderr).Profile)

if Version == "" {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
Expand Down

0 comments on commit fb6849c

Please sign in to comment.