Add persistent watch status screen (wave 3, F1)

render-screen is now the only printing path in watch mode: bold
header, current exercise path, captured output, a result line with
Exercise done + Solution for comparison on pass, pinned hints, a
width-aware colored progress bar (COLUMNS / TIOCGWINSZ / 80), and a
dynamic valid-key legend. Non-tty output prints full linear frames
delimited by --------; nothing is hidden on pipes.

*use-color* now requires a tty (LISPLINGS_COLOR=always/never still
override) and moves below stdout-is-tty-p, fixing its load order.
One-shot run/hint render the same frame. dev/verify.sh: 70/70.
This commit is contained in:
venomade committed 2026-11-22 09:44:22 +00:00
1 parent d0f9d8ceab
commit 4c6009e2e4
2 files changed
+246 -78

No files matched your search

+10 -6
View File
@@ -49,16 +49,20 @@ nix run /path/to/lisplings # watch mode, on this directory
## The workflow ## The workflow
1. Run `./lisplings` from the repository root (or your project copy). 1. Run `./lisplings` from the repository root (or your project copy).
2. Edit the exercise file shown in the terminal, in any editor. 2. Lisplings keeps one status screen on your terminal: the current
3. Save. The exercise runs again and its checks print. exercise file, the output of its checks, the hints you revealed, a
4. When everything passes, press `n` for the next exercise. progress bar, and the keys that work right now.
3. Edit that file in any editor and save. The exercise runs again and
the screen redraws; when every check passes it shows `Exercise
done ✓` and the matching solution to compare against.
4. Press `n` for the next exercise.
Keys in watch mode: Keys in watch mode (each key is a line: type it, then press ENTER):
| key | action | | key | action |
| --- | --------------------------------------- | | --- | ------------------------------------ |
| n | move to the next pending exercise | | n | move to the next pending exercise |
| h | show a hint | | h | show the hint, pinned on the screen |
| r | rerun the current exercise | | r | rerun the current exercise |
| l | list all exercises | | l | list all exercises |
| q | save progress and quit | | q | save progress and quit |
+236 -72
View File
@@ -145,11 +145,20 @@ so `reset' can restore them. Start lisplings before editing anything!"
;;; --------------------------------------------------------------------- ;;; ---------------------------------------------------------------------
;;; Terminal helpers ;;; Terminal helpers
(defun stdout-is-tty-p ()
(ignore-errors (plusp (sb-unix:unix-isatty 1))))
;; Defined after stdout-is-tty-p: the init form calls it, so this must
;; not sit above its definition.
(defparameter *use-color* (defparameter *use-color*
(and (not (or (sb-ext:posix-getenv "NO_COLOR") (let ((forced (sb-ext:posix-getenv "LISPLINGS_COLOR")))
(cond ((and forced (string-equal forced "always")) t)
((and forced (string-equal forced "never")) nil)
(t (and (stdout-is-tty-p)
(not (or (sb-ext:posix-getenv "NO_COLOR")
(sb-ext:posix-getenv "LISPLINGS_NO_COLOR"))) (sb-ext:posix-getenv "LISPLINGS_NO_COLOR")))
(sb-ext:posix-getenv "TERM") (let ((term (sb-ext:posix-getenv "TERM")))
(not (string= "dumb" (sb-ext:posix-getenv "TERM"))))) (and term (not (string= "dumb" term)))))))))
(defun ansi (code string) (defun ansi (code string)
(if *use-color* (if *use-color*
@@ -161,24 +170,58 @@ so `reset' can restore them. Start lisplings before editing anything!"
(defun yellow (s) (ansi 33 s)) (defun yellow (s) (ansi 33 s))
(defun bold (s) (ansi 1 s)) (defun bold (s) (ansi 1 s))
(defun stdout-is-tty-p ()
(ignore-errors (plusp (sb-unix:unix-isatty 1))))
(defun clear-screen () (defun clear-screen ()
(when (stdout-is-tty-p) (when (stdout-is-tty-p)
(format t "~c[H~c[2J~c[3J" #\escape #\escape #\escape) (format t "~c[H~c[2J~c[3J" #\escape #\escape #\escape)
(finish-output))) (finish-output)))
(defun progress-bar () (defun terminal-width ()
"Usable width of stdout: $COLUMNS when it is a positive integer,
else the kernel's window size via TIOCGWINSZ, else 80."
(let ((columns (ignore-errors
(parse-integer (sb-ext:posix-getenv "COLUMNS")))))
(or (and (integerp columns) (plusp columns) columns)
(or (ignore-errors
(sb-alien:with-alien
((ws (sb-alien:struct winsize
(ws-row (sb-alien:unsigned 16))
(ws-col (sb-alien:unsigned 16))
(ws-xpixel (sb-alien:unsigned 16))
(ws-ypixel (sb-alien:unsigned 16)))))
(when (zerop (sb-alien:alien-funcall
(sb-alien:extern-alien
"ioctl"
(sb-alien:function sb-alien:int
sb-alien:int
sb-alien:unsigned-long
(* (sb-alien:struct winsize
(ws-row (sb-alien:unsigned 16))
(ws-col (sb-alien:unsigned 16))
(ws-xpixel (sb-alien:unsigned 16))
(ws-ypixel (sb-alien:unsigned 16))))))
1 #x5413 (sb-alien:addr ws)))
(let ((col (sb-alien:slot ws 'ws-col)))
(and (plusp col) col)))))
80))))
(defun progress-bar (&key (width (terminal-width)))
"A colored bar that fits the terminal: the bar shrinks and grows with
the width, and terminals under 50 columns get text only."
(let* ((total (length *exercises*)) (let* ((total (length *exercises*))
(done (count-if #'exercise-done *exercises*)) (done (count-if #'exercise-done *exercises*))
(width 28) (percent (floor (* 100 done) (max 1 total))))
(filled (if (zerop total) 0 (floor (* width done) total)))) (if (< width 50)
(format nil "[~a~a] ~d% (~d/~d done)" (format nil "Progress: ~d/~d (~d%)" done total percent)
(make-string filled :initial-element #\#) (let* ((bar-chars (min 40 (max 10 (- width 30))))
(make-string (- width filled) :initial-element #\-) (filled (if (zerop total)
(floor (* 100 done) (max 1 total)) 0
done total))) (floor (* bar-chars done) total))))
(format nil "[~a~a] ~d% (~a done)"
(green (make-string filled :initial-element #\#))
(make-string (- bar-chars filled)
:initial-element #\-)
percent
(bold (format nil "~d/~d" done total)))))))
;;; --------------------------------------------------------------------- ;;; ---------------------------------------------------------------------
;;; Running exercises ;;; Running exercises
@@ -219,24 +262,115 @@ Returns (values output-string exit-code timed-out-p)."
(sb-thread:join-thread killer) (sb-thread:join-thread killer)
(values output (sb-ext:process-exit-code proc) timed-out))) (values output (sb-ext:process-exit-code proc) timed-out)))
(defun run-and-show (ex) ;;; ---------------------------------------------------------------------
(clear-screen) ;;; The status screen
(format t "~&~a~%" (bold (format nil "Lisplings · ~a" (exercise-name ex))))
(format t "~&Editing: ~a~%~%" (namestring (exercise-path ex))) (defstruct screen-state
(finish-output) ;; One watch frame, in memory only. Nothing here is persisted.
(multiple-value-bind (output code timed-out-p) (index 0) ; current exercise, an index into *exercises*
(run-exercise-file (exercise-path ex)) (output "") ; output of the last subprocess run
(format t "~a~%" output) (result :none) ; :none :running :pass :fail :timeout :alldone
(cond (revealed 0) ; hint paragraphs revealed for the current exercise
(timed-out-p notice) ; transient one-line message, cleared by render-screen
(format t "~&~a~%~%" (red "Timed out after 60 seconds. Infinite loop? Check the base case, or type r to rerun.")))
((zerop code) (defun current-exercise (state)
(mark-done ex) (nth (screen-state-index state) *exercises*))
(format t "~&~a~%~%" (green "Solved! Press n for the next exercise.")))
(defun hint-paragraphs (ex)
"The hint as a list of paragraph strings. The catalogue gives either
a plain string or a list of strings."
(let ((hint (exercise-hint ex)))
(cond ((null hint) '())
((stringp hint) (list hint))
((and (listp hint) (every #'stringp hint)) hint)
(t (error "Bad :hint for ~a: expected a string or list of strings"
(exercise-name ex))))))
(defun hint-count (ex)
(length (hint-paragraphs ex)))
(defun valid-keys (state)
"The keys that make sense right now, as key-and-label strings."
(let* ((ex (current-exercise state))
(revealed (screen-state-revealed state))
(total (hint-count ex))
(keys '()))
(when (or (exercise-done ex)
(eq (screen-state-result state) :pass))
(push "n next" keys))
(when (< revealed total)
(push (format nil "h hint (~d/~d)" (1+ revealed) total) keys))
(dolist (k '("r rerun" "l list" "q quit"))
(push k keys))
(nreverse keys)))
(defun render-screen (state &key (clear t))
"Print one complete watch frame: the only printing path in watch
mode. On a terminal the frame is cleared first; on a pipe nothing is
cleared, a separator line delimits frames, and the whole frame
always prints linearly."
(let ((ex (current-exercise state)))
(when clear (clear-screen))
(unless (stdout-is-tty-p)
(format t "~&--------~%"))
(cond ((eq (screen-state-result state) :alldone)
(format t "~&~a~%~%" (green "All exercises done!"))
(format t "~a~%~%" (info-field :final))
(format t "~&~a~%" (format nil "~{~a~^ · ~}"
'("r rerun" "l list" "q quit")))
(when (screen-state-notice state)
(format t "~&~a~%" (yellow (screen-state-notice state)))
(setf (screen-state-notice state) nil)))
(t (t
(format t "~&~a~%~%" (red "Keep working; the checks rerun every time you save.")))) (format t "~&~a~%"
(format t "~&~a~%~%" (progress-bar)) (bold (format nil "Lisplings · ~a/~a"
(format t "n next · h hint · r rerun · l list · q quit~%") (exercise-dir ex) (exercise-name ex))))
(format t "~&Current exercise: ~a~%"
(namestring (exercise-path ex)))
(format t "~%")
(cond ((eq (screen-state-result state) :running)
(format t "~&~a~%"
(bold (format nil "Checking exercises/~a/~a.lisp ..."
(exercise-dir ex)
(exercise-name ex)))))
(t
(let ((output (screen-state-output state)))
(when (plusp (length output))
(format t "~a"
(if (char= #\newline
(char output (1- (length output))))
output
(concatenate 'string output "~%")))))
(case (screen-state-result state)
(:pass
(format t "~&~a~%" (green (bold "Exercise done ✓")))
(format t "~&Solution for comparison: ~a~%"
(namestring (exercise-solution-path ex))))
(:fail
(format t "~&~a~%"
(red "Keep working. The checks rerun when you save.")))
(:timeout
(format t "~&~a~%"
(red "Timed out after 60 seconds. Infinite loop? Check the base case, or type r to rerun."))))))
(let* ((paragraphs (hint-paragraphs ex))
(total (length paragraphs))
(revealed (screen-state-revealed state)))
(when (plusp total)
(format t "~%")
(loop for i from 1 to revealed
for paragraph in paragraphs
do (format t "~&~a ~a~%"
(yellow (bold (format nil "Hint ~d/~d:" i total)))
paragraph))
(when (< revealed total)
(format t "~&~a~%"
(yellow "h reveals the next hint paragraph.")))))
(format t "~%~a~%" (progress-bar))
(format t "~&~a~%"
(format nil "~{~a~^ · ~}" (valid-keys state)))
(when (screen-state-notice state)
(format t "~&~a~%" (yellow (screen-state-notice state)))
(setf (screen-state-notice state) nil))))
(finish-output))) (finish-output)))
;;; --------------------------------------------------------------------- ;;; ---------------------------------------------------------------------
@@ -250,11 +384,23 @@ Returns (values output-string exit-code timed-out-p)."
(read-line *standard-input* nil nil) (read-line *standard-input* nil nil)
(clear-screen)) (clear-screen))
(defun show-hint (ex) (defun run-and-show (state)
(format t "~&~a~%" (yellow (format nil "Hint for ~a:" (exercise-name ex)))) "Run the current exercise and draw the frame that results. The
(format t "~&~a~%~%" (or (exercise-hint ex) first frame shows a `Checking ...' line, so the screen is alive
"No hint for this one. You're on your own!")) while the subprocess works and its output is still in the pipe."
(finish-output)) (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)))
(defun list-exercises (filter) (defun list-exercises (filter)
(format t "~&~a~%" (bold "Lisplings exercises:")) (format t "~&~a~%" (bold "Lisplings exercises:"))
@@ -306,49 +452,57 @@ Returns (values output-string exit-code timed-out-p)."
(list (string-trim " " line)))))))) (list (string-trim " " line))))))))
:name "lisplings-input"))) :name "lisplings-input")))
(declare (ignore reader)) (declare (ignore reader))
(let ((index (initial-index))) (let* ((index (initial-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 (nth index *exercises*)) (run-and-show state)
(block watch-loop (block watch-loop
(loop (loop
(loop for cmd = (pop-command queue lock) (loop for cmd = (pop-command queue lock)
while cmd while cmd
do (cond do (cond
((string-equal cmd "n") ((string-equal cmd "n")
(let ((current (nth index *exercises*))) (let ((current (current-exercise state)))
(cond ((not (exercise-done current)) (cond ((not (or (exercise-done current)
(format t "~&~a~%" (eq (screen-state-result state) :pass)))
(yellow "This exercise hasn't passed its checks yet. h for a hint, r to rerun."))) (setf (screen-state-notice state)
"This exercise hasn't passed its checks yet. h for a hint, r to rerun.")
(render-screen state))
(t (t
(let ((next (or (next-pending-index index) (let ((next (or (next-pending-index
(screen-state-index state))
(next-pending-index -1)))) (next-pending-index -1))))
(cond (next (cond (next
(setf index next) (setf (screen-state-index state) next
(set-current-name (exercise-name (nth index *exercises*))) (screen-state-revealed state) 0)
(run-and-show (nth index *exercises*))) (set-current-name
(exercise-name (nth next *exercises*)))
(run-and-show state))
(t (t
(clear-screen) (setf (screen-state-result state) :alldone)
(format t "~&~a~%~%" (green "All exercises done!")) (render-screen state))))))))
(format t "~a~%~%" (info-field :final))
(format t "r rerun · l list · q quit~%~%"))))))))
((string-equal cmd "h") ((string-equal cmd "h")
(show-hint (nth index *exercises*))) (setf (screen-state-revealed state)
(hint-count (current-exercise state)))
(render-screen state))
((or (string-equal cmd "r") (string= cmd "")) ((or (string-equal cmd "r") (string= cmd ""))
(run-and-show (nth index *exercises*))) (run-and-show state))
((string-equal cmd "l") ((string-equal cmd "l")
(list-exercises :all)) (list-exercises :all)
(render-screen state :clear nil))
((string-equal cmd "q") ((string-equal cmd "q")
(return-from watch-loop)) (return-from watch-loop))
(t (t
(format t "~&Unknown command: ~a~%" cmd) (setf (screen-state-notice state)
(format t "~&n next · h hint · r rerun · l list · q quit~%~%")))) (format nil "Unknown command: ~a" cmd))
(let ((was (exercise-mtime (nth index *exercises*)))) (render-screen state))))
(let ((was (exercise-mtime (current-exercise state))))
(sleep 0.2) (sleep 0.2)
(let ((now (exercise-mtime (nth index *exercises*)))) (let ((now (exercise-mtime (current-exercise state))))
(unless (eql now was) (unless (eql now was)
(run-and-show (nth index *exercises*))))))) (run-and-show state)))))))
(save-state) (save-state)
(format t "~&Bye! Progress saved.~%")))) (format t "~&Bye! Progress saved.~%")))
;;; --------------------------------------------------------------------- ;;; ---------------------------------------------------------------------
;;; One-shot commands ;;; One-shot commands
@@ -361,23 +515,33 @@ Returns (values output-string exit-code timed-out-p)."
(let ((ex (resolve-exercise name))) (let ((ex (resolve-exercise name)))
(set-current-name (exercise-name ex)) (set-current-name (exercise-name ex))
(ensure-orig-snapshot) (ensure-orig-snapshot)
(format t "~&~a~%"
(bold (format nil "Checking exercises/~a/~a.lisp ..."
(exercise-dir ex) (exercise-name ex))))
(finish-output)
(multiple-value-bind (output code timed-out-p) (multiple-value-bind (output code timed-out-p)
(run-exercise-file (exercise-path ex)) (run-exercise-file (exercise-path ex))
(format t "~a~%" output) (when (and (not timed-out-p) (zerop code))
(cond (timed-out-p (mark-done ex))
(format t "~&~a~%" (red "Timed out.")) (render-screen (make-screen-state
(sb-ext:exit :code 1)) :index (position ex *exercises*)
((zerop code) :output output
(mark-done ex) :result (cond (timed-out-p :timeout)
(format t "~&~a~%" (green (format nil "~a passed!" (exercise-name ex)))) ((zerop code) :pass)
(sb-ext:exit :code 0)) (t :fail)))
(t :clear nil)
(format t "~&~a~%" (red (format nil "~a failed." (exercise-name ex)))) (sb-ext:exit :code (if (and (not timed-out-p) (zerop code)) 0 1)))))
(sb-ext:exit :code 1))))))
(defun hint-command (name) (defun hint-command (name)
(show-hint (resolve-exercise name)) (let* ((ex (resolve-exercise name))
(sb-ext:exit :code 0)) (total (hint-count ex)))
(render-screen (make-screen-state
:index (position ex *exercises*)
:revealed total
:notice (when (zerop total)
"No hint for this one. You're on your own!"))
:clear nil)
(sb-ext:exit :code 0)))
(defun list-command (arg) (defun list-command (arg)
(let ((filter (cond ((null arg) :all) (let ((filter (cond ((null arg) :all)