Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat: add completion cmd for completion integration (#701)

Rui Chen907904612744
diff --git a/.claude-plugin/skills/worktrunk/reference/faq.md b/.claude-plugin/skills/worktrunk/reference/faq.md
index e366ae5f0..e724f0700 100644
--- a/.claude-plugin/skills/worktrunk/reference/faq.md
+++ b/.claude-plugin/skills/worktrunk/reference/faq.md
@@ -89,7 +89,7 @@ Created by `wt config shell install`:
- **Bash**: adds line to `~/.bashrc`
- **Zsh**: adds line to `~/.zshrc` (or `$ZDOTDIR/.zshrc`)
-- **Fish**: creates `~/.config/fish/conf.d/wt.fish` and `~/.config/fish/completions/wt.fish`
+- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish`
**To remove:** `wt config shell uninstall`.
diff --git a/docs/content/faq.md b/docs/content/faq.md
index 9e645fa0b..c45304fc2 100644
--- a/docs/content/faq.md
+++ b/docs/content/faq.md
@@ -95,7 +95,7 @@ Created by `wt config shell install`:
- **Bash**: adds line to `~/.bashrc`
- **Zsh**: adds line to `~/.zshrc` (or `$ZDOTDIR/.zshrc`)
-- **Fish**: creates `~/.config/fish/conf.d/wt.fish` and `~/.config/fish/completions/wt.fish`
+- **Fish**: creates `~/.config/fish/functions/wt.fish` and `~/.config/fish/completions/wt.fish`
**To remove:** `wt config shell uninstall`.
diff --git a/src/cli/config.rs b/src/cli/config.rs
index b260eb101..e6ecc9cf3 100644
--- a/src/cli/config.rs
+++ b/src/cli/config.rs
@@ -134,6 +134,19 @@ Detects various forms of the integration pattern regardless of:
- Prompts for user input"#
)]
ShowTheme,
+
+ /// Generate static shell completions for package managers
+ ///
+ /// Outputs static completion scripts for Homebrew and other package managers.
+ /// Only completes commands and flags, not branch names.
+ /// This is predominantly for package managers. Users should run
+ /// `wt config shell install` instead.
+ #[command(hide = true)]
+ Completions {
+ /// Shell to generate completions for
+ #[arg(value_enum)]
+ shell: Shell,
+ },
}
#[derive(Subcommand)]
diff --git a/src/commands/init.rs b/src/commands/init.rs
index b2cee3632..2c25e6d8c 100644
--- a/src/commands/init.rs
+++ b/src/commands/init.rs
@@ -1,6 +1,11 @@
+use clap::CommandFactory;
+use clap_complete::generate;
+use std::io;
use worktrunk::shell;
use worktrunk::styling::println;
+use crate::cli::Cli;
+
pub fn handle_init(shell: shell::Shell, cmd: String) -> Result<(), String> {
let init = shell::ShellInit::with_prefix(shell, cmd);
@@ -13,3 +18,57 @@ pub fn handle_init(shell: shell::Shell, cmd: String) -> Result<(), String> {
Ok(())
}
+
+/// Generate static shell completions to stdout.
+///
+/// This is the handler for `wt config shell completions <shell>`. It outputs completion
+/// scripts suitable for package manager integration (e.g., Homebrew's
+/// `generate_completions_from_executable`).
+///
+/// Unlike `wt config shell init`, this does not:
+/// - Modify any files
+/// - Include shell integration (cd-on-switch functionality)
+/// - Register dynamic completions
+///
+/// TODO(completions): We output static completions because that's the package manager
+/// convention, but dynamic completions (one-liner that calls the binary at tab-time)
+/// might be better—users would get branch name completion. See the fish example in
+/// `~/.config/fish/completions/wt.fish` which calls `COMPLETE=fish wt` at runtime.
+/// Other tools like gh/kubectl also call their binaries at runtime.
+pub fn handle_completions(shell: shell::Shell) -> anyhow::Result<()> {
+ let mut cmd = Cli::command();
+ let cmd_name = crate::binary_name();
+ let mut stdout = io::stdout();
+
+ match shell {
+ shell::Shell::Bash => {
+ generate(
+ clap_complete::shells::Bash,
+ &mut cmd,
+ &cmd_name,
+ &mut stdout,
+ );
+ }
+ shell::Shell::Fish => {
+ generate(
+ clap_complete::shells::Fish,
+ &mut cmd,
+ &cmd_name,
+ &mut stdout,
+ );
+ }
+ shell::Shell::Zsh => {
+ generate(clap_complete::shells::Zsh, &mut cmd, &cmd_name, &mut stdout);
+ }
+ shell::Shell::PowerShell => {
+ generate(
+ clap_complete::shells::PowerShell,
+ &mut cmd,
+ &cmd_name,
+ &mut stdout,
+ );
+ }
+ }
+
+ Ok(())
+}
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index a20ae7074..5a9fb9a36 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -32,7 +32,7 @@ pub(crate) use configure_shell::{
};
pub(crate) use for_each::step_for_each;
pub(crate) use hook_commands::{add_approvals, clear_approvals, handle_hook_show, run_hook};
-pub(crate) use init::handle_init;
+pub(crate) use init::{handle_completions, handle_init};
pub(crate) use list::handle_list;
pub(crate) use merge::{MergeOptions, execute_pre_remove_commands, handle_merge};
#[cfg(unix)]
diff --git a/src/main.rs b/src/main.rs
index 3f70f09cd..f334591f5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,7 +37,7 @@ use commands::handle_select;
use commands::worktree::{SwitchResult, handle_push};
use commands::{
MergeOptions, RebaseResult, ResolutionContext, SquashResult, add_approvals, approve_hooks,
- clear_approvals, execute_switch, handle_config_create, handle_config_show,
+ clear_approvals, execute_switch, handle_completions, handle_config_create, handle_config_show,
handle_configure_shell, handle_hints_clear, handle_hints_get, handle_hook_show, handle_init,
handle_list, handle_merge, handle_rebase, handle_remove, handle_remove_current,
handle_show_theme, handle_squash, handle_state_clear, handle_state_clear_all, handle_state_get,
@@ -411,6 +411,7 @@ fn main() {
ConfigShellCommand::ShowTheme => {
handle_show_theme().map_err(|e| anyhow::anyhow!("{}", e))
}
+ ConfigShellCommand::Completions { shell } => handle_completions(shell),
}
}
ConfigCommand::Create { project } => handle_config_create(project),
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index 8efa09e55..339c2ef16 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -1219,3 +1219,61 @@ fn test_complete_excludes_deprecated_args(repo: TestRepo) {
}
}
}
+
+/// Test static shell completions command for package managers.
+///
+/// The `wt config shell completions <shell>` command outputs static completion
+/// scripts suitable for package manager integration (e.g., Homebrew's
+/// `generate_completions_from_executable`).
+#[rstest]
+fn test_static_completions_for_all_shells() {
+ // Test each supported shell produces valid output
+ for shell in ["bash", "fish", "zsh", "powershell"] {
+ let output = wt_command()
+ .args(["config", "shell", "completions", shell])
+ .output()
+ .unwrap();
+
+ assert!(
+ output.status.success(),
+ "{shell}: completions command failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ !stdout.is_empty(),
+ "{shell}: completions output should not be empty"
+ );
+
+ // Each shell should have some indication it's a completion script
+ match shell {
+ "bash" => {
+ assert!(
+ stdout.contains("complete") || stdout.contains("_wt"),
+ "{shell}: should contain bash completion markers"
+ );
+ }
+ "fish" => {
+ assert!(
+ stdout.contains("complete") && stdout.contains("wt"),
+ "{shell}: should contain fish completion markers"
+ );
+ }
+ "zsh" => {
+ assert!(
+ stdout.contains("#compdef") || stdout.contains("_wt"),
+ "{shell}: should contain zsh completion markers"
+ );
+ }
+ "powershell" => {
+ assert!(
+ stdout.contains("Register-ArgumentCompleter")
+ || stdout.contains("$scriptBlock"),
+ "{shell}: should contain PowerShell completion markers"
+ );
+ }
+ _ => {}
+ }
+ }
+}