Skip to content

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

feat(shell): Stream stderr to terminal directly

Maximilian Roosc24f5bfecb8a
diff --git a/src/shell.rs b/src/shell.rs
index 17a081da4..f21e22bf3 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -209,7 +209,7 @@ impl ShellInit {
pub fn generate(&self) -> Result<String, askama::Error> {
match self.shell {
Shell::Bash => {
- let posix_shim = PosixDirectivesTemplate {}.render()?;
+ let posix_shim = PosixDirectivesTemplate { cmd_prefix: "wt" }.render()?;
let template = BashTemplate {
shell_name: self.shell.to_string(),
cmd_prefix: "wt",
@@ -218,7 +218,7 @@ impl ShellInit {
template.render()
}
Shell::Zsh => {
- let posix_shim = PosixDirectivesTemplate {}.render()?;
+ let posix_shim = PosixDirectivesTemplate { cmd_prefix: "wt" }.render()?;
let template = ZshTemplate {
cmd_prefix: "wt",
posix_shim: &posix_shim,
@@ -236,7 +236,9 @@ impl ShellInit {
/// POSIX directive shim template (shared by bash, zsh, oil)
#[derive(Template)]
#[template(path = "posix_directives.sh", escape = "none")]
-struct PosixDirectivesTemplate {}
+struct PosixDirectivesTemplate<'a> {
+ cmd_prefix: &'a str,
+}
/// Bash shell template
#[derive(Template)]
diff --git a/templates/bash.sh b/templates/bash.sh
index 3f2c31597..b50ab84bd 100644
--- a/templates/bash.sh
+++ b/templates/bash.sh
@@ -2,40 +2,27 @@
# Only initialize if {{ cmd_prefix }} is available (in PATH or via WORKTRUNK_BIN)
if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
- # Resolve binary path once at init. WORKTRUNK_BIN can override (for testing dev builds).
- export WORKTRUNK_BIN="${WORKTRUNK_BIN:-$(command -v {{ cmd_prefix }})}"
{{ posix_shim }}
# Override {{ cmd_prefix }} command to add --internal flag
{{ cmd_prefix }}() {
- # Re-resolve if unset (e.g., after shell snapshot restore)
- if [[ -z "$WORKTRUNK_BIN" ]]; then
- export WORKTRUNK_BIN="$(command -v {{ cmd_prefix }})"
- fi
-
local use_source=false
local args=()
- # Check for --source flag and strip it
for arg in "$@"; do
- if [[ "$arg" == "--source" ]]; then
- use_source=true
- else
- args+=("$arg")
- fi
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # Force colors if stderr is a TTY (directive mode outputs to stderr)
- # Respects NO_COLOR and explicit CLICOLOR_FORCE
- if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" ]]; then
- if [[ -t 2 ]]; then export CLICOLOR_FORCE=1; fi
+ # Force colors if stderr is a TTY (respects NO_COLOR/CLICOLOR_FORCE)
+ if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" && -t 2 ]]; then
+ export CLICOLOR_FORCE=1
fi
- # If --source was specified, use cargo run directly (builds and runs)
+ # --source: use cargo run (builds from source)
if [[ "$use_source" == true ]]; then
local script exit_code=0
- script="$(cargo run --quiet -- --internal "${args[@]}" 2>&2)" || exit_code=$?
+ script="$(cargo run --quiet -- --internal "${args[@]}")" || exit_code=$?
if [[ -n "$script" ]]; then
eval "$script"
if [[ $exit_code -eq 0 ]]; then
@@ -52,7 +39,7 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
_{{ cmd_prefix }}_lazy_complete() {
# Generate completions function once (check if clap's function exists)
if ! declare -F _clap_complete_{{ cmd_prefix }} >/dev/null; then
- eval "$(COMPLETE=bash "$WORKTRUNK_BIN" 2>/dev/null)" || return
+ eval "$(COMPLETE=bash "${WORKTRUNK_BIN:-{{ cmd_prefix }}}" 2>/dev/null)" || return
fi
_clap_complete_{{ cmd_prefix }} "$@"
}
diff --git a/templates/fish.fish b/templates/fish.fish
index 7c78dc51d..e28bdb6e7 100644
--- a/templates/fish.fish
+++ b/templates/fish.fish
@@ -2,24 +2,20 @@
# Only initialize if {{ cmd_prefix }} is available (in PATH or via WORKTRUNK_BIN)
if type -q {{ cmd_prefix }}; or set -q WORKTRUNK_BIN
- # Resolve binary path once at init. WORKTRUNK_BIN can override (for testing dev builds).
+ # TODO: Consider time-of-use pattern like bash/zsh instead of init-time setup.
+ # Fish lacks ${:-} syntax; would require verbose workaround at each call site.
if not set -q WORKTRUNK_BIN
set -gx WORKTRUNK_BIN (type -p {{ cmd_prefix }})
end
- # Helper function to run wt and eval the output script
- # stderr streams to terminal for real-time feedback, stdout is captured as shell script
+ # Capture stdout (shell script), eval in parent shell. stderr streams to terminal.
+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).
function wt_exec
- # Capture stdout (shell script), let stderr flow to terminal
- # Use string collect to join lines into a single string (fish splits on newlines by default)
- set -l script (command $WORKTRUNK_BIN $argv 2>&2 | string collect)
+ set -l script (command $WORKTRUNK_BIN $argv | string collect)
set -l exit_code $pipestatus[1]
- # Eval the script (cd, exec command, etc.) even on failure
- # This ensures cd happens before returning the error code
if test -n "$script"
eval $script
- # If script contains a command (--execute), use its exit code
if test $exit_code -eq 0
set exit_code $status
end
@@ -33,26 +29,18 @@ if type -q {{ cmd_prefix }}; or set -q WORKTRUNK_BIN
set -l use_source false
set -l args
- # Check for --source flag and strip it
for arg in $argv
- if test "$arg" = "--source"
- set use_source true
- else
- set -a args $arg
- end
+ if test "$arg" = "--source"; set use_source true; else; set -a args $arg; end
end
- # Force colors if stderr is a TTY (directive mode outputs to stderr)
- # Respects NO_COLOR and explicit CLICOLOR_FORCE
- if not set -q NO_COLOR; and not set -q CLICOLOR_FORCE
- if isatty stderr
- set -x CLICOLOR_FORCE 1
- end
+ # Force colors if stderr is a TTY (respects NO_COLOR/CLICOLOR_FORCE)
+ if not set -q NO_COLOR; and not set -q CLICOLOR_FORCE; and isatty stderr
+ set -x CLICOLOR_FORCE 1
end
- # If --source was specified, use cargo run directly (builds and runs)
+ # --source: use cargo run (builds from source)
if test $use_source = true
- set -l script (cargo run --quiet -- --internal $args 2>&2 | string collect)
+ set -l script (cargo run --quiet -- --internal $args | string collect)
set -l exit_code $pipestatus[1]
if test -n "$script"
eval $script
diff --git a/templates/posix_directives.sh b/templates/posix_directives.sh
index 7ddcfaf57..d15077054 100644
--- a/templates/posix_directives.sh
+++ b/templates/posix_directives.sh
@@ -1,22 +1,11 @@
-# Simple shell integration for POSIX shells (bash, zsh).
-#
-# Captures the shell script from wt's stdout and evals it in the parent shell.
-# stderr streams directly to the terminal for real-time user feedback.
-#
-# This pattern (stderr for logs, stdout for script) is proven by direnv.
-# No FIFOs, no background processes, no job control suppression needed.
+# Capture stdout (shell script), eval in parent shell. stderr streams to terminal.
+# WORKTRUNK_BIN can override the binary path (for testing dev builds).
wt_exec() {
local script exit_code=0
+ script="$(command "${WORKTRUNK_BIN:-{{ cmd_prefix }}}" "$@")" || exit_code=$?
- # Run wt with stderr attached to terminal (2>&2)
- # Capture stdout (the shell script) into $script
- script="$(command "$WORKTRUNK_BIN" "$@" 2>&2)" || exit_code=$?
-
- # Eval the script (cd, exec command, etc.) even on failure
- # This ensures cd happens before returning the error code
if [[ -n "$script" ]]; then
eval "$script"
- # If script contains a command (--execute), use its exit code
if [[ $exit_code -eq 0 ]]; then
exit_code=$?
fi
diff --git a/templates/zsh.zsh b/templates/zsh.zsh
index 975777c7e..d461a0969 100644
--- a/templates/zsh.zsh
+++ b/templates/zsh.zsh
@@ -5,40 +5,27 @@
# Only initialize if {{ cmd_prefix }} is available (in PATH or via WORKTRUNK_BIN)
if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
- # Resolve binary path once at init. WORKTRUNK_BIN can override (for testing dev builds).
- export WORKTRUNK_BIN="${WORKTRUNK_BIN:-$(command -v {{ cmd_prefix }})}"
{{ posix_shim }}
# Override {{ cmd_prefix }} command to add --internal flag
{{ cmd_prefix }}() {
- # Re-resolve if unset (e.g., after shell snapshot restore)
- if [[ -z "$WORKTRUNK_BIN" ]]; then
- export WORKTRUNK_BIN="$(command -v {{ cmd_prefix }})"
- fi
-
local use_source=false
local -a args
- # Check for --source flag and strip it
for arg in "$@"; do
- if [[ "$arg" == "--source" ]]; then
- use_source=true
- else
- args+=("$arg")
- fi
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # Force colors if stderr is a TTY (directive mode outputs to stderr)
- # Respects NO_COLOR and explicit CLICOLOR_FORCE
- if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" ]]; then
- if [[ -t 2 ]]; then export CLICOLOR_FORCE=1; fi
+ # Force colors if stderr is a TTY (respects NO_COLOR/CLICOLOR_FORCE)
+ if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" && -t 2 ]]; then
+ export CLICOLOR_FORCE=1
fi
- # If --source was specified, use cargo run directly (builds and runs)
+ # --source: use cargo run (builds from source)
if [[ "$use_source" == true ]]; then
local script exit_code=0
- script="$(cargo run --quiet -- --internal "${args[@]}" 2>&2)" || exit_code=$?
+ script="$(cargo run --quiet -- --internal "${args[@]}")" || exit_code=$?
if [[ -n "$script" ]]; then
eval "$script"
if [[ $exit_code -eq 0 ]]; then
@@ -55,7 +42,7 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
_{{ cmd_prefix }}_lazy_complete() {
# Generate completions function once (check if clap's function exists)
if ! (( $+functions[_clap_dynamic_completer_{{ cmd_prefix }}] )); then
- eval "$(COMPLETE=zsh "$WORKTRUNK_BIN" 2>/dev/null)" || return
+ eval "$(COMPLETE=zsh "${WORKTRUNK_BIN:-{{ cmd_prefix }}}" 2>/dev/null)" || return
fi
_clap_dynamic_completer_{{ cmd_prefix }} "$@"
}
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 5b96a5d8c..790b15f93 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -18,6 +18,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_BIN: ""
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: true
@@ -27,28 +28,15 @@ exit_code: 0
# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
- # Resolve binary path once at init. WORKTRUNK_BIN can override (for testing dev builds).
- export WORKTRUNK_BIN="${WORKTRUNK_BIN:-$(command -v wt)}"
-# Simple shell integration for POSIX shells (bash, zsh).
-#
-# Captures the shell script from wt's stdout and evals it in the parent shell.
-# stderr streams directly to the terminal for real-time user feedback.
-#
-# This pattern (stderr for logs, stdout for script) is proven by direnv.
-# No FIFOs, no background processes, no job control suppression needed.
+# Capture stdout (shell script), eval in parent shell. stderr streams to terminal.
+# WORKTRUNK_BIN can override the binary path (for testing dev builds).
wt_exec() {
local script exit_code=0
+ script="$(command "${WORKTRUNK_BIN:-wt}" "$@")" || exit_code=$?
- # Run wt with stderr attached to terminal (2>&2)
- # Capture stdout (the shell script) into $script
- script="$(command "$WORKTRUNK_BIN" "$@" 2>&2)" || exit_code=$?
-
- # Eval the script (cd, exec command, etc.) even on failure
- # This ensures cd happens before returning the error code
if [[ -n "$script" ]]; then
eval "$script"
- # If script contains a command (--execute), use its exit code
if [[ $exit_code -eq 0 ]]; then
exit_code=$?
fi
@@ -59,33 +47,22 @@ wt_exec() {
# Override wt command to add --internal flag
wt() {
- # Re-resolve if unset (e.g., after shell snapshot restore)
- if [[ -z "$WORKTRUNK_BIN" ]]; then
- export WORKTRUNK_BIN="$(command -v wt)"
- fi
-
local use_source=false
local args=()
- # Check for --source flag and strip it
for arg in "$@"; do
- if [[ "$arg" == "--source" ]]; then
- use_source=true
- else
- args+=("$arg")
- fi
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # Force colors if stderr is a TTY (directive mode outputs to stderr)
- # Respects NO_COLOR and explicit CLICOLOR_FORCE
- if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" ]]; then
- if [[ -t 2 ]]; then export CLICOLOR_FORCE=1; fi
+ # Force colors if stderr is a TTY (respects NO_COLOR/CLICOLOR_FORCE)
+ if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" && -t 2 ]]; then
+ export CLICOLOR_FORCE=1
fi
- # If --source was specified, use cargo run directly (builds and runs)
+ # --source: use cargo run (builds from source)
if [[ "$use_source" == true ]]; then
local script exit_code=0
- script="$(cargo run --quiet -- --internal "${args[@]}" 2>&2)" || exit_code=$?
+ script="$(cargo run --quiet -- --internal "${args[@]}")" || exit_code=$?
if [[ -n "$script" ]]; then
eval "$script"
if [[ $exit_code -eq 0 ]]; then
@@ -102,7 +79,7 @@ wt_exec() {
_wt_lazy_complete() {
# Generate completions function once (check if clap's function exists)
if ! declare -F _clap_complete_wt >/dev/null; then
- eval "$(COMPLETE=bash "$WORKTRUNK_BIN" 2>/dev/null)" || return
+ eval "$(COMPLETE=bash "${WORKTRUNK_BIN:-wt}" 2>/dev/null)" || return
fi
_clap_complete_wt "$@"
}
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 4094584b3..c912a0a5b 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -18,6 +18,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_BIN: ""
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: true
@@ -27,24 +28,20 @@ exit_code: 0
# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
if type -q wt; or set -q WORKTRUNK_BIN
- # Resolve binary path once at init. WORKTRUNK_BIN can override (for testing dev builds).
+ # TODO: Consider time-of-use pattern like bash/zsh instead of init-time setup.
+ # Fish lacks ${:-} syntax; would require verbose workaround at each call site.
if not set -q WORKTRUNK_BIN
set -gx WORKTRUNK_BIN (type -p wt)
end
- # Helper function to run wt and eval the output script
- # stderr streams to terminal for real-time feedback, stdout is captured as shell script
+ # Capture stdout (shell script), eval in parent shell. stderr streams to terminal.
+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).
function wt_exec
- # Capture stdout (shell script), let stderr flow to terminal
- # Use string collect to join lines into a single string (fish splits on newlines by default)
- set -l script (command $WORKTRUNK_BIN $argv 2>&2 | string collect)
+ set -l script (command $WORKTRUNK_BIN $argv | string collect)
set -l exit_code $pipestatus[1]
- # Eval the script (cd, exec command, etc.) even on failure
- # This ensures cd happens before returning the error code
if test -n "$script"
eval $script
- # If script contains a command (--execute), use its exit code
if test $exit_code -eq 0
set exit_code $status
end
@@ -58,26 +55,18 @@ if type -q wt; or set -q WORKTRUNK_BIN
set -l use_source false
set -l args
- # Check for --source flag and strip it
for arg in $argv
- if test "$arg" = "--source"
- set use_source true
- else
- set -a args $arg
- end
+ if test "$arg" = "--source"; set use_source true; else; set -a args $arg; end
end
- # Force colors if stderr is a TTY (directive mode outputs to stderr)
- # Respects NO_COLOR and explicit CLICOLOR_FORCE
- if not set -q NO_COLOR; and not set -q CLICOLOR_FORCE
- if isatty stderr
- set -x CLICOLOR_FORCE 1
- end
+ # Force colors if stderr is a TTY (respects NO_COLOR/CLICOLOR_FORCE)
+ if not set -q NO_COLOR; and not set -q CLICOLOR_FORCE; and isatty stderr
+ set -x CLICOLOR_FORCE 1
end
- # If --source was specified, use cargo run directly (builds and runs)
+ # --source: use cargo run (builds from source)
if test $use_source = true
- set -l script (cargo run --quiet -- --internal $args 2>&2 | string collect)
+ set -l script (cargo run --quiet -- --internal $args | string collect)
set -l exit_code $pipestatus[1]
if test -n "$script"
eval $script
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 752571c66..45b7ae1f2 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -18,6 +18,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_BIN: ""
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: true
@@ -30,28 +31,15 @@ exit_code: 0
# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
- # Resolve binary path once at init. WORKTRUNK_BIN can override (for testing dev builds).
- export WORKTRUNK_BIN="${WORKTRUNK_BIN:-$(command -v wt)}"
-# Simple shell integration for POSIX shells (bash, zsh).
-#
-# Captures the shell script from wt's stdout and evals it in the parent shell.
-# stderr streams directly to the terminal for real-time user feedback.
-#
-# This pattern (stderr for logs, stdout for script) is proven by direnv.
-# No FIFOs, no background processes, no job control suppression needed.
+# Capture stdout (shell script), eval in parent shell. stderr streams to terminal.
+# WORKTRUNK_BIN can override the binary path (for testing dev builds).
wt_exec() {
local script exit_code=0
+ script="$(command "${WORKTRUNK_BIN:-wt}" "$@")" || exit_code=$?
- # Run wt with stderr attached to terminal (2>&2)
- # Capture stdout (the shell script) into $script
- script="$(command "$WORKTRUNK_BIN" "$@" 2>&2)" || exit_code=$?
-
- # Eval the script (cd, exec command, etc.) even on failure
- # This ensures cd happens before returning the error code
if [[ -n "$script" ]]; then
eval "$script"
- # If script contains a command (--execute), use its exit code
if [[ $exit_code -eq 0 ]]; then
exit_code=$?
fi
@@ -62,33 +50,22 @@ wt_exec() {
# Override wt command to add --internal flag
wt() {
- # Re-resolve if unset (e.g., after shell snapshot restore)
- if [[ -z "$WORKTRUNK_BIN" ]]; then
- export WORKTRUNK_BIN="$(command -v wt)"
- fi
-
local use_source=false
local -a args
- # Check for --source flag and strip it
for arg in "$@"; do
- if [[ "$arg" == "--source" ]]; then
- use_source=true
- else
- args+=("$arg")
- fi
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # Force colors if stderr is a TTY (directive mode outputs to stderr)
- # Respects NO_COLOR and explicit CLICOLOR_FORCE
- if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" ]]; then
- if [[ -t 2 ]]; then export CLICOLOR_FORCE=1; fi
+ # Force colors if stderr is a TTY (respects NO_COLOR/CLICOLOR_FORCE)
+ if [[ -z "${NO_COLOR:-}" && -z "${CLICOLOR_FORCE:-}" && -t 2 ]]; then
+ export CLICOLOR_FORCE=1
fi
- # If --source was specified, use cargo run directly (builds and runs)
+ # --source: use cargo run (builds from source)
if [[ "$use_source" == true ]]; then
local script exit_code=0
- script="$(cargo run --quiet -- --internal "${args[@]}" 2>&2)" || exit_code=$?
+ script="$(cargo run --quiet -- --internal "${args[@]}")" || exit_code=$?
if [[ -n "$script" ]]; then
eval "$script"
if [[ $exit_code -eq 0 ]]; then
@@ -105,7 +82,7 @@ wt_exec() {
_wt_lazy_complete() {
# Generate completions function once (check if clap's function exists)
if ! (( $+functions[_clap_dynamic_completer_wt] )); then
- eval "$(COMPLETE=zsh "$WORKTRUNK_BIN" 2>/dev/null)" || return
+ eval "$(COMPLETE=zsh "${WORKTRUNK_BIN:-wt}" 2>/dev/null)" || return
fi
_clap_dynamic_completer_wt "$@"
}