aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/ai-vterm.el36
-rw-r--r--tests/test-ai-vterm--dispatch.el22
-rw-r--r--tests/test-ai-vterm--pick-project.el21
3 files changed, 63 insertions, 16 deletions
diff --git a/modules/ai-vterm.el b/modules/ai-vterm.el
index 670aa43a..9b47e33d 100644
--- a/modules/ai-vterm.el
+++ b/modules/ai-vterm.el
@@ -434,12 +434,28 @@ Returns the buffer."
(display-buffer buf)
buf)))))
+(defun cj/--ai-vterm-format-candidate (path)
+ "Return the display name for PATH in the AI-vterm project picker.
+
+Appends \" [running]\" when the project's claude buffer exists with
+a live process, so the user sees at a glance which projects already
+have a session. Path is abbreviated via `abbreviate-file-name' so
+it reads as ~/code/foo rather than the full home-dir form."
+ (let* ((name (cj/--ai-vterm-buffer-name path))
+ (buf (get-buffer name))
+ (running (and buf (cj/--ai-vterm-process-live-p buf)))
+ (display-path (abbreviate-file-name path)))
+ (if running
+ (format "%s [running]" display-path)
+ display-path)))
+
(defun cj/--ai-vterm-pick-project ()
"Prompt for a Claude-template project; return its absolute path.
Candidates come from `cj/--ai-vterm-candidates'. Display uses
-`abbreviate-file-name' so paths read as ~/code/foo instead of the
-full home-dir form. Signals `user-error' when no candidates exist."
+`cj/--ai-vterm-format-candidate', which abbreviates the path and
+flags projects with a live session via a \" [running]\" suffix.
+Signals `user-error' when no candidates exist."
(let ((candidates (cj/--ai-vterm-candidates)))
(unless candidates
(user-error "No Claude-template projects found under %s"
@@ -448,7 +464,7 @@ full home-dir form. Signals `user-error' when no candidates exist."
cj/ai-vterm-container-roots)
", ")))
(let* ((display-alist
- (mapcar (lambda (p) (cons (abbreviate-file-name p) p))
+ (mapcar (lambda (p) (cons (cj/--ai-vterm-format-candidate p) p))
candidates))
(chosen (completing-read "AI vterm project: "
display-alist nil t)))
@@ -460,8 +476,14 @@ full home-dir form. Signals `user-error' when no candidates exist."
Returns one of:
- (toggle-off . WINDOW) -- claude is displayed in WINDOW; quit it.
-- (redisplay-single . BUFFER) -- exactly one alive claude buffer; show it.
-- (pick-project) -- zero or 2+ alive claude buffers; prompt.
+- (redisplay-recent . BUFFER) -- 1+ alive claude buffers; show MRU.
+- (pick-project) -- zero alive claude buffers; prompt.
+
+When 2+ claude buffers are alive, F9 redisplays the most-recently-
+selected one rather than opening the project picker. C-F9 is the
+explicit \"start a different project\" surface; M-F9 is the explicit
+\"switch among existing claudes\" surface. F9 keeps a single, simple
+job: toggle whichever claude was last in use.
A pure-decision helper so the dispatch logic is exercisable in tests
without firing real `display-buffer' or `quit-window' calls."
@@ -471,7 +493,7 @@ without firing real `display-buffer' or `quit-window' calls."
(t
(let ((buffers (cj/--ai-vterm-claude-buffers)))
(cond
- ((= (length buffers) 1) (cons 'redisplay-single (car buffers)))
+ (buffers (cons 'redisplay-recent (car buffers)))
(t '(pick-project))))))))
(defun cj/--ai-vterm-pick-buffer-candidates (buffers shown-buffer)
@@ -587,7 +609,7 @@ AI-vterm buffers without touching the project list."
(bury-buffer (window-buffer win))
(delete-window win))
nil)
- (`(redisplay-single . ,buf)
+ (`(redisplay-recent . ,buf)
(display-buffer buf)
(unless arg
(let ((w (get-buffer-window buf)))
diff --git a/tests/test-ai-vterm--dispatch.el b/tests/test-ai-vterm--dispatch.el
index 3c0ae766..030b200d 100644
--- a/tests/test-ai-vterm--dispatch.el
+++ b/tests/test-ai-vterm--dispatch.el
@@ -2,10 +2,10 @@
;;; Commentary:
;; The dispatch helper is a pure decision function used by F9.
-;; Returns one of (toggle-off . WIN), (redisplay-single . BUF),
+;; Returns one of (toggle-off . WIN), (redisplay-recent . BUF),
;; or (pick-project) based on whether a claude buffer is currently
-;; displayed and how many alive claude buffers exist. Tests mock the
-;; two underlying helpers so the dispatch logic can be exercised
+;; displayed and whether any alive claude buffers exist. Tests mock
+;; the two underlying helpers so the dispatch logic can be exercised
;; without touching real windows.
;;; Code:
@@ -30,8 +30,8 @@
(should (equal (cj/--ai-vterm-dispatch)
(cons 'toggle-off sentinel-win))))))
-(ert-deftest test-ai-vterm--dispatch-no-window-single-buffer-returns-redisplay ()
- "Normal: no displayed claude, exactly one alive buffer -> redisplay-single."
+(ert-deftest test-ai-vterm--dispatch-no-window-single-buffer-returns-redisplay-recent ()
+ "Normal: no displayed claude, one alive buffer -> redisplay-recent + buffer."
(test-ai-vterm--dispatch-cleanup)
(let ((b1 (get-buffer-create "claude [single]")))
(unwind-protect
@@ -40,11 +40,14 @@
((symbol-function 'cj/--ai-vterm-claude-buffers)
(lambda () (list b1))))
(should (equal (cj/--ai-vterm-dispatch)
- (cons 'redisplay-single b1))))
+ (cons 'redisplay-recent b1))))
(kill-buffer b1))))
-(ert-deftest test-ai-vterm--dispatch-no-window-multiple-buffers-returns-pick-project ()
- "Normal: no displayed claude, 2+ alive buffers -> pick-project."
+(ert-deftest test-ai-vterm--dispatch-no-window-multiple-buffers-returns-redisplay-recent ()
+ "Normal: no displayed claude, 2+ alive buffers -> redisplay-recent + MRU.
+F9 redisplays the most-recently-selected claude (head of buffer-list
+order) rather than opening the project picker, so the user toggles
+THE claude they were last using. Other claudes are reachable via M-F9."
(test-ai-vterm--dispatch-cleanup)
(let ((b1 (get-buffer-create "claude [a]"))
(b2 (get-buffer-create "claude [b]")))
@@ -53,7 +56,8 @@
(lambda (&optional _frame) nil))
((symbol-function 'cj/--ai-vterm-claude-buffers)
(lambda () (list b1 b2))))
- (should (equal (cj/--ai-vterm-dispatch) '(pick-project))))
+ (should (equal (cj/--ai-vterm-dispatch)
+ (cons 'redisplay-recent b1))))
(kill-buffer b1)
(kill-buffer b2))))
diff --git a/tests/test-ai-vterm--pick-project.el b/tests/test-ai-vterm--pick-project.el
index 6fa2d185..fd5295bf 100644
--- a/tests/test-ai-vterm--pick-project.el
+++ b/tests/test-ai-vterm--pick-project.el
@@ -44,5 +44,26 @@
(cj/--ai-vterm-pick-project)
(should (equal (caar received-collection) "~/code/foo")))))
+(ert-deftest test-ai-vterm--format-candidate-flags-running-project ()
+ "Normal: a path whose claude buffer has a live process gets a [running] suffix."
+ (let* ((path (expand-file-name "~/code/already-running"))
+ (buffer-name (cj/--ai-vterm-buffer-name path))
+ (buf (get-buffer-create buffer-name)))
+ (unwind-protect
+ (cl-letf (((symbol-function 'cj/--ai-vterm-process-live-p)
+ (lambda (b) (eq b buf))))
+ (should (equal (cj/--ai-vterm-format-candidate path)
+ (format "%s [running]" (abbreviate-file-name path)))))
+ (kill-buffer buf))))
+
+(ert-deftest test-ai-vterm--format-candidate-omits-flag-when-not-running ()
+ "Boundary: a path with no buffer or no live process -> plain abbreviated path."
+ (let ((path (expand-file-name "~/code/not-running")))
+ ;; Make sure no claude buffer exists for this path.
+ (let ((bn (cj/--ai-vterm-buffer-name path)))
+ (when (get-buffer bn) (kill-buffer bn)))
+ (should (equal (cj/--ai-vterm-format-candidate path)
+ (abbreviate-file-name path)))))
+
(provide 'test-ai-vterm--pick-project)
;;; test-ai-vterm--pick-project.el ends here