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
+261 -93

No files matched your search

+15 -11
View File
@@ -49,19 +49,23 @@ nix run /path/to/lisplings # watch mode, on this directory
## The workflow
1. Run `./lisplings` from the repository root (or your project copy).
2. Edit the exercise file shown in the terminal, in any editor.
3. Save. The exercise runs again and its checks print.
4. When everything passes, press `n` for the next exercise.
2. Lisplings keeps one status screen on your terminal: the current
exercise file, the output of its checks, the hints you revealed, a
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 |
| --- | --------------------------------------- |
| n | move to the next pending exercise |
| h | show a hint |
| r | rerun the current exercise |
| l | list all exercises |
| q | save progress and quit |
| key | action |
| --- | ------------------------------------ |
| n | move to the next pending exercise |
| h | show the hint, pinned on the screen |
| r | rerun the current exercise |
| l | list all exercises |
| q | save progress and quit |
Really stuck? `solutions/` mirrors `exercises/` with every exercise
solved.
+246 -82
View File
@@ -145,11 +145,20 @@ so `reset' can restore them. Start lisplings before editing anything!"
;;; ---------------------------------------------------------------------
;;; 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*
(and (not (or (sb-ext:posix-getenv "NO_COLOR")
(sb-ext:posix-getenv "LISPLINGS_NO_COLOR")))
(sb-ext:posix-getenv "TERM")
(not (string= "dumb" (sb-ext:posix-getenv "TERM")))))
(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")))
(let ((term (sb-ext:posix-getenv "TERM")))
(and term (not (string= "dumb" term)))))))))
(defun ansi (code string)
(if *use-color*
@@ -161,24 +170,58 @@ so `reset' can restore them. Start lisplings before editing anything!"
(defun yellow (s) (ansi 33 s))
(defun bold (s) (ansi 1 s))
(defun stdout-is-tty-p ()
(ignore-errors (plusp (sb-unix:unix-isatty 1))))
(defun clear-screen ()
(when (stdout-is-tty-p)
(format t "~c[H~c[2J~c[3J" #\escape #\escape #\escape)
(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*))
(done (count-if #'exercise-done *exercises*))
(width 28)
(filled (if (zerop total) 0 (floor (* width done) total))))
(format nil "[~a~a] ~d% (~d/~d done)"
(make-string filled :initial-element #\#)
(make-string (- width filled) :initial-element #\-)
(floor (* 100 done) (max 1 total))
done total)))
(percent (floor (* 100 done) (max 1 total))))
(if (< width 50)
(format nil "Progress: ~d/~d (~d%)" done total percent)
(let* ((bar-chars (min 40 (max 10 (- width 30))))
(filled (if (zerop total)
0
(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
@@ -219,24 +262,115 @@ Returns (values output-string exit-code timed-out-p)."
(sb-thread:join-thread killer)
(values output (sb-ext:process-exit-code proc) timed-out)))
(defun run-and-show (ex)
(clear-screen)
(format t "~&~a~%" (bold (format nil "Lisplings · ~a" (exercise-name ex))))
(format t "~&Editing: ~a~%~%" (namestring (exercise-path ex)))
(finish-output)
(multiple-value-bind (output code timed-out-p)
(run-exercise-file (exercise-path ex))
(format t "~a~%" output)
(cond
(timed-out-p
(format t "~&~a~%~%" (red "Timed out after 60 seconds. Infinite loop? Check the base case, or type r to rerun.")))
((zerop code)
(mark-done ex)
(format t "~&~a~%~%" (green "Solved! Press n for the next exercise.")))
(t
(format t "~&~a~%~%" (red "Keep working; the checks rerun every time you save."))))
(format t "~&~a~%~%" (progress-bar))
(format t "n next · h hint · r rerun · l list · q quit~%")
;;; ---------------------------------------------------------------------
;;; The status screen
(defstruct screen-state
;; One watch frame, in memory only. Nothing here is persisted.
(index 0) ; current exercise, an index into *exercises*
(output "") ; output of the last subprocess run
(result :none) ; :none :running :pass :fail :timeout :alldone
(revealed 0) ; hint paragraphs revealed for the current exercise
notice) ; transient one-line message, cleared by render-screen
(defun current-exercise (state)
(nth (screen-state-index state) *exercises*))
(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
(format t "~&~a~%"
(bold (format nil "Lisplings · ~a/~a"
(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)))
;;; ---------------------------------------------------------------------
@@ -250,11 +384,23 @@ Returns (values output-string exit-code timed-out-p)."
(read-line *standard-input* nil nil)
(clear-screen))
(defun show-hint (ex)
(format t "~&~a~%" (yellow (format nil "Hint for ~a:" (exercise-name ex))))
(format t "~&~a~%~%" (or (exercise-hint ex)
"No hint for this one. You're on your own!"))
(finish-output))
(defun run-and-show (state)
"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)))
(defun list-exercises (filter)
(format t "~&~a~%" (bold "Lisplings exercises:"))
@@ -300,55 +446,63 @@ Returns (values output-string exit-code timed-out-p)."
(ignore-errors
(loop for line = (read-line stdin nil :eof)
until (eq line :eof)
do (sb-thread:with-mutex (lock)
(setf (car queue)
(append (car queue)
(list (string-trim " " line))))))))
do (sb-thread:with-mutex (lock)
(setf (car queue)
(append (car queue)
(list (string-trim " " line))))))))
:name "lisplings-input")))
(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*)))
(run-and-show (nth index *exercises*))
(run-and-show state)
(block watch-loop
(loop
(loop for cmd = (pop-command queue lock)
while cmd
do (cond
((string-equal cmd "n")
(let ((current (nth index *exercises*)))
(cond ((not (exercise-done current))
(format t "~&~a~%"
(yellow "This exercise hasn't passed its checks yet. h for a hint, r to rerun.")))
(let ((current (current-exercise state)))
(cond ((not (or (exercise-done current)
(eq (screen-state-result state) :pass)))
(setf (screen-state-notice state)
"This exercise hasn't passed its checks yet. h for a hint, r to rerun.")
(render-screen state))
(t
(let ((next (or (next-pending-index index)
(next-pending-index -1))))
(let ((next (or (next-pending-index
(screen-state-index state))
(next-pending-index -1))))
(cond (next
(setf index next)
(set-current-name (exercise-name (nth index *exercises*)))
(run-and-show (nth index *exercises*)))
(setf (screen-state-index state) next
(screen-state-revealed state) 0)
(set-current-name
(exercise-name (nth next *exercises*)))
(run-and-show state))
(t
(clear-screen)
(format t "~&~a~%~%" (green "All exercises done!"))
(format t "~a~%~%" (info-field :final))
(format t "r rerun · l list · q quit~%~%"))))))))
(setf (screen-state-result state) :alldone)
(render-screen state))))))))
((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 ""))
(run-and-show (nth index *exercises*)))
(run-and-show state))
((string-equal cmd "l")
(list-exercises :all))
(list-exercises :all)
(render-screen state :clear nil))
((string-equal cmd "q")
(return-from watch-loop))
(t
(format t "~&Unknown command: ~a~%" cmd)
(format t "~&n next · h hint · r rerun · l list · q quit~%~%"))))
(let ((was (exercise-mtime (nth index *exercises*))))
(sleep 0.2)
(let ((now (exercise-mtime (nth index *exercises*))))
(unless (eql now was)
(run-and-show (nth index *exercises*)))))))
(save-state)
(format t "~&Bye! Progress saved.~%"))))
(setf (screen-state-notice state)
(format nil "Unknown command: ~a" cmd))
(render-screen state))))
(let ((was (exercise-mtime (current-exercise state))))
(sleep 0.2)
(let ((now (exercise-mtime (current-exercise state))))
(unless (eql now was)
(run-and-show state)))))))
(save-state)
(format t "~&Bye! Progress saved.~%")))
;;; ---------------------------------------------------------------------
;;; One-shot commands
@@ -361,23 +515,33 @@ Returns (values output-string exit-code timed-out-p)."
(let ((ex (resolve-exercise name)))
(set-current-name (exercise-name ex))
(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)
(run-exercise-file (exercise-path ex))
(format t "~a~%" output)
(cond (timed-out-p
(format t "~&~a~%" (red "Timed out."))
(sb-ext:exit :code 1))
((zerop code)
(mark-done ex)
(format t "~&~a~%" (green (format nil "~a passed!" (exercise-name ex))))
(sb-ext:exit :code 0))
(t
(format t "~&~a~%" (red (format nil "~a failed." (exercise-name ex))))
(sb-ext:exit :code 1))))))
(when (and (not timed-out-p) (zerop code))
(mark-done ex))
(render-screen (make-screen-state
:index (position ex *exercises*)
:output output
:result (cond (timed-out-p :timeout)
((zerop code) :pass)
(t :fail)))
:clear nil)
(sb-ext:exit :code (if (and (not timed-out-p) (zerop code)) 0 1)))))
(defun hint-command (name)
(show-hint (resolve-exercise name))
(sb-ext:exit :code 0))
(let* ((ex (resolve-exercise name))
(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)
(let ((filter (cond ((null arg) :all)