Skip to content

Commit 0f0a4ce

Browse files
committed
docs: switch cmux workspace lookup to cmux --json (flag before subcommand)
cmux 0.63.2 parses --json as a pre-command flag (CLI/cmux.swift:1647-1650), so the flag must precede the subcommand: `cmux --json list-workspaces`. The reverted `cmux list-workspaces --json` form falls through to commandArgs and is silently ignored, leaving you with text output. The daemon's workspace.list result (TerminalController.swift:3327-3373) yields the field shape `{"workspaces": [{"ref", "title", "selected", "index", "remote", ...}]}`, so jq can exact-match on `.title` instead of grep -F's substring match (fixes kay-schecker's demo / demo2 ambiguity). Adds jq as a prerequisite alongside cmux. Verified by tracing the cmux 0.63.2 source — cmux is a macOS-only AppKit app and can't be run from Linux CI; please sanity-check the rendered output on a real cmux install before merging.
1 parent 38b9fb0 commit 0f0a4ce

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎docs/content/tips-patterns.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,27 +322,33 @@ To create a worktree and immediately attach:
322322

323323
Each worktree gets its own [cmux](https://cmux.com) workspace. Switching worktrees switches workspaces; removing a worktree closes its workspace.
324324

325+
**Prerequisites:** [jq](https://jqlang.org) (`brew install jq`)
326+
325327
```toml
326328
# ~/.config/worktrunk/config.toml
327329
[pre-start]
328330
cmux = "cmux new-workspace --name {{ repo | sanitize }}/{{ branch | sanitize }} --cwd {{ worktree_path }}"
329331

330332
[pre-switch]
331333
cmux = """
332-
WS=$(cmux list-workspaces 2>/dev/null | grep -F {{ repo | sanitize }}/{{ branch | sanitize }} | head -1 | grep -o 'workspace:[0-9]*')
334+
WS=$(cmux --json list-workspaces 2>/dev/null \\
335+
| jq -r --arg t '{{ repo | sanitize }}/{{ branch | sanitize }}' \\
336+
'.workspaces[] | select(.title == $t) | .ref' | head -1)
333337
[ -n "$WS" ] && cmux select-workspace --workspace "$WS" || true
334338
"""
335339

336340
[pre-remove]
337341
cmux = """
338-
WS=$(cmux list-workspaces 2>/dev/null | grep -F {{ repo | sanitize }}/{{ branch | sanitize }} | head -1 | grep -o 'workspace:[0-9]*')
342+
WS=$(cmux --json list-workspaces 2>/dev/null \\
343+
| jq -r --arg t '{{ repo | sanitize }}/{{ branch | sanitize }}' \\
344+
'.workspaces[] | select(.title == $t) | .ref' | head -1)
339345
[ -n "$WS" ] && cmux close-workspace --workspace "$WS" || true
340346
"""
341347
```
342348

343349
**Why `pre-*` instead of `post-*`?** cmux restricts socket access to processes spawned inside a cmux terminal. `post-*` hooks run as detached background processes, breaking the process ancestry chain. `pre-*` hooks run in the foreground and inherit the terminal's process lineage.
344350

345-
The `grep -o 'workspace:[0-9]*'` pattern extracts the workspace ref regardless of whether the workspace is selected (which adds a `*` prefix to the line).
351+
**Why `cmux --json` and not `cmux list-workspaces --json`?** cmux parses `--json` as a pre-command flag, before the subcommand — placed after the subcommand it falls through to the command's argument list and is silently ignored, leaving you with text output. Exact-matching on `.title` then avoids the substring bleed that `grep -F` would cause for sibling workspaces sharing a prefix (e.g., `demo` vs `demo2`).
346352

347353
## Xcode DerivedData cleanup
348354

‎skills/worktrunk/reference/tips-patterns.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,27 +342,33 @@ $ wt switch --create feature -x 'tmux attach -t {{ branch | sanitize }}'
342342

343343
Each worktree gets its own [cmux](https://cmux.com) workspace. Switching worktrees switches workspaces; removing a worktree closes its workspace.
344344

345+
**Prerequisites:** [jq](https://jqlang.org) (`brew install jq`)
346+
345347
```toml
346348
# ~/.config/worktrunk/config.toml
347349
[pre-start]
348350
cmux = "cmux new-workspace --name {{ repo | sanitize }}/{{ branch | sanitize }} --cwd {{ worktree_path }}"
349351

350352
[pre-switch]
351353
cmux = """
352-
WS=$(cmux list-workspaces 2>/dev/null | grep -F {{ repo | sanitize }}/{{ branch | sanitize }} | head -1 | grep -o 'workspace:[0-9]*')
354+
WS=$(cmux --json list-workspaces 2>/dev/null \\
355+
| jq -r --arg t '{{ repo | sanitize }}/{{ branch | sanitize }}' \\
356+
'.workspaces[] | select(.title == $t) | .ref' | head -1)
353357
[ -n "$WS" ] && cmux select-workspace --workspace "$WS" || true
354358
"""
355359

356360
[pre-remove]
357361
cmux = """
358-
WS=$(cmux list-workspaces 2>/dev/null | grep -F {{ repo | sanitize }}/{{ branch | sanitize }} | head -1 | grep -o 'workspace:[0-9]*')
362+
WS=$(cmux --json list-workspaces 2>/dev/null \\
363+
| jq -r --arg t '{{ repo | sanitize }}/{{ branch | sanitize }}' \\
364+
'.workspaces[] | select(.title == $t) | .ref' | head -1)
359365
[ -n "$WS" ] && cmux close-workspace --workspace "$WS" || true
360366
"""
361367
```
362368

363369
**Why `pre-*` instead of `post-*`?** cmux restricts socket access to processes spawned inside a cmux terminal. `post-*` hooks run as detached background processes, breaking the process ancestry chain. `pre-*` hooks run in the foreground and inherit the terminal's process lineage.
364370

365-
The `grep -o 'workspace:[0-9]*'` pattern extracts the workspace ref regardless of whether the workspace is selected (which adds a `*` prefix to the line).
371+
**Why `cmux --json` and not `cmux list-workspaces --json`?** cmux parses `--json` as a pre-command flag, before the subcommand — placed after the subcommand it falls through to the command's argument list and is silently ignored, leaving you with text output. Exact-matching on `.title` then avoids the substring bleed that `grep -F` would cause for sibling workspaces sharing a prefix (e.g., `demo` vs `demo2`).
366372

367373
## Xcode DerivedData cleanup
368374

0 commit comments

Comments
 (0)