Chapter intros on chapter entry (wave 5, backlog A3)

Watch mode prints the chapter README above the exercise when the
chapter changes: on the first frame, on n, and on jump-to. A move
inside the same chapter shows a one-line pointer instead, and the
block prints once, not on every rerun. `run <name>` prints the
target chapter's README too.
This commit is contained in:
venomade committed 2026-11-22 11:45:14 +00:00
1 parent 40af05dbff
commit 7424463d5e
2 files changed
+86 -9

No files matched your search

+3 -1
View File
@@ -51,7 +51,9 @@ nix run /path/to/lisplings # watch mode, on this directory
1. Run `./lisplings` from the repository root (or your project copy). 1. Run `./lisplings` from the repository root (or your project copy).
2. Lisplings keeps one status screen on your terminal: the current 2. Lisplings keeps one status screen on your terminal: the current
exercise file, the output of its checks, the hints you revealed, a exercise file, the output of its checks, the hints you revealed, a
progress bar, and the keys that work right now. progress bar, and the keys that work right now. Crossing into a new
chapter prints that chapter's README above the exercise; `run <name>`
prints the target chapter's README too.
3. Edit that file in any editor and save. The exercise runs again and 3. Edit that file in any editor and save. The exercise runs again and
the screen redraws; when every check passes it shows `Exercise the screen redraws; when every check passes it shows `Exercise
done ✓` and the matching solution to compare against. done ✓` and the matching solution to compare against.
+83 -8
View File
@@ -305,6 +305,55 @@ the width, and terminals under 50 columns get text only."
percent percent
(bold (format nil "~d/~d" done total))))))) (bold (format nil "~d/~d" done total)))))))
;;; ---------------------------------------------------------------------
;;; Chapter introductions
(defun chapter-readme-body (dir)
"The body of exercises/<dir>/README.md without its leading title
line, or NIL when the chapter has no README. Leading and trailing
blank lines are trimmed."
(let ((path (merge-pathnames (format nil "exercises/~a/README.md" dir)
*project*)))
(when (probe-file path)
(with-open-file (in path)
(let* ((first (read-line in nil nil))
(rest (with-output-to-string (out)
(loop for line = (read-line in nil :eof)
until (eq line :eof)
do (write-line line out)))))
(when first
(let ((body (string-trim '(#\newline #\return #\space #\tab)
(if (and (>= (length first) 2)
(string= "# " first :end2 2))
rest
(concatenate 'string first
(string #\newline)
rest)))))
(and (plusp (length body)) body))))))))
(defun print-chapter-intro (dir body)
"Print one chapter README as a framed block: a rule naming the
chapter, the body, and a closing rule."
(let* ((width (terminal-width))
(label (format nil "---- ~a " dir))
(rule (make-string (max 4 (- width (length label)))
:initial-element #\-)))
(format t "~&~a~a~%" label rule)
(format t "~a~%" body)
(format t "~a~%" (make-string width :initial-element #\-))))
(defun chapter-note-for (old-dir new-index)
"The chapter note for a move to the exercise at NEW-INDEX, coming
from a position in chapter OLD-DIR (NIL before the first frame): the
chapter README block when the chapter changed, a one-line pointer
when the move stays inside it."
(let* ((ex (nth new-index *exercises*))
(dir (exercise-dir ex)))
(cond ((and old-dir (string= dir old-dir))
(and (chapter-readme-body dir) (list :line dir)))
(t (let ((body (chapter-readme-body dir)))
(and body (list :intro dir body)))))))
;;; --------------------------------------------------------------------- ;;; ---------------------------------------------------------------------
;;; Running exercises ;;; Running exercises
@@ -353,7 +402,8 @@ Returns (values output-string exit-code timed-out-p)."
(output "") ; output of the last subprocess run (output "") ; output of the last subprocess run
(result :none) ; :none :running :pass :fail :timeout :alldone (result :none) ; :none :running :pass :fail :timeout :alldone
(revealed 0) ; hint paragraphs revealed for the current exercise (revealed 0) ; hint paragraphs revealed for the current exercise
notice) ; transient one-line message, cleared by render-screen notice ; transient one-line message, cleared by render-screen
chapter-note) ; chapter README block or pointer, set on index change
(defun current-exercise (state) (defun current-exercise (state)
(nth (screen-state-index state) *exercises*)) (nth (screen-state-index state) *exercises*))
@@ -417,6 +467,17 @@ always prints linearly."
(format t "~&Current exercise: ~a~%" (format t "~&Current exercise: ~a~%"
(namestring (exercise-path ex))) (namestring (exercise-path ex)))
(format t "~%") (format t "~%")
(let ((note (screen-state-chapter-note state)))
(when note
(setf (screen-state-chapter-note state) nil)
(if (eq (first note) :intro)
(progn
(print-chapter-intro (second note) (third note))
(format t "~%"))
(format t "~&~a~%"
(yellow (format nil
"Chapter intro: exercises/~a/README.md"
(second note)))))))
(cond ((eq (screen-state-result state) :running) (cond ((eq (screen-state-result state) :running)
(format t "~&~a~%" (format t "~&~a~%"
(bold (format nil "Checking exercises/~a/~a.lisp ..." (bold (format nil "Checking exercises/~a/~a.lisp ..."
@@ -588,10 +649,13 @@ empty answer (or EOF) stays on the current exercise."
(let ((number (ignore-errors (parse-integer answer :junk-allowed t)))) (let ((number (ignore-errors (parse-integer answer :junk-allowed t))))
(cond ((and (integerp number) (cond ((and (integerp number)
(<= 1 number) (<= number (length *exercises*))) (<= 1 number) (<= number (length *exercises*)))
(let ((old-dir (exercise-dir (current-exercise state))))
(setf (screen-state-index state) (1- number) (setf (screen-state-index state) (1- number)
(screen-state-revealed state) 0) (screen-state-revealed state) 0
(screen-state-chapter-note state)
(chapter-note-for old-dir (1- number)))
(set-current-name (exercise-name (nth (1- number) *exercises*))) (set-current-name (exercise-name (nth (1- number) *exercises*)))
(run-and-show state queue lock)) (run-and-show state queue lock)))
((integerp number) ((integerp number)
(setf (screen-state-notice state) (setf (screen-state-notice state)
(format nil "No exercise number ~d." number)) (format nil "No exercise number ~d." number))
@@ -600,11 +664,14 @@ empty answer (or EOF) stays on the current exercise."
(let ((match (find answer *exercises* :key #'exercise-name (let ((match (find answer *exercises* :key #'exercise-name
:test #'string=))) :test #'string=)))
(cond (match (cond (match
(setf (screen-state-index state) (let* ((new-index (position match *exercises*))
(position match *exercises*) (old-dir (exercise-dir (current-exercise state))))
(screen-state-revealed state) 0) (setf (screen-state-index state) new-index
(screen-state-revealed state) 0
(screen-state-chapter-note state)
(chapter-note-for old-dir new-index))
(set-current-name (exercise-name match)) (set-current-name (exercise-name match))
(run-and-show state queue lock)) (run-and-show state queue lock)))
(t (t
(setf (screen-state-notice state) (setf (screen-state-notice state)
(format nil "No exercise matches '~a'." answer)) (format nil "No exercise matches '~a'." answer))
@@ -636,6 +703,8 @@ empty answer (or EOF) stays on the current exercise."
(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*)))
(setf (screen-state-chapter-note state)
(chapter-note-for nil index))
(run-and-show state queue lock) (run-and-show state queue lock)
(block watch-loop (block watch-loop
(loop (loop
@@ -655,7 +724,9 @@ empty answer (or EOF) stays on the current exercise."
(next-pending-index -1)))) (next-pending-index -1))))
(cond (next (cond (next
(setf (screen-state-index state) next (setf (screen-state-index state) next
(screen-state-revealed state) 0) (screen-state-revealed state) 0
(screen-state-chapter-note state)
(chapter-note-for (exercise-dir current) next))
(set-current-name (set-current-name
(exercise-name (nth next *exercises*))) (exercise-name (nth next *exercises*)))
(run-and-show state queue lock)) (run-and-show state queue lock))
@@ -697,6 +768,10 @@ empty answer (or EOF) stays on the current exercise."
(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)
(let ((body (chapter-readme-body (exercise-dir ex))))
(when body
(print-chapter-intro (exercise-dir ex) body)
(format t "~%")))
(format t "~&~a~%" (format t "~&~a~%"
(bold (format nil "Checking exercises/~a/~a.lisp ..." (bold (format nil "Checking exercises/~a/~a.lisp ..."
(exercise-dir ex) (exercise-name ex)))) (exercise-dir ex) (exercise-name ex))))