From 516b256ea3f1c36c071a0893d21b6f0672bd959f Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 09:50:52 +0000 Subject: [PATCH] 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. --- README.md | 6 ++++++ src/core.lisp | 8 +++++++- src/exercise-runner.lisp | 8 +++++--- src/harness.lisp | 34 ++++++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 875c790..bf178da 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,12 @@ runs one exercise (`src/exercise-runner.lisp`). 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//.lisp` and the solved `solutions//.lisp`, add a `(:name ... :dir ... :hint ...)` entry to `info.sexp`, and run `dev/verify.sh`. diff --git a/src/core.lisp b/src/core.lisp index b0159a6..f47d013 100644 --- a/src/core.lisp +++ b/src/core.lisp @@ -25,6 +25,9 @@ (defparameter *exercises* nil) ; list of exercise structs (defparameter *state* nil) ; (:done ("name" ...) :current "name") +;; setup reads this before the terminal helpers define it. +(declaim (special *use-color*)) + (defstruct exercise name dir hint done path solution-path) @@ -57,7 +60,10 @@ *state* (load-state) *exercises* (make-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 () (mapcar (lambda (spec) diff --git a/src/exercise-runner.lisp b/src/exercise-runner.lisp index 6dadab1..badba23 100644 --- a/src/exercise-runner.lisp +++ b/src/exercise-runner.lisp @@ -34,13 +34,15 @@ (load (sibling-pathname "harness")) (load exercise)) (error (e) - (format *error-output* "~&~%The exercise did not load:~%") - (format *error-output* " ~a~%" e) + (format *error-output* "~&~%~a~%" + (%lisplings/color 31 "The exercise did not load:")) + (format *error-output* " ~a~%" e) (finish-output *error-output*) (sb-ext:exit :code 2))) (handler-case (lisplings/run-checks) (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*) (sb-ext:exit :code 3))))))) diff --git a/src/harness.lisp b/src/harness.lisp index 6d0cdbb..c5acbfc 100644 --- a/src/harness.lisp +++ b/src/harness.lisp @@ -35,12 +35,31 @@ "Record a check: FORM must signal an error of type CONDITION-TYPE." `(%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) - (format t "~&ok ~s~%" (lisplings-check-description check)) + (format t "~&~a ~s~%" (%lisplings/color 32 "ok") + (lisplings-check-description check)) t) (defun %lisplings/fail (check explanation) - (format t "~&FAIL ~s~% ~a~%" + (format t "~&~a ~s~% ~a~%" (%lisplings/color 31 "FAIL") (lisplings-check-description check) explanation) nil) @@ -92,10 +111,17 @@ Exit code 0 means every check passed." (cond ((zerop total) (sb-ext:exit :code 0)) ((= 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*) (sb-ext:exit :code 0)) (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*) (sb-ext:exit :code 1)))))