Browse Source

fixed ask_yn

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

48
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 while true; do
if [ -z "$REPLY" ] || [ "$REPLY" = "$(printf '\003')" ]; then echo -n "$1 ($prompt): "
REPLY=$default stty raw -echo
answer=$(head -c 1) || exit
stty -echo
printf "\n" printf "\n"
break case "$answer" in
fi "" ) answer="$default"; break;;
y|Y) answer="y"; break;;
case "$REPLY" in n|N) answer="n"; break;;
y|Y ) printf "\n"; return 1;;
n|N ) printf "\n"; return 0;;
* ) printf "\nInvalid input. Please type 'y' or 'n'.";;
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