From b62d32d99520c2c292206b5c2609f1dda3cda0b7 Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 09:47:41 +0000 Subject: [PATCH] Discard keys typed mid-run (wave 3, F6) run-and-show prints its :running frame (bold Checking exercises//.lisp ...) before spawning the subprocess, so the screen stays alive while the pipe is silent. When the run returns, keys queued while it ran are discarded; keys already queued when the run started survive, so scripted sessions still work. This fixes the stray-n auto-advance after a save. The reader thread now sets an open-p flag at EOF; prompt-line reads one answer line through the input queue and returns NIL at EOF. A 50ms grace at watch start lets piped stdin land before the first frame. README notes that keys typed mid-run are ignored. --- README.md | 3 +++ src/core.lisp | 74 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8f05b54..875c790 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,9 @@ Keys in watch mode (each key is a line: type it, then press ENTER): | l | list all exercises | | q | save progress and quit | +Keys typed while a check is running are ignored; press the key +again once it finishes. + Really stuck? `solutions/` mirrors `exercises/` with every exercise solved. diff --git a/src/core.lisp b/src/core.lisp index 4735625..b0159a6 100644 --- a/src/core.lisp +++ b/src/core.lisp @@ -384,23 +384,27 @@ always prints linearly." (read-line *standard-input* nil nil) (clear-screen)) -(defun run-and-show (state) +(defun run-and-show (state queue lock) "Run the current exercise and draw the frame that results. The first frame shows a `Checking ...' line, so the screen is alive -while the subprocess works and its output is still in the pipe." - (setf (screen-state-result state) :running) - (render-screen state) - (multiple-value-bind (output code timed-out-p) - (run-exercise-file (exercise-path (current-exercise state))) - (setf (screen-state-output state) output) - (cond (timed-out-p - (setf (screen-state-result state) :timeout)) - ((zerop code) - (mark-done (current-exercise state)) - (setf (screen-state-result state) :pass)) - (t - (setf (screen-state-result state) :fail))) - (render-screen state))) +while the subprocess works and its output is still in the pipe. +Keys typed while the check ran are discarded, so they cannot act +on the fresh frame." + (let ((pending (queue-length queue lock))) + (setf (screen-state-result state) :running) + (render-screen state) + (multiple-value-bind (output code timed-out-p) + (run-exercise-file (exercise-path (current-exercise state))) + (setf (screen-state-output state) output) + (cond (timed-out-p + (setf (screen-state-result state) :timeout)) + ((zerop code) + (mark-done (current-exercise state)) + (setf (screen-state-result state) :pass)) + (t + (setf (screen-state-result state) :fail))) + (drain-commands queue lock :keep pending) + (render-screen state)))) (defun list-exercises (filter) (format t "~&~a~%" (bold "Lisplings exercises:")) @@ -434,6 +438,29 @@ while the subprocess works and its output is still in the pipe." (sb-thread:with-mutex (lock) (pop (car queue)))) +(defun queue-length (queue lock) + (sb-thread:with-mutex (lock) + (length (car queue)))) + +(defun drain-commands (queue lock &key (keep 0)) + "Drop queued commands beyond the first KEEP. Called after a check +run: keys typed while it ran never reach the dispatch loop." + (sb-thread:with-mutex (lock) + (setf (car queue) (subseq (car queue) 0 (min keep (length (car queue))))) + (values))) + +(defun prompt-line (prompt queue lock open-p) + "Print PROMPT and read one answer line through the watch input +queue; the reader thread owns stdin, so answers must come through +the queue. Returns NIL at EOF." + (format t "~&~a" prompt) + (finish-output) + (loop + (let ((line (pop-command queue lock))) + (cond (line (return line)) + ((null (car open-p)) (return nil)) + (t (sleep 0.05)))))) + (defun watch () (ensure-orig-snapshot) (unless (probe-file (state-path)) @@ -441,6 +468,7 @@ while the subprocess works and its output is still in the pipe." (let* ((queue (list nil)) (lock (sb-thread:make-mutex)) (stdin *standard-input*) + (open-p (list t)) (reader (sb-thread:make-thread (lambda () (ignore-errors @@ -449,13 +477,17 @@ while the subprocess works and its output is still in the pipe." do (sb-thread:with-mutex (lock) (setf (car queue) (append (car queue) - (list (string-trim " " line)))))))) - :name "lisplings-input"))) + (list (string-trim " " line)))))) + (setf (car open-p) nil)) + :name "lisplings-input")))) (declare (ignore reader)) + ;; Let a freshly piped stdin land in the queue before the first + ;; frame, so a scripted session is not mistaken for mid-run keys. + (sleep 0.05) (let* ((index (initial-index)) (state (make-screen-state :index index))) (set-current-name (exercise-name (nth index *exercises*))) - (run-and-show state) + (run-and-show state queue lock) (block watch-loop (loop (loop for cmd = (pop-command queue lock) @@ -477,7 +509,7 @@ while the subprocess works and its output is still in the pipe." (screen-state-revealed state) 0) (set-current-name (exercise-name (nth next *exercises*))) - (run-and-show state)) + (run-and-show state queue lock)) (t (setf (screen-state-result state) :alldone) (render-screen state)))))))) @@ -486,7 +518,7 @@ while the subprocess works and its output is still in the pipe." (hint-count (current-exercise state))) (render-screen state)) ((or (string-equal cmd "r") (string= cmd "")) - (run-and-show state)) + (run-and-show state queue lock)) ((string-equal cmd "l") (list-exercises :all) (render-screen state :clear nil)) @@ -500,7 +532,7 @@ while the subprocess works and its output is still in the pipe." (sleep 0.2) (let ((now (exercise-mtime (current-exercise state)))) (unless (eql now was) - (run-and-show state))))))) + (run-and-show state queue lock))))))) (save-state) (format t "~&Bye! Progress saved.~%")))