From 0cc16426f2f38a506a2c965fa347085b084e0403 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Sat, 22 Apr 2023 11:56:13 -0400 Subject: [PATCH] sorter.sh: make it POSIX-compliant --- src/.local/bin/sorter.sh | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/.local/bin/sorter.sh b/src/.local/bin/sorter.sh index 63f1a62..a47faf9 100755 --- a/src/.local/bin/sorter.sh +++ b/src/.local/bin/sorter.sh @@ -1,6 +1,27 @@ #!/bin/sh # Categorize files in the quarantine directory +# read -n 1 isn't POSIX-compliant so we implement something +# https://unix.stackexchange.com/questions/464930/can-i-read-a-single-character-from-stdin-in-posix-shell +readc() { + # return a single character from input + + # if tty + if [ -t 0 ]; then + # save settings + saved_settings="$(stty -g)" + # make it so we get the character even if no enter press + stty -icanon + fi + + dd bs=1 count=1 2>/dev/null + + if [ -t 0 ]; then + # restore settings + stty "$saved_settings" + fi +} + view() { mpv --no-resume-playback "$1" } @@ -61,7 +82,7 @@ while read -r FILE; do printf "send to: %s\n" "$DESTPATH" printf "\nh view again, j set directory, k set name, l confirm, L confirm and preserve information\n q exit, s skip, d move to trash\n" printf "\n> " - read -n 1 ANS < /dev/tty + ANS="$(readc