Browse Source

ask_yn fixed

main
deicidus 1 year ago
parent
commit
7d5dbf0ab2
  1. 13
      spells/cantrips/ask_yn

13
spells/cantrips/ask_yn

@ -1,21 +1,24 @@
#!/bin/bash #!/bin/sh
question=$1 question=$1
default_answer=$2 default_answer=$(echo $2 | tr a-z A-Z)
# Capitalize the default answer and construct the prompt # Capitalize the default answer and construct the prompt
if [[ $default_answer == "Y" ]]; then if [ "$default_answer" = "Y" ]; then
prompt="${question} (Y/n)? " prompt="${question} (Y/n)? "
else else
prompt="${question} (y/N)? " prompt="${question} (y/N)? "
fi fi
# Handle Ctrl-C
trap "printf '\n'; exit 2" 2
# Use stty to make read command work for single character input # Use stty to make read command work for single character input
old_stty_cfg=$(stty -g) old_stty_cfg=$(stty -g)
stty raw -echo stty raw -echo
printf "%s" "$prompt" printf "%s" "$prompt"
answer=$( while ! head -c 1 | grep -i '[yn]'; do true; done ) answer=$( while ! head -c 1 | grep -i '[yn]'; do true; done )
stty $old_stty_cfg stty "$old_stty_cfg"
# If no answer is given, use the default answer # If no answer is given, use the default answer
if [ -z "$answer" ]; then if [ -z "$answer" ]; then
@ -24,7 +27,7 @@ fi
# Print the answer and return appropriate exit code # Print the answer and return appropriate exit code
printf "%s\n" "$answer" printf "%s\n" "$answer"
if [[ $answer == "Y" || $answer == "y" ]]; then if [ "$answer" = "Y" -o "$answer" = "y" ]; then
exit 0 exit 0
else else
exit 1 exit 1

Loading…
Cancel
Save