diff --git a/spells/cantrips/ask_yn b/spells/cantrips/ask_yn index af22c7c..cd0b1ee 100755 --- a/spells/cantrips/ask_yn +++ b/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 +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