charmbracelet / gum

A tool for glamorous shell scripts 🎀
MIT License
18.05k stars 340 forks source link

Let `gum filter` autoselect a value if only one matches the `--value` parameter #311

Open kotfu opened 1 year ago

kotfu commented 1 year ago

Is your feature request related to a problem? Please describe. I have a shell script that accepts a search term from the command line, and then passes it to gum filter to allow the user to select a choice. The search term is passed to gum using the --value option. If there is only a single line of input that matches value, then gum shows the single value and requires you to press return to select it. For example:

$ echo "Applications\nLibrary\nDownloads" | gum filter --value=Down

gum filters the list to a single item, but I still have to press return to select Downloads.

Describe the solution you'd like It may not be a good idea to change the default behavior in this situation, so perhaps another command line option that could tell gum to autoselect if only a single item matches what's passed in --value.

$ echo "Applications\nLibrary\nDownloads" | gum filter --value=Down --autoselect

This same solution could also work if there was only a single line of input:

$ echo "Downloads" | gum filter --autoselect

thereby avoiding presenting a user interface to ask the user to select from a single option.

The --autoselect option should not be allowed if --limit is greater than 1, because there is no way for gum to know which of the options the user would select, or how many of the options the user would select.

Describe alternatives you've considered You can always run the command that generates the input to gum filter and use grep to check if there is only a single line that matches. It's inefficient, because if more than one line matches, you have to run the command again to pipe it to gum filter. But it's a reasonable workaround. However, if you are using --fuzzy, no other shell tools use exactly the same algorithm, so this workaround doesn't apply.

basilioss commented 1 year ago

The same option should be available for gum choose. Maybe it's better to not reinvent the wheel and replace --autoselect with -1, --select-1, because it's often used with fzf.

eggbean commented 1 year ago

This really ought to be implemented. I have been looking through the options expecting something like this to be there, but it's not!

Drew-Goddyn commented 5 months ago

I assumed this is what --select-if-one would do but apparently not.

is this a duplicate request?

kotfu commented 5 months ago

The --select-if-one logic is applied before the --value logic:

$ echo "Downloads" | gum filter --select-if-one
Downloads

This:

$ echo "Applications\nLibrary\nDownloads" | gum filter --value=Down --select-if-one

still pops up the interactive selection UI even though there is only one item that matches the criteria specified by --value=Down