Browse Source

ask_yn works perfectly without read -n!

main
deicidus 1 year ago
parent
commit
dbf657888e
  1. 20
      spells/cantrips/ask_yn

20
spells/cantrips/ask_yn

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

Loading…
Cancel
Save