quit when input ends at the guess prompt
At end of input (Ctrl-D, or stdin closed) read returns straight away with an empty guess, so the "Must be 5 letters" loop spun forever at full CPU. If the game's parent process was killed first, the subshell was left orphaned and spinning. Exit retrieve_guess when read hits EOF, pass that back through the retry prompts with a single recursive call, and quit from main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d34d98c15d
commit
a3f9ae6a17
1 file changed
+17
-11
@@ -58,7 +58,7 @@ function main() {
|
||||
for (( ROW; ROW<HEIGHT; ROW++ )); do
|
||||
declare GUESS
|
||||
[[ "${KEYBOARD}" == 1 ]] && render_keyboard
|
||||
GUESS=$(retrieve_guess)
|
||||
GUESS=$(retrieve_guess) || { echo -en "${NEWLINE}"; exit 0; }
|
||||
check_guess "${GUESS}" "${ROW}" ; WIN=$?
|
||||
|
||||
# Save before the reveal so quitting or disconnecting can't undo a guess
|
||||
@@ -243,25 +243,31 @@ function render_board() {
|
||||
[[ "${BORDER}" == 1 ]] && echo -e "└─────────────────┘"
|
||||
}
|
||||
|
||||
# Exits non-zero at end of input (Ctrl-D) so the caller can quit
|
||||
function retrieve_guess() {
|
||||
if [[ -n $1 ]]; then
|
||||
read -r -p "${CLEAR_UP1}$1: " GUESS
|
||||
read -r -p "${CLEAR_UP1}$1: " GUESS || exit 1
|
||||
else
|
||||
read -r -p "${CLEAR_2END}Enter Guess: " GUESS
|
||||
read -r -p "${CLEAR_2END}Enter Guess: " GUESS || exit 1
|
||||
fi
|
||||
|
||||
while [[ ${#GUESS} -ne 5 ]] || [[ ! "${GUESS,,}" =~ ^[a-z]{5}$ ]]; do
|
||||
read -r -p "${CLEAR_UP1}Must be 5 letters: " GUESS
|
||||
done
|
||||
read -r -p "${CLEAR_UP1}Must be 5 letters: " GUESS || exit 1
|
||||
done
|
||||
declare valid_guess
|
||||
validate_guess "${GUESS}" ; valid_guess=$?
|
||||
|
||||
[[ ${valid_guess} == 1 ]] && GUESS=$(retrieve_guess "Not A Valid Word")
|
||||
[[ ${valid_guess} == 2 ]] && GUESS=$(retrieve_guess "Not A Valid Guess")
|
||||
[[ ${valid_guess} == 3 ]] && GUESS=$(retrieve_guess "GREEN Issue")
|
||||
[[ ${valid_guess} == 4 ]] && GUESS=$(retrieve_guess "YELLOW Issue")
|
||||
[[ ${valid_guess} == 5 ]] && GUESS=$(retrieve_guess "POOL Issue")
|
||||
[[ ${valid_guess} == 6 ]] && GUESS=$(retrieve_guess "NEW Issue")
|
||||
declare message
|
||||
[[ ${valid_guess} == 1 ]] && message="Not A Valid Word"
|
||||
[[ ${valid_guess} == 2 ]] && message="Not A Valid Guess"
|
||||
[[ ${valid_guess} == 3 ]] && message="GREEN Issue"
|
||||
[[ ${valid_guess} == 4 ]] && message="YELLOW Issue"
|
||||
[[ ${valid_guess} == 5 ]] && message="POOL Issue"
|
||||
[[ ${valid_guess} == 6 ]] && message="NEW Issue"
|
||||
|
||||
if [[ -n "${message}" ]]; then
|
||||
GUESS=$(retrieve_guess "${message}") || exit 1
|
||||
fi
|
||||
|
||||
echo -n "${GUESS^^}"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user