Skip to content

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

Support Fish shell completion by detecting index from args length

Maximilian Roos68957ac2e37e
diff --git a/src/completion.rs b/src/completion.rs
index 56be3032c..fb5b64f59 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -52,10 +52,13 @@ pub fn maybe_handle_env_completion() -> bool {
let mut cmd = completion_command();
cmd.build();
+ // Determine the index of the word being completed.
+ // - Bash/Zsh: Pass `_CLAP_COMPLETE_INDEX` env var with the cursor position
+ // - Fish: Appends the current token as the last argument, so index = len - 1
let index: usize = std::env::var("_CLAP_COMPLETE_INDEX")
.ok()
.and_then(|i| i.parse().ok())
- .unwrap_or_default();
+ .unwrap_or_else(|| args.len() - 1);
// Check if the current word is exactly "-" (single dash)
// If so, we want to show both short flags (-h) AND long flags (--help)
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 33bd45f88..3177b69e1 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -75,15 +75,30 @@ pub fn configure_completion_invocation(cmd: &mut Command, words: &[&str]) {
}
/// Configure an existing command to mimic shell completion environment for a specific shell.
+///
+/// This matches how each shell actually invokes completions:
+/// - Bash/Zsh: Set `_CLAP_COMPLETE_INDEX` env var with cursor position
+/// - Fish: Appends current token as last arg (no index env var)
pub fn configure_completion_invocation_for_shell(cmd: &mut Command, words: &[&str], shell: &str) {
- let index = words.len().saturating_sub(1);
cmd.arg("--");
cmd.args(words);
cmd.env("COMPLETE", shell);
- cmd.env("_CLAP_COMPLETE_INDEX", index.to_string());
- cmd.env("_CLAP_COMPLETE_COMP_TYPE", "9"); // normal completion
- cmd.env("_CLAP_COMPLETE_SPACE", "true");
cmd.env("_CLAP_IFS", "\n");
+
+ // Shell-specific environment setup to match real completion behavior
+ match shell {
+ "fish" => {
+ // Fish doesn't set _CLAP_COMPLETE_INDEX - it appends the current token
+ // as the last argument, so the completion handler uses args.len() - 1
+ }
+ _ => {
+ // Bash and Zsh set the index via environment variable
+ let index = words.len().saturating_sub(1);
+ cmd.env("_CLAP_COMPLETE_INDEX", index.to_string());
+ cmd.env("_CLAP_COMPLETE_COMP_TYPE", "9"); // normal completion (bash)
+ cmd.env("_CLAP_COMPLETE_SPACE", "true"); // bash
+ }
+ }
}
/// Configure an existing command with the standardized worktrunk CLI environment.