From 91134967ffa260cb42aa1a6174f19d8d3a8777c0 Mon Sep 17 00:00:00 2001 From: deicidus Date: Fri, 26 May 2023 02:07:40 -0700 Subject: [PATCH] fixed ask_yn --- spells/cantrips/ask_yn | 50 ++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/spells/cantrips/ask_yn b/spells/cantrips/ask_yn index a625eac..65ebb84 100755 --- a/spells/cantrips/ask_yn +++ b/spells/cantrips/ask_yn @@ -6,39 +6,31 @@ ask_yn() { default=${2:-"y"} default=$(echo "$default" | tr '[:upper:]' '[:lower:]') - if [ "$default" = "y" ]; then - prompt="${1} (Y/n): " - default_output=1 - else - prompt="${1} (y/N): " - default_output=0 - fi + + # Set prompt string + case "$default" in + y) prompt="Y/n" ;; + n) prompt="y/N" ;; + *) echo "Invalid default answer"; exit 1 ;; + esac while true; do - # Show prompt and get user input - printf "%s" "$prompt" - if [ "$(uname)" = "Darwin" ]; then - # On macOS, use "read -n 1" - read -n 1 -r REPLY - else - # On Linux and others, use "read -n 1 -s" - read -r -n 1 -s REPLY - fi - - # If user hits Ctrl+C or Enter, break and return default output - if [ -z "$REPLY" ] || [ "$REPLY" = "$(printf '\003')" ]; then - REPLY=$default - printf "\n" - break - fi - - case "$REPLY" in - y|Y ) printf "\n"; return 1;; - n|N ) printf "\n"; return 0;; - * ) printf "\nInvalid input. Please type 'y' or 'n'.";; + echo -n "$1 ($prompt): " + stty raw -echo + answer=$(head -c 1) || exit + stty -echo + printf "\n" + case "$answer" in + "" ) answer="$default"; break;; + y|Y) answer="y"; break;; + n|N) answer="n"; break;; esac done - return $default_output + + case "$answer" in + y|Y) echo 1;; + n|N) echo 0;; + esac } ask_yn "$@" \ No newline at end of file