fzy icon indicating copy to clipboard operation
fzy copied to clipboard

Skip execution when receiving empty input

Open terlar opened this issue 6 years ago • 6 comments

It would be nice to have an option that tells fzy to exit if there is no data being piped to it. I am using fzy for a menu completion system that presents new menus and when there is no option left it will produce an empty list and then there is nothing to do.

With fzf you kind of can do this with the option:

 -0, --exit-0          Exit immediately when there's no match

However, I see it rather as exit immediately if no input was given to fzy.

terlar avatar Nov 25 '18 10:11 terlar

Can you use grep . for this? e.g.

$ echo | grep .
$ echo $?
1

$ echo 'a' | grep .
a
$ echo $?
0

casr avatar Nov 26 '18 11:11 casr

I was already thinking of that and thought that might solve the problem, but the fzy will still execute. E.g.

echo | grep . | fzy 
* empty menu appears *

terlar avatar Nov 26 '18 12:11 terlar

echo prints a new line, but you are still right. Try ifne tool from moreutils:

printf '' | ifne fzy

maximbaz avatar Nov 26 '18 12:11 maximbaz

@maximbaz thanks for the input, that works, only downside is that I have to install another tool, but it is fine for my use case.

terlar avatar Nov 26 '18 16:11 terlar

I was thinking you already had a script prepared that you could integrate something like:

MENU_ITEMS='one\ntwo\nthree\n'
printf "${MENU_ITEMS}" | grep . >/dev/null
VALID_MENU=$?
if [ $VALID_MENU -eq 0 ]; then
    CHOICE=$(printf "${MENU_ITEMS}" | fzy)
fi
# do something with CHOICE...

casr avatar Nov 26 '18 18:11 casr

Indeed, but that is in my opinion something overly complicated for something that should be quite simple. Bash + capture multi-line strings from commands is not great in my experience. If it is out of scope, the ifne tool solves it much nicer IMO. Thanks for the tips though.

terlar avatar Nov 26 '18 21:11 terlar