gum icon indicating copy to clipboard operation
gum copied to clipboard

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

Open kotfu opened this issue 1 year ago • 4 comments

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.

kotfu avatar Mar 19 '23 22:03 kotfu

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.

basilioss avatar Apr 22 '23 11:04 basilioss

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

eggbean avatar Sep 13 '23 14:09 eggbean

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

is this a duplicate request?

Drew-Goddyn avatar May 17 '24 17:05 Drew-Goddyn

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

kotfu avatar May 17 '24 17:05 kotfu