fzy
fzy copied to clipboard
Skip execution when receiving empty input
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.
Can you use grep .
for this? e.g.
$ echo | grep .
$ echo $?
1
$ echo 'a' | grep .
a
$ echo $?
0
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 *
echo
prints a new line, but you are still right. Try ifne
tool from moreutils
:
printf '' | ifne fzy
@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.
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...
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.