Discard keys typed mid-run (wave 3, F6)

run-and-show prints its :running frame (bold Checking exercises/<dir>/<name>.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.
This commit is contained in:
venomade committed 2026-11-22 09:47:41 +00:00
1 parent 4c6009e2e4
commit b62d32d995
2 files changed
+56 -21

No files matched your search

+3
View File
@@ -67,6 +67,9 @@ Keys in watch mode (each key is a line: type it, then press ENTER):
| l | list all exercises | | l | list all exercises |
| q | save progress and quit | | 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 Really stuck? `solutions/` mirrors `exercises/` with every exercise
solved. solved.
+53 -21
View File
@@ -384,23 +384,27 @@ always prints linearly."
(read-line *standard-input* nil nil) (read-line *standard-input* nil nil)
(clear-screen)) (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 "Run the current exercise and draw the frame that results. The
first frame shows a `Checking ...' line, so the screen is alive first frame shows a `Checking ...' line, so the screen is alive
while the subprocess works and its output is still in the pipe." while the subprocess works and its output is still in the pipe.
(setf (screen-state-result state) :running) Keys typed while the check ran are discarded, so they cannot act
(render-screen state) on the fresh frame."
(multiple-value-bind (output code timed-out-p) (let ((pending (queue-length queue lock)))
(run-exercise-file (exercise-path (current-exercise state))) (setf (screen-state-result state) :running)
(setf (screen-state-output state) output) (render-screen state)
(cond (timed-out-p (multiple-value-bind (output code timed-out-p)
(setf (screen-state-result state) :timeout)) (run-exercise-file (exercise-path (current-exercise state)))
((zerop code) (setf (screen-state-output state) output)
(mark-done (current-exercise state)) (cond (timed-out-p
(setf (screen-state-result state) :pass)) (setf (screen-state-result state) :timeout))
(t ((zerop code)
(setf (screen-state-result state) :fail))) (mark-done (current-exercise state))
(render-screen 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) (defun list-exercises (filter)
(format t "~&~a~%" (bold "Lisplings exercises:")) (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) (sb-thread:with-mutex (lock)
(pop (car queue)))) (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 () (defun watch ()
(ensure-orig-snapshot) (ensure-orig-snapshot)
(unless (probe-file (state-path)) (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)) (let* ((queue (list nil))
(lock (sb-thread:make-mutex)) (lock (sb-thread:make-mutex))
(stdin *standard-input*) (stdin *standard-input*)
(open-p (list t))
(reader (sb-thread:make-thread (reader (sb-thread:make-thread
(lambda () (lambda ()
(ignore-errors (ignore-errors
@@ -449,13 +477,17 @@ while the subprocess works and its output is still in the pipe."
do (sb-thread:with-mutex (lock) do (sb-thread:with-mutex (lock)
(setf (car queue) (setf (car queue)
(append (car queue) (append (car queue)
(list (string-trim " " line)))))))) (list (string-trim " " line))))))
:name "lisplings-input"))) (setf (car open-p) nil))
:name "lisplings-input"))))
(declare (ignore reader)) (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)) (let* ((index (initial-index))
(state (make-screen-state :index index))) (state (make-screen-state :index index)))
(set-current-name (exercise-name (nth index *exercises*))) (set-current-name (exercise-name (nth index *exercises*)))
(run-and-show state) (run-and-show state queue lock)
(block watch-loop (block watch-loop
(loop (loop
(loop for cmd = (pop-command queue lock) (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) (screen-state-revealed state) 0)
(set-current-name (set-current-name
(exercise-name (nth next *exercises*))) (exercise-name (nth next *exercises*)))
(run-and-show state)) (run-and-show state queue lock))
(t (t
(setf (screen-state-result state) :alldone) (setf (screen-state-result state) :alldone)
(render-screen state)))))))) (render-screen state))))))))
@@ -486,7 +518,7 @@ while the subprocess works and its output is still in the pipe."
(hint-count (current-exercise state))) (hint-count (current-exercise state)))
(render-screen state)) (render-screen state))
((or (string-equal cmd "r") (string= cmd "")) ((or (string-equal cmd "r") (string= cmd ""))
(run-and-show state)) (run-and-show state queue lock))
((string-equal cmd "l") ((string-equal cmd "l")
(list-exercises :all) (list-exercises :all)
(render-screen state :clear nil)) (render-screen state :clear nil))
@@ -500,7 +532,7 @@ while the subprocess works and its output is still in the pipe."
(sleep 0.2) (sleep 0.2)
(let ((now (exercise-mtime (current-exercise state)))) (let ((now (exercise-mtime (current-exercise state))))
(unless (eql now was) (unless (eql now was)
(run-and-show state))))))) (run-and-show state queue lock)))))))
(save-state) (save-state)
(format t "~&Bye! Progress saved.~%"))) (format t "~&Bye! Progress saved.~%")))