treat Ctrl-D at the guess prompt like Enter

Ctrl-D doesn't move the cursor to a new line, but the retry prompts
move up a line assuming Enter was pressed, so each Ctrl-D climbed up
the screen over the keyboard and board. It also quit the game, losing
an offline game to a stray key press.

On a terminal, print the missing newline and carry on as if Enter was
pressed. Only exit when input has really ended (not a terminal), so a
closed pipe still can't spin the retry loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adamandClaude Opus 5.5 committed 2026-09-29 17:39:02 +00:00
1 parent 0f8640e9e8
commit 8e62094a6f
1 file changed
+12 -4
+12 -4
View File
@@ -247,16 +247,16 @@ function render_answer() {
echo -en "${NEWLINE}${RESET}"
}
# Exits non-zero at end of input (Ctrl-D) so the caller can quit
# Exits non-zero when input has ended so the caller can quit
function retrieve_guess() {
if [[ -n $1 ]]; then
read -r -p "${CLEAR_UP1}$1: " GUESS || exit 1
read -r -p "${CLEAR_UP1}$1: " GUESS || end_of_input
else
read -r -p "${CLEAR_2END}Enter Guess: " GUESS || exit 1
read -r -p "${CLEAR_2END}Enter Guess: " GUESS || end_of_input
fi
while [[ ${#GUESS} -ne 5 ]] || [[ ! "${GUESS,,}" =~ ^[a-z]{5}$ ]]; do
read -r -p "${CLEAR_UP1}Must be 5 letters: " GUESS || exit 1
read -r -p "${CLEAR_UP1}Must be 5 letters: " GUESS || end_of_input
done
declare valid_guess
validate_guess "${GUESS}" ; valid_guess=$?
@@ -278,6 +278,14 @@ function retrieve_guess() {
echo -n "${GUESS^^}"
}
# Called when read gets Ctrl-D. On a terminal treat it like Enter, but if
# input has really ended (closed pipe) exit, or the retry loop spins forever
function end_of_input() {
[[ -t 0 ]] || exit 1
# Ctrl-D doesn't move to a new line like Enter, which the prompts expect
echo -en "${NEWLINE}" >&2
}
function render_keyboard() {
declare pos=$(( BORDER*2+7 ))
echo -en "${RESET}\e[$pos;0H"