Skip to content

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

Remove `command_output` API for direct streaming

Maximilian Roosda1fbe0ff339
diff --git a/src/output/directive.rs b/src/output/directive.rs
index f4923b3a7..032fe8aa0 100644
--- a/src/output/directive.rs
+++ b/src/output/directive.rs
@@ -55,18 +55,10 @@ impl DirectiveOutput {
io::stderr().flush()
}
- pub fn command_output(&mut self, stdout: &str, stderr: &str) -> io::Result<()> {
- // Always write NUL to stderr first, even if empty
- // This ensures subsequent directives are in separate chunks
- write!(io::stderr(), "{}\0", stderr.trim_end())?;
- io::stderr().flush()?;
-
- // Always write NUL to stdout, even if empty
- // This maintains consistent chunk boundaries for the shell wrapper
- write!(io::stdout(), "{}\0", stdout.trim_end())?;
- io::stdout().flush()?;
-
- Ok(())
+ pub fn terminate_output(&mut self) -> io::Result<()> {
+ // Write NUL terminator to separate command output from subsequent directives
+ write!(io::stdout(), "\0")?;
+ io::stdout().flush()
}
}
diff --git a/src/output/global.rs b/src/output/global.rs
index e04b45f26..0e5587205 100644
--- a/src/output/global.rs
+++ b/src/output/global.rs
@@ -112,15 +112,14 @@ pub fn flush() -> io::Result<()> {
})
}
-/// Emit command output (stdout/stderr from external processes)
+/// Terminate command output
///
-/// In directive mode, ensures output is NUL-terminated so subsequent directives
-/// are in separate chunks for the shell wrapper to parse correctly.
-/// In interactive mode, prints output normally with colors.
-pub fn command_output(stdout: &str, stderr: &str) -> io::Result<()> {
+/// In directive mode, writes a NUL terminator to separate command output from
+/// subsequent directives. In interactive mode, this is a no-op.
+pub fn terminate_output() -> io::Result<()> {
OUTPUT_CONTEXT.with(|ctx| match &mut *ctx.borrow_mut() {
- OutputHandler::Interactive(i) => i.command_output(stdout, stderr),
- OutputHandler::Directive(d) => d.command_output(stdout, stderr),
+ OutputHandler::Interactive(i) => i.terminate_output(),
+ OutputHandler::Directive(d) => d.terminate_output(),
})
}
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index aab5c3e97..ed0551f6d 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -109,32 +109,34 @@ pub fn handle_remove_output(result: &RemoveResult) -> Result<(), GitError> {
}
/// Execute a command in a worktree directory
+///
+/// Uses Stdio::inherit() for real-time streaming output in both modes.
+/// Calls terminate_output() after completion to handle mode-specific cleanup
+/// (NUL terminator in directive mode, no-op in interactive mode).
pub fn execute_command_in_worktree(
worktree_path: &std::path::Path,
command: &str,
) -> Result<(), GitError> {
- use std::process::Command;
+ use std::process::{Command, Stdio};
- let output = Command::new("sh")
+ let status = Command::new("sh")
.arg("-c")
.arg(command)
.current_dir(worktree_path)
- .output()
+ .stdout(Stdio::inherit())
+ .stderr(Stdio::inherit())
+ .status()
.map_err(|e| GitError::CommandFailed(format!("Failed to execute command: {}", e)))?;
- if !output.status.success() {
+ if !status.success() {
return Err(GitError::CommandFailed(format!(
"Command failed with exit code: {}",
- output.status
+ status
)));
}
- // Print command output through the framework
- super::command_output(
- &String::from_utf8_lossy(&output.stdout),
- &String::from_utf8_lossy(&output.stderr),
- )
- .map_err(|e| GitError::CommandFailed(e.to_string()))?;
+ // Terminate output (adds NUL in directive mode, no-op in interactive)
+ super::terminate_output().map_err(|e| GitError::CommandFailed(e.to_string()))?;
Ok(())
}
diff --git a/src/output/interactive.rs b/src/output/interactive.rs
index 1e2265a13..a3dc85095 100644
--- a/src/output/interactive.rs
+++ b/src/output/interactive.rs
@@ -73,14 +73,8 @@ impl InteractiveOutput {
Ok(())
}
- pub fn command_output(&mut self, stdout: &str, stderr: &str) -> io::Result<()> {
- if !stdout.is_empty() {
- println!("{}", stdout.trim_end());
- }
- if !stderr.is_empty() {
- use worktrunk::styling::eprintln;
- eprintln!("{}", stderr.trim_end());
- }
+ pub fn terminate_output(&mut self) -> io::Result<()> {
+ // No-op in interactive mode - no NUL terminators needed
Ok(())
}
}
diff --git a/src/output/mod.rs b/src/output/mod.rs
index 579400762..1f0f3b4f0 100644
--- a/src/output/mod.rs
+++ b/src/output/mod.rs
@@ -63,7 +63,7 @@ pub mod interactive;
// Re-export the public API
pub use global::{
- OutputMode, change_directory, command_output, execute, flush, initialize, progress, success,
+ OutputMode, change_directory, execute, flush, initialize, progress, success, terminate_output,
};
// Re-export output handlers