Browse Source

fixed ask_yn

main
deicidus 1 year 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): " # Set prompt string
default_output=1 case "$default" in
else y) prompt="Y/n" ;;
prompt="${1} (y/N): " n) prompt="y/N" ;;
default_output=0 *) echo "Invalid default answer"; exit 1 ;;
fi esac
while true; do while true; do
# Show prompt and get user input echo -n "$1 ($prompt): "
printf "%s" "$prompt" stty raw -echo
if [ "$(uname)" = "Darwin" ]; then answer=$(head -c 1) || exit
# On macOS, use "read -n 1" stty -echo
read -n 1 -r REPLY printf "\n"
else case "$answer" in
# On Linux and others, use "read -n 1 -s" "" ) answer="$default"; break;;
read -r -n 1 -s REPLY y|Y) answer="y"; break;;
fi n|N) answer="n"; break;;
# 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'.";;
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