fzfmenu: DRY and don't create temporary file

This commit is contained in:
tv 2020-06-16 21:13:00 +02:00
parent ac9d611884
commit 59aa9811f3

View File

@ -29,21 +29,34 @@ in
pkgs.writeDashBin "fzfmenu" '' pkgs.writeDashBin "fzfmenu" ''
set -efu set -efu
# Spawn terminal if called without one, like e.g. from a window manager.
if [ -z ''${TERM+x} ]; then
exec 3<&0
exec 4>&1
export FZFMENU_INPUT_FD=3
export FZFMENU_OUTPUT_FD=4
exec ${pkgs.rxvt_unicode}/bin/urxvt \
-name ${cfg.appName} \
-title ${shell.escape cfg.windowTitle} \
-e "$0" "$@"
else
exec 0<&''${FZFMENU_INPUT_FD-0}
exec 1>&''${FZFMENU_OUTPUT_FD-1}
fi
PROMPT=">" PROMPT=">"
for i in "$@" for i in "$@"; do
do
case $i in case $i in
-p) -p)
PROMPT="$2" PROMPT=$2
shift shift 2
shift
break break
;; ;;
-l) -l)
# no reason to filter number of lines # no reason to filter number of lines
LINES="$2" LINES=$2
shift shift 2
shift
break break
;; ;;
-i) -i)
@ -57,27 +70,12 @@ pkgs.writeDashBin "fzfmenu" ''
;; ;;
esac esac
done done
INPUT=$(${pkgs.coreutils}/bin/cat)
OUTPUT="$(${pkgs.coreutils}/bin/mktemp)" ${pkgs.fzf}/bin/fzf \
if [ -z ''${TERM+x} ]; then #check if we can print fzf in the shell
${pkgs.rxvt_unicode}/bin/urxvt \
-name ${cfg.appName} \
-title ${shell.escape cfg.windowTitle} \
-e ${pkgs.dash}/bin/dash -c \
"echo \"$INPUT\" | ${pkgs.fzf}/bin/fzf \
--history=/dev/null \
--print-query \
--prompt=\"$PROMPT\" \
--reverse \
> \"$OUTPUT\"" 2>/dev/null
else
echo "$INPUT" | ${pkgs.fzf}/bin/fzf \
--history=/dev/null \ --history=/dev/null \
--print-query \ --print-query \
--prompt="$PROMPT" \ --prompt="$PROMPT" \
--reverse \ --reverse \
> "$OUTPUT" |
fi ${pkgs.coreutils}/bin/tail -1
${pkgs.coreutils}/bin/tail -1 "$OUTPUT"
${pkgs.coreutils}/bin/rm "$OUTPUT"
'' ''