From 77843402fa0cb01afdbf8d9578cd0a65cba9efc4 Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 10:02:00 +0000 Subject: [PATCH] Number the exercise list and add jump-to (wave 3, F5) list-exercises prints 1-based right-aligned numbers. In watch mode, l lists the exercises and then asks Jump to (name or number, empty stays): a name or catalogue number switches to that exercise (revealed hints reset, :current saved), an empty answer or EOF stays, and unknown names or out-of-range numbers show a notice. No new CLI command: run [name] already selects the exercise for watch mode; usage and README note it. --- README.md | 4 ++-- src/core.lisp | 63 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e22fabd..c496a88 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Keys in watch mode (each key is a line: type it, then press ENTER): | n | move to the next pending exercise | | h | reveal the next hint paragraph (stays pinned) | | r | rerun the current exercise | -| l | list all exercises | +| l | list exercises, then jump by name or number | | x | reset the current exercise (asks y/n) | | q | save progress and quit | @@ -78,7 +78,7 @@ solved. ```sh ./lisplings watch the current exercise (the default) -./lisplings run [name] run one exercise and print its output +./lisplings run [name] run one exercise; also selects it as current ./lisplings hint [name] print every hint paragraph, numbered ./lisplings list [pending|done|all] ./lisplings reset restore an exercise to its pristine state diff --git a/src/core.lisp b/src/core.lisp index f2659be..ff1607c 100644 --- a/src/core.lisp +++ b/src/core.lisp @@ -431,14 +431,18 @@ on the fresh frame." (defun list-exercises (filter) (format t "~&~a~%" (bold "Lisplings exercises:")) - (dolist (ex *exercises*) - (when (ecase filter - (:all t) - (:pending (not (exercise-done ex))) - (:done (exercise-done ex))) - (format t " ~a ~a~%" - (if (exercise-done ex) (green "✓ ") (yellow ". ")) - (format nil "~a/~a" (exercise-dir ex) (exercise-name ex))))) + (let ((width (length (format nil "~d" (max 1 (length *exercises*)))))) + (loop for ex in *exercises* + for number from 1 + do (when (ecase filter + (:all t) + (:pending (not (exercise-done ex))) + (:done (exercise-done ex))) + (format t " ~vd ~a ~a~%" + width number + (if (exercise-done ex) (green "✓ ") (yellow ". ")) + (format nil "~a/~a" + (exercise-dir ex) (exercise-name ex)))))) (format t "~&~%Pending: ~d of ~d. Progress: ~a~%~%" (count-if (complement #'exercise-done) *exercises*) (length *exercises*) @@ -506,6 +510,39 @@ and rerun it, on no (or EOF) just say so." (setf (screen-state-notice state) "Reset cancelled.") (render-screen state)))))))) +(defun jump-prompt (state queue lock open-p) + "Jump to the exercise the user names or numbers after the list; an +empty answer (or EOF) stays on the current exercise." + (let ((answer (prompt-line "Jump to (name or number, empty stays): " + queue lock open-p))) + (cond ((or (null answer) (string= answer "")) + (render-screen state)) + (t + (let ((number (ignore-errors (parse-integer answer :junk-allowed t)))) + (cond ((and (integerp number) + (<= 1 number) (<= number (length *exercises*))) + (setf (screen-state-index state) (1- number) + (screen-state-revealed state) 0) + (set-current-name (exercise-name (nth (1- number) *exercises*))) + (run-and-show state queue lock)) + ((integerp number) + (setf (screen-state-notice state) + (format nil "No exercise number ~d." number)) + (render-screen state)) + (t + (let ((match (find answer *exercises* :key #'exercise-name + :test #'string=))) + (cond (match + (setf (screen-state-index state) + (position match *exercises*) + (screen-state-revealed state) 0) + (set-current-name (exercise-name match)) + (run-and-show state queue lock)) + (t + (setf (screen-state-notice state) + (format nil "No exercise matches '~a'." answer)) + (render-screen state))))))))))) + (defun watch () (ensure-orig-snapshot) (unless (probe-file (state-path)) @@ -563,9 +600,9 @@ and rerun it, on no (or EOF) just say so." (render-screen state)) ((or (string-equal cmd "r") (string= cmd "")) (run-and-show state queue lock)) - ((string-equal cmd "l") - (list-exercises :all) - (render-screen state :clear nil)) + ((string-equal cmd "l") + (list-exercises :all) + (jump-prompt state queue lock open-p)) ((string-equal cmd "x") (reset-prompt state queue lock open-p)) ((string-equal cmd "q") @@ -708,8 +745,8 @@ and rerun it, on no (or EOF) just say so." (format t "Running lisplings with no command starts the watcher: it runs~%") (format t "the current exercise and reruns it whenever you save the file.~%~%") (format t "Commands:~%") - (format t " run [name] run one exercise and print its output~%") - (format t " hint [name] print the hint for an exercise~%") + (format t " run [name] run one exercise, and select it for watch mode~%") + (format t " hint [name] print every hint paragraph, numbered~%") (format t " list [pending|done|all] list exercises with their status~%") (format t " reset restore an exercise to its pristine state~%") (format t " check-all run every exercise and resync progress~%")