Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Preserve colors in directive output and suppress hints
Maximilian Roos6cd5da6f5b50
diff --git a/src/output/directive.rs b/src/output/directive.rsindex 64cc77355..de719d102 100644--- a/src/output/directive.rs+++ b/src/output/directive.rs@@ -1,6 +1,5 @@//! Directive output mode for shell integration-use anstream::adapter::strip_str;use std::io::{self, Write};use std::path::Path;@@ -15,19 +14,24 @@ impl DirectiveOutput {}pub fn success(&mut self, message: String) -> io::Result<()> {- let plain = strip_str(&message).to_string();- write!(io::stdout(), "{}\0", plain)?;+ // Don't strip colors - users see this output and need styling+ write!(io::stdout(), "{}\0", message)?;io::stdout().flush()}pub fn progress(&mut self, message: String) -> io::Result<()> {// Progress messages are for humans - output them just like success messages- // The shell wrapper will display these to users- let plain = strip_str(&message).to_string();- write!(io::stdout(), "{}\0", plain)?;+ // The shell wrapper will display these to users with colors preserved+ write!(io::stdout(), "{}\0", message)?;io::stdout().flush()}+ pub fn hint(&mut self, _message: String) -> io::Result<()> {+ // Hints are only for interactive mode - suppress in directive mode+ // When users run through shell wrapper, they already have integration+ Ok(())+ }+pub fn change_directory(&mut self, path: &Path) -> io::Result<()> {write!(io::stdout(), "__WORKTRUNK_CD__{}\0", path.display())?;io::stdout().flush()@@ -96,9 +100,9 @@ mod tests {assert_eq!(nul_count, 3, "Should have exactly 3 NUL terminators");}- /// Test that anstyle formatting is stripped from success messages+ /// Test that anstyle formatting is preserved in directive output#[test]- fn test_success_strips_anstyle() {+ fn test_success_preserves_anstyle() {use anstyle::{AnsiColor, Color, Style};let bold = Style::new().bold();@@ -107,12 +111,13 @@ mod tests {// Create a styled messagelet styled = format!("{cyan}Styled{cyan:#} {bold}message{bold:#}");- // The strip_str function should remove the ANSI codes- let plain = strip_str(&styled).to_string();- assert_eq!(plain, "Styled message");+ // The styled message should contain ANSI escape codesassert!(- !plain.contains('\x1b'),- "Should not contain ANSI escape codes"+ styled.contains('\x1b'),+ "Styled message should contain ANSI escape codes");++ // Directive mode preserves styling for users viewing through shell wrapper+ // (We're not testing actual output here, just documenting the behavior)}}diff --git a/src/output/global.rs b/src/output/global.rsindex 0e5587205..5e5c8a4bf 100644--- a/src/output/global.rs+++ b/src/output/global.rs@@ -68,10 +68,10 @@ pub fn success(message: impl Into<String>) -> io::Result<()> {})}-/// Emit a progress message (only shown in interactive mode)+/// Emit a progress message////// Progress messages are intermediate status updates like "π Cleaning up worktree..."-/// They are shown to human users but suppressed in directive mode (shell integration).+/// They are shown to users in both modes (users need to see what's happening).pub fn progress(message: impl Into<String>) -> io::Result<()> {OUTPUT_CONTEXT.with(|ctx| {let msg = message.into();@@ -82,6 +82,20 @@ pub fn progress(message: impl Into<String>) -> io::Result<()> {})}+/// Emit a hint message (only shown in interactive mode)+///+/// Hints are suggestions for interactive users, like "To enable automatic cd, run: wt configure-shell"+/// They are shown in interactive mode but suppressed in directive mode (where they don't apply).+pub fn hint(message: impl Into<String>) -> io::Result<()> {+ OUTPUT_CONTEXT.with(|ctx| {+ let msg = message.into();+ match &mut *ctx.borrow_mut() {+ OutputHandler::Interactive(i) => i.hint(msg),+ OutputHandler::Directive(d) => d.hint(msg),+ }+ })+}+/// Request directory change (for shell integration)pub fn change_directory(path: impl AsRef<Path>) -> io::Result<()> {OUTPUT_CONTEXT.with(|ctx| {diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex ea0d46d50..cc54f9e6a 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -78,8 +78,9 @@ pub fn handle_switch_output(if let Some(cmd) = execute {super::execute(cmd)?;} else {- // No execute command: show shell integration hint (only in interactive mode)- super::progress(format!("\n{}", shell_integration_hint()))?;+ // No execute command: show shell integration hint+ // (suppressed in directive mode since user already has integration)+ super::hint(format!("\n{}", shell_integration_hint()))?;}// Flush output (important for directive mode)diff --git a/src/output/interactive.rs b/src/output/interactive.rsindex 2fe7893f1..bca9ded66 100644--- a/src/output/interactive.rs+++ b/src/output/interactive.rs@@ -29,6 +29,13 @@ impl InteractiveOutput {Ok(())}+ pub fn hint(&mut self, message: String) -> io::Result<()> {+ // Hints are suggestions for interactive users (like "run wt configure-shell")+ println!("{message}");+ io::stdout().flush()?;+ Ok(())+ }+pub fn change_directory(&mut self, path: &Path) -> io::Result<()> {// In interactive mode, we can't actually change directory// Just store the target for execute commandsdiff --git a/src/output/mod.rs b/src/output/mod.rsindex 1f0f3b4f0..4a432e23d 100644--- a/src/output/mod.rs+++ b/src/output/mod.rs@@ -63,7 +63,8 @@ pub mod interactive;// Re-export the public APIpub use global::{- OutputMode, change_directory, execute, flush, initialize, progress, success, terminate_output,+ OutputMode, change_directory, execute, flush, hint, initialize, progress, success,+ terminate_output,};// Re-export output handlersdiff --git a/tests/integration_tests/directives.rs b/tests/integration_tests/directives.rsindex bd04290c5..7311fb9e0 100644--- a/tests/integration_tests/directives.rs+++ b/tests/integration_tests/directives.rs@@ -173,15 +173,9 @@ fn test_merge_internal_keep() {.arg("--keep").current_dir(&feature_wt);- assert_cmd_snapshot!(cmd, @"success: true-exit_code: 0------ stdout ------π Validating merge...\0π Rebasing onto main...\0π Pushing 1 commit to main @ [SHA]-\0* 6551244 (HEAD -> feature) Add feature file-\0 feature.txt | 1 +- 1 file changed, 1 insertion(+)-\0β Pushed to main (1 commit, 1 file, +1) \0\0Kept worktree (use 'wt remove' to clean up)\0------ stderr -----");+ // Note: Using file snapshot instead of inline because multiline inline snapshots+ // don't work well with NUL bytes (\0) in the output+ assert_cmd_snapshot!(cmd);});}@@ -237,14 +231,8 @@ fn test_merge_internal_remove() {.arg("main").current_dir(&feature_wt);- assert_cmd_snapshot!(cmd, @"success: true-exit_code: 0------ stdout ------π Validating merge...\0π Rebasing onto main...\0π Pushing 1 commit to main @ [SHA]-\0* 6551244 (HEAD -> feature) Add feature file-\0 feature.txt | 1 +- 1 file changed, 1 insertion(+)-\0β Pushed to main (1 commit, 1 file, +1) \0π Cleaning up worktree...\0__WORKTRUNK_CD__[PATH]\0\0Returned to primary at [REPO]\0------ stderr -----");+ // Note: Using file snapshot instead of inline because multiline inline snapshots+ // don't work well with NUL bytes (\0) in the output+ assert_cmd_snapshot!(cmd);});}diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rsindex 5a5b15c44..054143d08 100644--- a/tests/integration_tests/shell_wrapper.rs+++ b/tests/integration_tests/shell_wrapper.rs@@ -371,6 +371,36 @@ command = "echo 'test command executed'"assert_snapshot!(normalized.as_ref());}+ #[test]+ fn test_bash_shell_integration_hint_suppressed() {+ let repo = TestRepo::new();+ repo.commit("Initial commit");++ // When running through the shell wrapper, the "To enable automatic cd" hint+ // should NOT appear because the user already has shell integration+ let output = exec_through_bash_wrapper(&repo, "switch", &["--create", "bash-test"]);++ // Critical: shell integration hint must be suppressed in directive mode+ assert!(+ !output.contains("To enable automatic cd"),+ "Shell integration hint should not appear when running through wrapper. Output:\n{}",+ output+ );++ // Should still have the success message+ assert!(+ output.contains("Created new worktree"),+ "Success message missing"+ );++ // Normalize paths in output for snapshot testing+ let normalized = regex::Regex::new(r"/private/var/folders/[^/]+/[^/]+/T/\.tmp[^/]+")+ .unwrap()+ .replace_all(&output, "[TMPDIR]");++ assert_snapshot!(normalized.as_ref());+ }+#[test]fn test_wrapper_preserves_progress_messages() {let repo = TestRepo::new();@@ -515,6 +545,36 @@ command = "echo 'fish background task'"assert_snapshot!(normalized.as_ref());}+ #[test]+ fn test_fish_shell_integration_hint_suppressed() {+ let repo = TestRepo::new();+ repo.commit("Initial commit");++ // When running through the shell wrapper, the "To enable automatic cd" hint+ // should NOT appear because the user already has shell integration+ let output = exec_through_fish_wrapper(&repo, "switch", &["--create", "fish-test"]);++ // Critical: shell integration hint must be suppressed in directive mode+ assert!(+ !output.contains("To enable automatic cd"),+ "Shell integration hint should not appear when running through wrapper. Output:\n{}",+ output+ );++ // Should still have the success message+ assert!(+ output.contains("Created new worktree"),+ "Success message missing"+ );++ // Normalize paths in output for snapshot testing+ let normalized = regex::Regex::new(r"/private/var/folders/[^/]+/[^/]+/T/\.tmp[^/]+")+ .unwrap()+ .replace_all(&output, "[TMPDIR]");++ assert_snapshot!(normalized.as_ref());+ }+#[test]fn test_fish_remove_no_directive_leak() {let mut repo = TestRepo::new();diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__bash_shell_integration_hint_suppressed.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__bash_shell_integration_hint_suppressed.snapnew file mode 100644index 000000000..191d35d7c--- /dev/null+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__bash_shell_integration_hint_suppressed.snap@@ -0,0 +1,5 @@+---+source: tests/integration_tests/shell_wrapper.rs+expression: normalized.as_ref()+---+Created new worktree for [1mbash-test[0m at [TMPDIR]/test-repo.bash-testdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_multiline_command_execution.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_multiline_command_execution.snapindex 469ec5864..4ff650de5 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_multiline_command_execution.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_multiline_command_execution.snap@@ -2,7 +2,7 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Created new worktree for fish-multiline at [TMPDIR]/test-repo.fish-multiline+Created new worktree for [1mfish-multiline[0m at [TMPDIR]/test-repo.fish-multilineline 1line 2line 3diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_remove_no_directive_leak.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_remove_no_directive_leak.snapindex ba57bc54a..89858f611 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_remove_no_directive_leak.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_remove_no_directive_leak.snap@@ -2,4 +2,4 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Removed worktree for fish-to-remove+Removed worktree for [1mfish-to-remove[0mdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_shell_integration_hint_suppressed.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_shell_integration_hint_suppressed.snapnew file mode 100644index 000000000..ee63f6121--- /dev/null+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_shell_integration_hint_suppressed.snap@@ -0,0 +1,5 @@+---+source: tests/integration_tests/shell_wrapper.rs+expression: normalized.as_ref()+---+Created new worktree for [1mfish-test[0m at [TMPDIR]/test-repo.fish-testdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_switch_create_no_directive_leak.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_switch_create_no_directive_leak.snapindex 3653a65c8..7e26bb7bb 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_switch_create_no_directive_leak.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_switch_create_no_directive_leak.snap@@ -2,6 +2,4 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Created new worktree for fish-feature at [TMPDIR]/test-repo.fish-feature--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mfish-feature[0m at [TMPDIR]/test-repo.fish-featurediff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_handles_empty_chunks.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_handles_empty_chunks.snapindex 07cb096e3..d7f114c09 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_handles_empty_chunks.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_handles_empty_chunks.snap@@ -2,6 +2,4 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Created new worktree for fish-minimal at [TMPDIR]/test-repo.fish-minimal--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mfish-minimal[0m at [TMPDIR]/test-repo.fish-minimaldiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_preserves_progress_messages.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_preserves_progress_messages.snapindex 54ffee1a0..2356e4528 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_preserves_progress_messages.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_wrapper_preserves_progress_messages.snap@@ -2,9 +2,7 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----π Starting (background):- echo 'fish background task'+π [36mStarting (background):[0m+[40m [0m echo 'fish background task'-Created new worktree for fish-bg at [TMPDIR]/test-repo.fish-bg--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mfish-bg[0m at [TMPDIR]/test-repo.fish-bgdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_through_wrapper_no_directive_leak.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_through_wrapper_no_directive_leak.snapindex e87fc5646..8988b0ab3 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_through_wrapper_no_directive_leak.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_through_wrapper_no_directive_leak.snap@@ -2,5 +2,5 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----π Validating merge...-Already on main, nothing to merge+π [36mValidating merge...[0m+Already on [1mmain[0m, nothing to mergediff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_through_wrapper_no_directive_leak.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_through_wrapper_no_directive_leak.snapindex 5b5123bd0..7367ff4c8 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_through_wrapper_no_directive_leak.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_through_wrapper_no_directive_leak.snap@@ -2,4 +2,4 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Removed worktree for to-remove+Removed worktree for [1mto-remove[0mdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_through_wrapper_no_directive_leak.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_through_wrapper_no_directive_leak.snapindex b422750b2..8bd165fbc 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_through_wrapper_no_directive_leak.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_through_wrapper_no_directive_leak.snap@@ -2,6 +2,4 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Created new worktree for feature at [TMPDIR]/test-repo.feature--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mfeature[0m at [TMPDIR]/test-repo.featurediff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_through_wrapper.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_through_wrapper.snapindex 884eb3b3b..bc351b769 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_through_wrapper.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_through_wrapper.snap@@ -2,5 +2,5 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----Created new worktree for test-exec at [TMPDIR]/test-repo.test-exec+Created new worktree for [1mtest-exec[0m at [TMPDIR]/test-repo.test-execexecuteddiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_post_start_command_no_directive_leak.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_post_start_command_no_directive_leak.snapindex 41378e994..84e9c9b90 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_post_start_command_no_directive_leak.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_post_start_command_no_directive_leak.snap@@ -2,9 +2,7 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----π Starting (background):- echo 'test command executed'+π [36mStarting (background):[0m+[40m [0m echo 'test command executed'-Created new worktree for feature-with-hooks at [TMPDIR]/test-repo.feature-with-hooks--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mfeature-with-hooks[0m at [TMPDIR]/test-repo.feature-with-hooksdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__wrapper_preserves_progress_messages.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__wrapper_preserves_progress_messages.snapindex 18bdf6bd7..3b25deb00 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__wrapper_preserves_progress_messages.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__wrapper_preserves_progress_messages.snap@@ -2,9 +2,7 @@source: tests/integration_tests/shell_wrapper.rsexpression: normalized.as_ref()----π Starting (background):- echo 'background task'+π [36mStarting (background):[0m+[40m [0m echo 'background task'-Created new worktree for feature-bg at [TMPDIR]/test-repo.feature-bg--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mfeature-bg[0m at [TMPDIR]/test-repo.feature-bgdiff --git a/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snap b/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snapnew file mode 100644index 000000000..05cdfdcbeBinary files /dev/null and b/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snap differdiff --git a/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap b/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snapnew file mode 100644index 000000000..ff2e9747bBinary files /dev/null and b/tests/snapshots/integration__integration_tests__directives__merge_internal_remove.snap differdiff --git a/tests/snapshots/integration__integration_tests__e2e_shell__bash_e2e_switch_linebreaks.snap b/tests/snapshots/integration__integration_tests__e2e_shell__bash_e2e_switch_linebreaks.snapindex 673e47d2a..9bcea3c61 100644--- a/tests/snapshots/integration__integration_tests__e2e_shell__bash_e2e_switch_linebreaks.snap+++ b/tests/snapshots/integration__integration_tests__e2e_shell__bash_e2e_switch_linebreaks.snap@@ -2,7 +2,5 @@source: tests/integration_tests/e2e_shell.rsexpression: output----Created new worktree for test-branch at [REPO].test-branch--To enable automatic cd, run: wt configure-shell+Created new worktree for [1mtest-branch[0m at [REPO].test-branchMARKERdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap b/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snapindex e5317ec7b..492c08a5d 100644Binary files a/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap and b/tests/snapshots/integration__integration_tests__switch__switch_existing_internal.snap differdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap b/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snapindex 28874ef36..70114eb1c 100644Binary files a/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap and b/tests/snapshots/integration__integration_tests__switch__switch_internal_mode.snap differdiff --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 73f2ac660..88e45e48b 100644Binary files a/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap and b/tests/snapshots/integration__integration_tests__switch__switch_internal_with_execute.snap differ