Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: add nushell tab completions (#1220)
Maximilian Rooscbd5e4da5045
diff --git a/src/completion.rs b/src/completion.rsindex 43feb2826..b5f13e96b 100644--- a/src/completion.rs+++ b/src/completion.rs@@ -56,7 +56,7 @@ pub(crate) fn maybe_handle_env_completion() -> bool {// 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+ // - Fish/Nushell: Append the current token as the last argument, so index = len - 1let index: usize = std::env::var("_CLAP_COMPLETE_INDEX").ok().and_then(|i| i.parse().ok())diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snapindex 711ca80a8..53f175903 100644--- a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap+++ b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap@@ -4,6 +4,41 @@ expression: output---# worktrunk shell integration for nushell+# Tab completions: calls binary with COMPLETE=nu to get candidates.+# Note: nushell's completion engine bypasses custom completers when the current+# token starts with `-`, so flag completions (e.g. `wt switch --<TAB>`) don't+# appear. Subcommand and value completions work. (nushell/nushell#14504)+def "nu-complete wt" [context: string] {+ let worktrunk_bin = if ($env.WORKTRUNK_BIN? | is-not-empty) {+ $env.WORKTRUNK_BIN+ } else {+ let external = (which -a wt | where type == "external")+ if ($external | is-empty) { return [] }+ ($external | get 0.path)+ }++ let tokens = ($context | split row " " | where {|t| $t != "" })+ let tokens = if ($context | str ends-with " ") {+ $tokens | append ""+ } else {+ $tokens+ }++ let result = (do {+ with-env { COMPLETE: nu } { ^$worktrunk_bin -- ...$tokens }+ } | complete)+ if $result.exit_code != 0 { return [] }++ $result.stdout | lines | each {|line|+ let parts = ($line | split row "\t")+ if ($parts | length) >= 2 {+ { value: ($parts | get 0), description: ($parts | get 1) }+ } else {+ { value: ($parts | get 0) }+ }+ }+}+# Override wt command with file-based directive passing.# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, executes directives after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).@@ -34,7 +69,7 @@ expression: output# Stderr flows to the terminal in real-time. The binary sees non-TTY stdout# and uses buffered mode, but commands other than `list` don't benefit from# progressive rendering anyway.-def --env --wrapped wt [...args: string] {+def --env --wrapped wt [...args: string@"nu-complete wt"] {let worktrunk_bin = if ($env.WORKTRUNK_BIN? | is-not-empty) {$env.WORKTRUNK_BIN} else {diff --git a/templates/nushell.nu b/templates/nushell.nuindex 21c7df141..00921863a 100644--- a/templates/nushell.nu+++ b/templates/nushell.nu@@ -1,5 +1,40 @@# worktrunk shell integration for nushell+# Tab completions: calls binary with COMPLETE=nu to get candidates.+# Note: nushell's completion engine bypasses custom completers when the current+# token starts with `-`, so flag completions (e.g. `wt switch --<TAB>`) don't+# appear. Subcommand and value completions work. (nushell/nushell#14504)+def "nu-complete {{ cmd }}" [context: string] {+ let worktrunk_bin = if ($env.WORKTRUNK_BIN? | is-not-empty) {+ $env.WORKTRUNK_BIN+ } else {+ let external = (which -a {{ cmd }} | where type == "external")+ if ($external | is-empty) { return [] }+ ($external | get 0.path)+ }++ let tokens = ($context | split row " " | where {|t| $t != "" })+ let tokens = if ($context | str ends-with " ") {+ $tokens | append ""+ } else {+ $tokens+ }++ let result = (do {+ with-env { COMPLETE: nu } { ^$worktrunk_bin -- ...$tokens }+ } | complete)+ if $result.exit_code != 0 { return [] }++ $result.stdout | lines | each {|line|+ let parts = ($line | split row "\t")+ if ($parts | length) >= 2 {+ { value: ($parts | get 0), description: ($parts | get 1) }+ } else {+ { value: ($parts | get 0) }+ }+ }+}+# Override {{ cmd }} command with file-based directive passing.# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, executes directives after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).@@ -30,7 +65,7 @@# Stderr flows to the terminal in real-time. The binary sees non-TTY stdout# and uses buffered mode, but commands other than `list` don't benefit from# progressive rendering anyway.-def --env --wrapped {{ cmd }} [...args: string] {+def --env --wrapped {{ cmd }} [...args: string@"nu-complete {{ cmd }}"] {let worktrunk_bin = if ($env.WORKTRUNK_BIN? | is-not-empty) {$env.WORKTRUNK_BIN} else {diff --git a/tests/common/mod.rs b/tests/common/mod.rsindex 2614b79b7..b80e36bf5 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -653,9 +653,10 @@ pub fn configure_completion_invocation_for_shell(cmd: &mut Command, words: &[&stlet index = words.len().saturating_sub(1);cmd.env("_CLAP_COMPLETE_INDEX", index.to_string());}- "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+ "fish" | "nu" => {+ // Fish and Nushell don't set _CLAP_COMPLETE_INDEX - they append the+ // current token as the last argument, so the completion handler uses+ // args.len() - 1}_ => {}}diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex 640654d48..b54c4e6bb 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -1053,7 +1053,7 @@ fn test_complete_switch_single_dash_shows_options_not_branches(repo: TestRepo) {fn test_complete_help_flag_all_shells(repo: TestRepo) {repo.commit("initial");- for shell in ["bash", "zsh", "fish"] {+ for shell in ["bash", "zsh", "fish", "nu"] {// Test: wt --help<cursor> - should complete --helplet output = repo.completion_cmd_for_shell(&["wt", "--help"], shell)@@ -1085,7 +1085,7 @@ fn test_complete_help_flag_all_shells(repo: TestRepo) {fn test_complete_version_flag_all_shells(repo: TestRepo) {repo.commit("initial");- for shell in ["bash", "zsh", "fish"] {+ for shell in ["bash", "zsh", "fish", "nu"] {// Test: wt --version<cursor> - should complete --versionlet output = repo.completion_cmd_for_shell(&["wt", "--version"], shell)@@ -1108,7 +1108,7 @@ fn test_complete_version_flag_all_shells(repo: TestRepo) {fn test_complete_single_dash_shows_both_short_and_long_flags(repo: TestRepo) {repo.commit("initial");- for shell in ["bash", "zsh", "fish"] {+ for shell in ["bash", "zsh", "fish", "nu"] {// Test: wt -<cursor> - should show both -h and --helplet output = repo.completion_cmd_for_shell(&["wt", "-"], shell)@@ -1163,7 +1163,7 @@ fn test_complete_excludes_deprecated_args(repo: TestRepo) {// Deprecated args that should never appearlet deprecated = ["--no-background"];- for shell in ["bash", "zsh", "fish"] {+ for shell in ["bash", "zsh", "fish", "nu"] {// Test: wt remove --<cursor> - should NOT show --no-backgroundlet output = repo.completion_cmd_for_shell(&["wt", "remove", "--"], shell)@@ -1280,6 +1280,10 @@ fn test_static_completions_for_all_shells() {stdout.contains("def --wrapped") || stdout.contains("def --env"),"{shell}: should contain nushell function markers");+ assert!(+ stdout.contains("nu-complete wt"),+ "{shell}: should contain nushell completer function"+ );}"powershell" => {assert!(diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rsindex da4bae0f6..145de4da4 100644--- a/tests/integration_tests/shell_wrapper.rs+++ b/tests/integration_tests/shell_wrapper.rs@@ -3112,6 +3112,29 @@ for c in "${{COMPREPLY[@]}}"; do echo "${{c%% *}}"; doneassert_snapshot!(completions);}+ /// Black-box test: nushell completion produces correct subcommands.+ ///+ /// Nushell completions call binary with COMPLETE=nu (same protocol as fish).+ #[test]+ fn test_nushell_completion_subcommands() {+ let wt_bin = wt_bin();++ let output = std::process::Command::new(&wt_bin)+ .args(["--", "wt", ""])+ .env("COMPLETE", "nu")+ .output()+ .unwrap();++ // Nushell format is "value\tdescription" - extract just values+ let completions: String = String::from_utf8_lossy(&output.stdout)+ .lines()+ .map(|line| line.split('\t').next().unwrap_or(line))+ .collect::<Vec<_>>()+ .join("\n");++ assert_snapshot!(completions);+ }+// ========================================================================// Stderr/Stdout Redirection Tests// ========================================================================diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__nushell_completion_subcommands.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__nushell_completion_subcommands.snapnew file mode 100644index 000000000..1f7b8bc9d--- /dev/null+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__nushell_completion_subcommands.snap@@ -0,0 +1,11 @@+---+source: tests/integration_tests/shell_wrapper.rs+expression: completions+---+switch+list+remove+merge+step+hook+config