From e44be71ab07fb1bcc9b40664aa907eeda1352747 Mon Sep 17 00:00:00 2001 From: deicidus Date: Fri, 26 May 2023 02:19:00 -0700 Subject: [PATCH] reverted with fix --- spells/cantrips/ask_yn | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/spells/cantrips/ask_yn b/spells/cantrips/ask_yn index d0aebc4..1df4d0b 100755 --- a/spells/cantrips/ask_yn +++ b/spells/cantrips/ask_yn @@ -1,5 +1,8 @@ #!/bin/sh +# Define ask_yn, which asks a yes or no question and defaults to the specified choice. +# Outputs 1 for yes and 0 for no + ask_yn() { default=${2:-"y"} default=$(printf "%s" "$default" | tr '[:upper:]' '[:lower:]') @@ -12,14 +15,14 @@ ask_yn() { esac while true; do - trap 'exit' INT + trap 'stty sane; exit' INT printf "%s (%s): " "$1" "$prompt" - read -r answer || exit - if [ -z "$answer" ]; then - answer="$default" - break - fi + 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 @@ -29,4 +32,6 @@ ask_yn() { y|Y) printf "1";; n|N) printf "0";; esac -} \ No newline at end of file +} + +ask_yn "$@" \ No newline at end of file