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.rsindex 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 itlet 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 codewriteln!(stdout, "{}", cmd)?;}diff --git a/templates/bash.sh b/templates/bash.shindex 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"); fidone- # 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.fishindex 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; endend- # 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 = truecargo run --quiet -- --internal $args | string collect --allow-empty | read --local -z scriptdiff --git a/templates/zsh.zsh b/templates/zsh.zshindex 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"); fidone- # 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.rsindex 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 ------ β [31mBranch [1mmy-feature[22m not found[39m+ [0mβ [31mBranch [1mmy-feature[22m not found[39mπ‘ [2mUse [90m--create[39m to create a new branch, or [90mwt list --branches --remotes[39m for available branches[22m");@@ -90,7 +90,7 @@ fn test_remove_internal_directive() {----- stdout ---------- stderr ------ β [31mThe main worktree cannot be removed[39m+ [0mβ [31mThe main worktree cannot be removed[39m");});}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.snapindex 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[107m [0m 1 file changed, 1 insertion(+)β [32mMerged to [1mmain[22m [90m(1 commit, 1 file, [32m+1[39m[39m[90m)[39m[39mβͺ Worktree preserved (--no-remove)+[0mdiff --git a/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap b/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snapindex 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]'[107m [0m 1 file changed, 1 insertion(+)β [32mMerged to [1mmain[22m [90m(1 commit, 1 file, [32m+1[39m[39m[90m)[39m[39mπ [36m[36mRemoving [1mfeature[22m worktree & branch in background (already in main)[39m[39m+[0mdiff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 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"); fidone- # 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.snapindex 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; endend- # 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 = truecargo run --quiet -- --internal $args | string collect --allow-empty | read --local -z scriptdiff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 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"); fidone- # 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.snapindex 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[2mβͺ Skipped bash; ~/.bashrc not found[22m[2mβͺ Skipped zsh; ~/.zshrc not found[22m[2mβͺ Skipped fish; ~/.config/fish/conf.d not found[22m+[0mdiff --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.snapindex 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@ [1mmain[0m [2m^[22m [1m.[0m [2mb834638e[0m [2m1d[0m [2mInitial commit[0mβͺ [2mShowing 1 worktree[0m+[0mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snapindex 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 -----π [36m[36mRemoving [1mfeature-internal[22m worktree & branch in background (already in main)[39m[39m+[0mdiff --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.snapindex 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 ------β [31mBranch [1m__WORKTRUNK_CD__/tmp[22m already exists[39m+[0mβ [31mBranch [1m__WORKTRUNK_CD__/tmp[22m already exists[39mπ‘ [2mRemove [90m--create[39m flag to switch to the existing branch[22mdiff --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.snapindex 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 ------β [31mBranch [1m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[22m not found[39m+[0mβ [31mBranch [1m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[22m not found[39mπ‘ [2mUse [90m--create[39m to create a new branch, or [90mwt list --branches --remotes[39m for available branches[22mdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap b/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snapindex 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 -----β [32m[32mSwitched to worktree for [1mexisting-wt[22m at [1m[REPO].existing-wt[39m[22m[39m+[0mdiff --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.snapindex 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β [32m[32mCreated new worktree for [1mexit-code-test[22m from [1mmain[22m at [1m[REPO].exit-code-test[39m[22m[39mπ [36mExecuting (--execute):[39m[107m [0m [2m[0m[2m[34mexit[0m[2m 42[0m+[0mdiff --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.snapindex 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π [36mExecuting (--execute):[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'doing work'[0m[2m[0m[107m [0m [2m[0m[2m[34mexit[0m[2m 7[0m+[0mdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snapindex 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 -----β [32m[32mCreated new worktree for [1minternal-test[22m from [1mmain[22m at [1m[REPO].internal-test[39m[22m[39m+[0mdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snapindex 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'π [36mExecuting (--execute):[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'line1'[0m[2m[0m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'line2'[0m[2m[0m+[0m