Skip to content

feat(switch): add --print flag for scripting - #1404

Closed
ruudk wants to merge 3 commits into
max-sixty:mainfrom
ruudk:switch-print
Closed

ruudk wants to merge 3 commits into
max-sixty:mainfrom
ruudk:switch-print

Conversation

@ruudk

@ruudk ruudk commented Mar 9, 2026

Copy link
Copy Markdown

First of all, thanks for this great project! I just started with it a few days ago and I love it!!

I'd like to be able to script a bit more and thought it would be nice to include a --print option to the wt switch command.

It opens the interactive picker and then when you press enter, it prints the selected working tree back. This can be used to automate/script things easier for custom workflows.

Curious what you think :)

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean addition — the flag is well-scoped (picker-only, conflicts with mutually exclusive options) and the implementation is minimal.

Two observations:

  1. --print doesn't guard against alt-r (remove) — see inline comment.

  2. No test for the --print behavior itself. The help snapshots are updated, but there's no test asserting that --print actually prints and exits without switching. This is hard to test with the TUI picker, but worth noting — a regression here would be silent.

Comment thread src/commands/select/mod.rs Resolved

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing on all three platforms with the same error — the check_no_stdout_in_commands output guard rejects println! in command code.

The project requires either using worktrunk::styling::println (for styled output) or adding the file to STDOUT_ALLOWED_PATHS (for intentional data output). Since --print is explicitly data output for scripting, adding "select/mod.rs" to the allowlist is the right fix:

// --print flag: branch name output for scripting
"select/mod.rs",

@ruudk
ruudk force-pushed the switch-print branch 2 times, most recently from 9d7d943 to 2c17d06 Compare March 9, 2026 08:40
worktrunk-bot
worktrunk-bot previously approved these changes Mar 9, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows CI fails with field 'print' is never read — handle_select is unix-only, so spec.print is dead code on Windows. The fix is to add it to the existing suppression on line 504:

let _ = (spec.branches, spec.remotes, spec.print);

This line isn't in the diff so I can't post an inline suggestion — happy to push a fix commit if that helps.

@worktrunk-bot
worktrunk-bot dismissed their stale review March 9, 2026 08:50

CI failed on Windows — unused field print on non-unix

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing on all platforms — cargo fmt wants the bail! message split across lines:

anyhow::bail!(
    "--print is read-only and cannot be combined with remove (alt-r)"
);

(line 332 in src/commands/select/mod.rs)

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing on all platforms — cargo fmt needs the bail! macro reformatted. See inline suggestion.

Comment thread src/commands/select/mod.rs Resolved
Opens the interactive picker and prints the selected branch name to
stdout without switching. Useful for scripting and automation, e.g.
`branch=$(wt switch --print)`.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All previous feedback has been addressed — output guard, Windows suppression, formatting, and the alt-r guard are all handled.

One minor note on the new .expect() at line 329 — see inline comment.

Comment thread src/commands/select/mod.rs Resolved
worktrunk-bot added a commit that referenced this pull request Mar 9, 2026
GitHub's dismiss API returns 404 on cross-repository PRs, so the bot
cannot undo an approval if CI fails. This left stale approvals
contradicting red CI on fork PRs (observed in run 22847633269 on #1404).

The fix defers approval to after CI passes for fork PRs, eliminating the
stale-approval window entirely.

Fixes #1405

Co-Authored-By: Claude Opus 4.6 <[email protected]>
ruudk added 2 commits March 9, 2026 11:23
Extract resolve_print_identifier() from picker callback so the
--print logic can be tested without an interactive terminal.
@ruudk

ruudk commented Mar 9, 2026

Copy link
Copy Markdown
Author

@max-sixty It's finally ready 😁

@max-sixty

Copy link
Copy Markdown
Owner

I was trying to think whether there was a way of getting this without adding another option.

we could add a shortcut like alt-p. do you think that would suffice? or it's going to lead to lots of cursing when you inevitably hit 'return' and it goes and switches branch? 😀

possibly we could default to this when we run the picker with --no-cd?!

@ruudk

ruudk commented Mar 11, 2026

Copy link
Copy Markdown
Author

we could add a shortcut like alt-p. do you think that would suffice? or it's going to lead to lots of cursing when you inevitably hit 'return' and it goes and switches branch? 😀

Hmm, if you run wt switch --print and press enter, it should not switch anything. It just prints the worktree name.

@ruudk

ruudk commented Mar 11, 2026

Copy link
Copy Markdown
Author

Another option could be, to add this to wt list and make that one interactive, so that it can be used for scripting?

@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot can you draft a PR that, if we run wt switch --no-cd, then the interactive picker only prints, rather than changing path? (and then we can avoid adding the --print option)?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Drafted #1445 — it reuses --no-cd so the picker prints the selected branch name instead of switching, no new flag needed.

When wt switch --no-cd opens the picker, selecting a branch prints its name to stdout and exits (no switching, no cd directive, no hooks). The existing wt switch --no-cd <branch> behavior is unchanged.

@max-sixty

Copy link
Copy Markdown
Owner

I prefer #1445 — @ruudk are you OK with that? we get the same feature without another arg

@ruudk

ruudk commented Mar 12, 2026

Copy link
Copy Markdown
Author

Perfect let's do that one! Thank you 😊

@ruudk ruudk closed this Mar 12, 2026
max-sixty pushed a commit that referenced this pull request Mar 12, 2026
## Summary

Draft implementation per @max-sixty's
[suggestion](#1404 (comment)):
reuse `--no-cd` to make the picker print-only instead of adding a
separate `--print` flag.

- When `wt switch --no-cd` opens the interactive picker (no branch
argument), selecting a branch prints its name to stdout and exits — no
switching, no cd directive, no hooks
- When `wt switch --no-cd <branch>` is used with an explicit branch,
behavior is unchanged (switches without cd)
- `alt-r` (remove) is blocked in print mode since it's a read-only
operation

This avoids adding a new CLI flag while giving the same scripting
capability that #1404 proposed.

## Test plan

- [x] Unit tests for `resolve_print_identifier` (switch, create, remove
cases)
- [x] PTY integration test: `--no-cd` picker prints branch name, emits
no cd directive
- [x] Existing `--no-cd` tests still pass (directive suppression, hooks,
execute)
- [x] Help snapshots and doc sync updated


🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants