Browse Source

fixed ask_yn

main
deicidus 2 years ago
parent
commit
91134967ff
  1. 50
      spells/cantrips/ask_yn

50
spells/cantrips/ask_yn

@ -6,39 +6,31 @@
ask_yn() { ask_yn() {
default=${2:-"y"} default=${2:-"y"}
default=$(echo "$default" | tr '[:upper:]' '[:lower:]') 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
while true; do # Set prompt string
# Show prompt and get user input case "$default" in
printf "%s" "$prompt" y) prompt="Y/n" ;;
if [ "$(uname)" = "Darwin" ]; then n) prompt="y/N" ;;
# On macOS, use "read -n 1" *) echo "Invalid default answer"; exit 1 ;;
read -n 1 -r REPLY esac
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 while true; do
y|Y ) printf "\n"; return 1;; echo -n "$1 ($prompt): "
n|N ) printf "\n"; return 0;; stty raw -echo
* ) printf "\nInvalid input. Please type 'y' or 'n'.";; 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 esac
done done
return $default_output
case "$answer" in
y|Y) echo 1;;
n|N) echo 0;;
esac
} }
ask_yn "$@" ask_yn "$@"
Loading…
Cancel
Save