Skip to content

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

fix: reset ANSI state before returning to shell

Maximilian Roos334f6d992795
diff --git a/src/output/directive.rs b/src/output/directive.rs
index 3fb4dbab4..83565ea30 100644
--- a/src/output/directive.rs
+++ b/src/output/directive.rs
@@ -105,6 +105,11 @@ impl OutputHandler for DirectiveOutput {
}
fn terminate_output(&mut self) -> io::Result<()> {
+ // Reset ANSI state before returning to shell.
+ // wt's colored messages set terminal state on stderr.
+ write!(self.stderr, "{}", anstyle::Reset)?;
+ self.stderr.flush()?;
+
// Emit shell script to stdout with buffered directives
// The shell wrapper captures this via $(...) and evals it
let mut stdout = io::stdout();
@@ -122,7 +127,6 @@ impl OutputHandler for DirectiveOutput {
// exec command (if one was set via --execute)
if let Some(ref cmd) = self.exec_command {
- // Command is written directly - it's already shell code
writeln!(stdout, "{}", cmd)?;
}
diff --git a/templates/bash.sh b/templates/bash.sh
index 729562d39..7a4900966 100644
--- a/templates/bash.sh
+++ b/templates/bash.sh
@@ -14,11 +14,6 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # 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
-
# Completion mode: call binary directly, bypassing --internal and wt_exec.
# This check MUST be here (not in the binary) because clap's completion
# handler runs before argument parsing - we can't detect --internal there.
diff --git a/templates/fish.fish b/templates/fish.fish
index 876b5d84c..0ea127f35 100644
--- a/templates/fish.fish
+++ b/templates/fish.fish
@@ -36,11 +36,6 @@ if type -q {{ cmd_prefix }}; or test -n "$WORKTRUNK_BIN"
if test "$arg" = "--source"; set use_source true; else; set -a args $arg; end
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
-
# --source: use cargo run (builds from source)
if test $use_source = true
cargo run --quiet -- --internal $args | string collect --allow-empty | read --local -z script
diff --git a/templates/zsh.zsh b/templates/zsh.zsh
index 5a6aa4b18..bced5d881 100644
--- a/templates/zsh.zsh
+++ b/templates/zsh.zsh
@@ -17,11 +17,6 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # 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
-
# Completion mode: call binary directly, bypassing --internal and wt_exec.
# This check MUST be here (not in the binary) because clap's completion
# handler runs before argument parsing - we can't detect --internal there.
diff --git a/tests/integration_tests/directives.rs b/tests/integration_tests/directives.rs
index 5a3ece4e3..ed295c84f 100644
--- a/tests/integration_tests/directives.rs
+++ b/tests/integration_tests/directives.rs
@@ -29,7 +29,7 @@ fn test_switch_internal_directive() {
----- stdout -----
----- stderr -----
- ❌ Branch my-feature not found
+ ❌ Branch my-feature not found
πŸ’‘ Use --create to create a new branch, or wt list --branches --remotes for available branches
");
@@ -90,7 +90,7 @@ fn test_remove_internal_directive() {
----- stdout -----
----- stderr -----
- ❌ The main worktree cannot be removed
+ ❌ The main worktree cannot be removed
");
});
}
diff --git a/tests/snapshots/integration__integration_tests__directives__merge_internal_no_remove.snap b/tests/snapshots/integration__integration_tests__directives__merge_internal_no_remove.snap
index a163de7f7..d4850ebc6 100644
--- a/tests/snapshots/integration__integration_tests__directives__merge_internal_no_remove.snap
+++ b/tests/snapshots/integration__integration_tests__directives__merge_internal_no_remove.snap
@@ -31,3 +31,4 @@ exit_code: 0
  1 file changed, 1 insertion(+)
βœ… Merged to main (1 commit, 1 file, +1)
βšͺ Worktree preserved (--no-remove)
+
diff --git a/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap b/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap
index 5e792af90..c11d98ff8 100644
--- a/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap
+++ b/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap
@@ -31,3 +31,4 @@ cd '[PATH]'
  1 file changed, 1 insertion(+)
βœ… Merged to main (1 commit, 1 file, +1)
πŸ”„ Removing feature worktree & branch in background (already in main)
+
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 38fcab1ad..6f2346d02 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -53,11 +53,6 @@ wt_exec() {
if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # 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
-
# Completion mode: call binary directly, bypassing --internal and wt_exec.
# This check MUST be here (not in the binary) because clap's completion
# handler runs before argument parsing - we can't detect --internal there.
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 1db0d25a8..bc6c2e80a 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -61,11 +61,6 @@ if type -q wt; or test -n "$WORKTRUNK_BIN"
if test "$arg" = "--source"; set use_source true; else; set -a args $arg; end
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
-
# --source: use cargo run (builds from source)
if test $use_source = true
cargo run --quiet -- --internal $args | string collect --allow-empty | read --local -z script
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index b3844f41d..61dd1a3e3 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -56,11 +56,6 @@ wt_exec() {
if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
done
- # 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
-
# Completion mode: call binary directly, bypassing --internal and wt_exec.
# This check MUST be here (not in the binary) because clap's completion
# handler runs before argument parsing - we can't detect --internal there.
diff --git a/tests/snapshots/integration__integration_tests__internal_flag__config_show_with_internal_flag.snap b/tests/snapshots/integration__integration_tests__internal_flag__config_show_with_internal_flag.snap
index 4a1fbb4df..a092b4c3b 100644
--- a/tests/snapshots/integration__integration_tests__internal_flag__config_show_with_internal_flag.snap
+++ b/tests/snapshots/integration__integration_tests__internal_flag__config_show_with_internal_flag.snap
@@ -35,3 +35,4 @@ exit_code: 0
βšͺ Skipped bash; ~/.bashrc not found
βšͺ Skipped zsh; ~/.zshrc not found
βšͺ Skipped fish; ~/.config/fish/conf.d not found
+
diff --git a/tests/snapshots/integration__integration_tests__internal_flag__list_with_internal_flag.snap b/tests/snapshots/integration__integration_tests__internal_flag__list_with_internal_flag.snap
index 25413bb88..fbc7a9bbe 100644
--- a/tests/snapshots/integration__integration_tests__internal_flag__list_with_internal_flag.snap
+++ b/tests/snapshots/integration__integration_tests__internal_flag__list_with_internal_flag.snap
@@ -27,3 +27,4 @@ exit_code: 0
@ main ^ . b834638e 1d Initial commit
βšͺ Showing 1 worktree
+
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap
index ff0feba52..b991b395a 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap
@@ -25,3 +25,4 @@ cd '[REPO]'
----- stderr -----
πŸ”„ Removing feature-internal worktree & branch in background (already in main)
+
diff --git a/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap b/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap
index f105054df..9b7ff87f4 100644
--- a/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap
+++ b/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap
@@ -25,6 +25,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-❌ Branch __WORKTRUNK_CD__/tmp already exists
+❌ Branch __WORKTRUNK_CD__/tmp already exists
πŸ’‘ Remove --create flag to switch to the existing branch
diff --git a/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap b/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap
index e73f51c66..363156d04 100644
--- a/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap
+++ b/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap
@@ -24,6 +24,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-❌ Branch __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6 not found
+❌ Branch __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6 not found
πŸ’‘ Use --create to create a new branch, or wt list --branches --remotes for available branches
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap b/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap
index 357eec14a..d82ee2f8d 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap
@@ -27,3 +27,4 @@ cd '[REPO].existing-wt'
----- stderr -----
βœ… Switched to worktree for existing-wt at [REPO].existing-wt
+
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_exit_code.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_exit_code.snap
index 15d060867..6a1960943 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_exit_code.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_exit_code.snap
@@ -33,3 +33,4 @@ exit 42
βœ… Created new worktree for exit-code-test from main at [REPO].exit-code-test
πŸ”„ Executing (--execute):
  exit 42
+
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_output_then_exit.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_output_then_exit.snap
index c26a9fb85..ffe1a910d 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_output_then_exit.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_internal_execute_output_then_exit.snap
@@ -35,3 +35,4 @@ exit 7
πŸ”„ Executing (--execute):
  echo 'doing work'
  exit 7
+
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap
index 237bfb7ee..1b2084dbd 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap
@@ -28,3 +28,4 @@ cd '[REPO].internal-test'
----- stderr -----
βœ… Created new worktree for internal-test from main at [REPO].internal-test
+
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap
index 930007bed..89ac2b9bc 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap
@@ -35,3 +35,4 @@ echo 'line2'
πŸ”„ Executing (--execute):
  echo 'line1'
  echo 'line2'
+