Color check output, gated by LISPLINGS_COLOR (wave 3, F3)

The exercise subprocess writes to a pipe in watch mode and cannot see the parent's terminal, so setup now sets LISPLINGS_COLOR=always/never and the harness colorizes its own report: green ok, red FAIL, bold summaries, red failure headers in exercise-runner.lisp.

Piped output stays ANSI-free unless forced: NO_COLOR and TERM=dumb are honored, and LISPLINGS_COLOR=always ./lisplings ... | cat -v forces codes for debugging. A (declaim (special *use-color*)) lets setup read the value without a compile-time warning.
This commit is contained in:
venomade committed 2026-11-22 09:50:52 +00:00
1 parent b62d32d995
commit 516b256ea3
4 files changed
+47 -7

No files matched your search

+6
View File
@@ -140,6 +140,12 @@ runs one exercise (`src/exercise-runner.lisp`).
dev/verify.sh # every exercise fails when broken, passes when solved dev/verify.sh # every exercise fails when broken, passes when solved
``` ```
The watcher shows color only on a terminal, and turns it off for
`NO_COLOR` or `TERM=dumb`. The exercise subprocess writes to a pipe,
so it cannot detect the terminal itself: the watcher passes the
decision on as `LISPLINGS_COLOR=always` or `=never`. To debug piped
output, force it with `LISPLINGS_COLOR=always ./lisplings ... | cat -v`.
To add an exercise: write the broken `exercises/<chapter>/<name>.lisp` To add an exercise: write the broken `exercises/<chapter>/<name>.lisp`
and the solved `solutions/<chapter>/<name>.lisp`, add a `(:name ... and the solved `solutions/<chapter>/<name>.lisp`, add a `(:name ...
:dir ... :hint ...)` entry to `info.sexp`, and run `dev/verify.sh`. :dir ... :hint ...)` entry to `info.sexp`, and run `dev/verify.sh`.
+7 -1
View File
@@ -25,6 +25,9 @@
(defparameter *exercises* nil) ; list of exercise structs (defparameter *exercises* nil) ; list of exercise structs
(defparameter *state* nil) ; (:done ("name" ...) :current "name") (defparameter *state* nil) ; (:done ("name" ...) :current "name")
;; setup reads this before the terminal helpers define it.
(declaim (special *use-color*))
(defstruct exercise (defstruct exercise
name dir hint done path solution-path) name dir hint done path solution-path)
@@ -57,7 +60,10 @@
*state* (load-state) *state* (load-state)
*exercises* (make-exercises)) *exercises* (make-exercises))
(unless (plusp (length *exercises*)) (unless (plusp (length *exercises*))
(error "The exercise catalogue (info.sexp) is empty."))) (error "The exercise catalogue (info.sexp) is empty."))
;; The exercise subprocess writes to a pipe, so it cannot see the
;; terminal; tell it whether the parent is showing color.
(sb-posix:setenv "LISPLINGS_COLOR" (if *use-color* "always" "never") 1))
(defun make-exercises () (defun make-exercises ()
(mapcar (lambda (spec) (mapcar (lambda (spec)
+4 -2
View File
@@ -34,13 +34,15 @@
(load (sibling-pathname "harness")) (load (sibling-pathname "harness"))
(load exercise)) (load exercise))
(error (e) (error (e)
(format *error-output* "~&~%The exercise did not load:~%") (format *error-output* "~&~%~a~%"
(%lisplings/color 31 "The exercise did not load:"))
(format *error-output* " ~a~%" e) (format *error-output* " ~a~%" e)
(finish-output *error-output*) (finish-output *error-output*)
(sb-ext:exit :code 2))) (sb-ext:exit :code 2)))
(handler-case (handler-case
(lisplings/run-checks) (lisplings/run-checks)
(error (e) (error (e)
(format *error-output* "~&~%The check harness failed: ~a~%" e) (format *error-output* "~&~%~a ~a~%"
(%lisplings/color 31 "The check harness failed:") e)
(finish-output *error-output*) (finish-output *error-output*)
(sb-ext:exit :code 3))))))) (sb-ext:exit :code 3)))))))
+30 -4
View File
@@ -35,12 +35,31 @@
"Record a check: FORM must signal an error of type CONDITION-TYPE." "Record a check: FORM must signal an error of type CONDITION-TYPE."
`(%lisplings/record ',form :error ,condition-type (lambda () ,form))) `(%lisplings/record ',form :error ,condition-type (lambda () ,form)))
;;; Color, decided once per subprocess. The watcher sets
;;; LISPLINGS_COLOR to always or never because this process writes
;;; to a pipe and cannot see the parent's terminal.
(defun lisplings-color-on-p ()
(let ((spec (sb-ext:posix-getenv "LISPLINGS_COLOR")))
(cond ((string-equal spec "always") t)
((string-equal spec "never") nil)
(t (and (not (sb-ext:posix-getenv "NO_COLOR"))
(ignore-errors (plusp (sb-unix:unix-isatty 1))))))))
(defparameter *lisplings/color* (lisplings-color-on-p))
(defun %lisplings/color (code string)
(if *lisplings/color*
(format nil "~c[~dm~a~c[0m" #\escape code string #\escape)
string))
(defun %lisplings/ok (check) (defun %lisplings/ok (check)
(format t "~&ok ~s~%" (lisplings-check-description check)) (format t "~&~a ~s~%" (%lisplings/color 32 "ok")
(lisplings-check-description check))
t) t)
(defun %lisplings/fail (check explanation) (defun %lisplings/fail (check explanation)
(format t "~&FAIL ~s~% ~a~%" (format t "~&~a ~s~% ~a~%" (%lisplings/color 31 "FAIL")
(lisplings-check-description check) explanation) (lisplings-check-description check) explanation)
nil) nil)
@@ -92,10 +111,17 @@ Exit code 0 means every check passed."
(cond ((zerop total) (cond ((zerop total)
(sb-ext:exit :code 0)) (sb-ext:exit :code 0))
((= passed total) ((= passed total)
(format t "~&~%All ~d check~:p passed!~%" total) (format t "~&~%~a~%"
(%lisplings/color 1
(%lisplings/color 32
(format nil "All ~d check~:p passed!" total))))
(finish-output *standard-output*) (finish-output *standard-output*)
(sb-ext:exit :code 0)) (sb-ext:exit :code 0))
(t (t
(format t "~&~%~d of ~d check~:p failed.~%" (- total passed) total) (format t "~&~%~a~%"
(%lisplings/color 1
(%lisplings/color 31
(format nil "~d of ~d check~:p failed."
(- total passed) total))))
(finish-output *standard-output*) (finish-output *standard-output*)
(sb-ext:exit :code 1))))) (sb-ext:exit :code 1)))))