Browse Source

ask_yn works perfectly without read -n!

main
deicidus 2 years ago
parent
commit
dbf657888e
  1. 18
      spells/cantrips/ask_yn

18
spells/cantrips/ask_yn

@ -10,20 +10,24 @@ else
prompt="${question} (y/N)? "
fi
# Save previous stty config
# Save stty configuration
old_stty_cfg=$(stty -g)
# Handle Ctrl-C
trap "stty '$old_stty_cfg'; printf '\n'; exit 2" 2
# Use stty to make read command work for single character input
stty raw -echo
printf "%s" "$prompt" >&2
while IFS= read -r -n1 answer
while true
do
if [ "$answer" = "" ] || echo "$answer" | grep -iq "^y\|^n"
then
answer=$(head -c 1)
if [ "$answer" = $'\n' ] || [ "$answer" = $'\r' ]; then
answer=""
break
elif printf "%s" "$answer" | grep -iq "^y\|^n"; then
break
elif [ "$answer" = $'\003' ]; then
stty "$old_stty_cfg"
printf '\n'
exit 2
fi
done
stty "$old_stty_cfg" >&2 # Restore original stty configuration

Loading…
Cancel
Save