Skip to content

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

Rename output::raw/raw_terminal to data/table

Maximilian Roos0a2e1de9d9a2
diff --git a/CLAUDE.md b/CLAUDE.md
index 41f51cd15..4d6ad903d 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -485,8 +485,8 @@ The output module (`src/output/global.rs`) provides:
- `shell_integration_hint(message)` - Shell integration hints (💡, suppressed in directive)
- `gutter(content)` - Gutter-formatted content (use with `format_with_gutter()`)
- `blank()` - Blank line for visual separation
-- `raw(content)` - Raw output without emoji (JSON data)
-- `raw_terminal(content)` - Raw terminal output to stderr (tables)
+- `data(content)` - Structured data output without emoji (JSON, for piping)
+- `table(content)` - Table/UI output to stderr
- `change_directory(path)` - Request directory change
- `execute(command)` - Execute command or buffer for shell script
- `flush()` - Flush output buffers
diff --git a/src/commands/config.rs b/src/commands/config.rs
index a9a921d29..804fa8d0c 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -457,8 +457,8 @@ pub fn handle_cache_show() -> anyhow::Result<()> {
// Show default branch cache
crate::output::info(format!("{HINT}Default branch cache:{HINT:#}"))?;
match repo.default_branch() {
- Ok(branch) => crate::output::raw(format!(" {branch}"))?,
- Err(_) => crate::output::raw(" (not cached)")?,
+ Ok(branch) => crate::output::data(format!(" {branch}"))?,
+ Err(_) => crate::output::data(" (not cached)")?,
}
crate::output::blank()?;
@@ -467,7 +467,7 @@ pub fn handle_cache_show() -> anyhow::Result<()> {
let entries = CachedCiStatus::list_all(&repo);
if entries.is_empty() {
- crate::output::raw(" (no CI cache entries)")?;
+ crate::output::data(" (no CI cache entries)")?;
return Ok(());
}
@@ -483,7 +483,7 @@ pub fn handle_cache_show() -> anyhow::Result<()> {
let age = now_secs.saturating_sub(cached.checked_at);
let head: String = cached.head.chars().take(8).collect();
- crate::output::raw(format!(" {branch}: {status} (age: {age}s, head: {head})"))?;
+ crate::output::data(format!(" {branch}: {status} (age: {age}s, head: {head})"))?;
}
Ok(())
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index 1ed863186..b43620519 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -666,14 +666,12 @@ pub fn collect(
layout.hidden_nonempty_count,
);
- crate::output::raw_terminal(layout.format_header_line())?;
+ crate::output::table(layout.format_header_line())?;
for item in &all_items {
- crate::output::raw_terminal(
- layout.format_list_item_line(item, previous_branch.as_deref()),
- )?;
+ crate::output::table(layout.format_list_item_line(item, previous_branch.as_deref()))?;
}
- crate::output::raw_terminal("")?;
- crate::output::raw_terminal(final_msg)?;
+ crate::output::table("")?;
+ crate::output::table(final_msg)?;
}
// Status symbols are now computed during data collection (both modes), no fallback needed
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index 516f652cb..6b2b9b330 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -180,7 +180,7 @@ pub fn handle_list(
// Display fields are already computed in collect()
let json = serde_json::to_string_pretty(&items)
.map_err(|e| anyhow::anyhow!("Failed to serialize to JSON: {}", e))?;
- crate::output::raw(json)?;
+ crate::output::data(json)?;
}
crate::OutputFormat::Table => {
// Table and summary already rendered in collect() for all modes
diff --git a/src/commands/list/progressive.rs b/src/commands/list/progressive.rs
index fb23a4b36..7e28c2568 100644
--- a/src/commands/list/progressive.rs
+++ b/src/commands/list/progressive.rs
@@ -27,7 +27,7 @@ impl RenderMode {
// Priority 2: Auto-detect based on TTY
// Check the appropriate stream based on output mode:
- // - Directive mode: check stderr (where table output goes via output::raw())
+ // - Directive mode: check stderr (where table output goes via output::table())
// - Interactive mode: check stdout (where table output goes)
use std::io::IsTerminal;
let is_tty = if directive_mode {
diff --git a/src/commands/statusline.rs b/src/commands/statusline.rs
index 10bd087b3..173f64977 100644
--- a/src/commands/statusline.rs
+++ b/src/commands/statusline.rs
@@ -198,9 +198,9 @@ pub fn run(claude_code: bool) -> Result<()> {
if !output.is_empty() {
if claude_code {
let reset = anstyle::Reset;
- output::raw(format!("{reset} {output}"))?;
+ output::data(format!("{reset} {output}"))?;
} else {
- output::raw(output)?;
+ output::data(output)?;
}
}
diff --git a/src/output/directive.rs b/src/output/directive.rs
index 9cf8c6681..3fb4dbab4 100644
--- a/src/output/directive.rs
+++ b/src/output/directive.rs
@@ -89,7 +89,7 @@ impl OutputHandler for DirectiveOutput {
Ok(())
}
- // Note: raw() uses the default which calls write_message_line() -> stderr
+ // Note: data() uses the default which calls write_message_line() -> stderr
// This is correct for directive mode where stdout is reserved for the final shell script
fn change_directory(&mut self, path: &Path) -> io::Result<()> {
diff --git a/src/output/global.rs b/src/output/global.rs
index 644674ff6..e1a338a73 100644
--- a/src/output/global.rs
+++ b/src/output/global.rs
@@ -127,37 +127,33 @@ pub fn blank() -> io::Result<()> {
with_output(|h| h.blank())
}
-/// Emit raw output without emoji decoration
+/// Emit structured data output without emoji decoration
///
-/// Used for structured data like JSON. Goes to stdout in interactive mode,
-/// stderr in directive mode (where stdout is reserved for directives).
+/// Used for JSON and other pipeable data. In interactive mode, writes to stdout
+/// for piping. In directive mode, writes to stderr (where user messages go).
///
/// Example:
/// ```rust,ignore
-/// output::raw(json_string)?;
+/// output::data(json_string)?;
/// ```
-pub fn raw(content: impl Into<String>) -> io::Result<()> {
- with_output(|h| h.raw(content.into()))
+pub fn data(content: impl Into<String>) -> io::Result<()> {
+ with_output(|h| h.data(content.into()))
}
-/// Emit raw terminal output to stderr
+/// Emit table/UI output to stderr
///
-/// Used for table output that should appear on the same stream as progress bars.
-/// Goes to stderr in both interactive and directive modes.
-///
-/// TODO: This split between raw() and raw_terminal() is messy. Consider unifying
-/// the output system to have a clearer separation between structured data (JSON)
-/// and terminal UI (tables, progress bars).
+/// Used for table rows and progress indicators that should appear on the same
+/// stream as progress bars. Both modes write to stderr.
///
/// Example:
/// ```rust,ignore
-/// output::raw_terminal(layout.format_header_line())?;
+/// output::table(layout.format_header_line())?;
/// for item in items {
-/// output::raw_terminal(layout.format_item_line(item))?;
+/// output::table(layout.format_item_line(item))?;
/// }
/// ```
-pub fn raw_terminal(content: impl Into<String>) -> io::Result<()> {
- with_output(|h| h.raw_terminal(content.into()))
+pub fn table(content: impl Into<String>) -> io::Result<()> {
+ with_output(|h| h.table(content.into()))
}
/// Request directory change (for shell integration)
@@ -196,19 +192,6 @@ pub fn terminate_output() -> io::Result<()> {
with_output(|h| h.terminate_output())
}
-/// Format a switch success message (identical across modes)
-///
-/// Both modes now report `"at {path}"` so users see the same wording whether
-/// they invoke worktrunk directly or through the shell wrapper.
-pub fn format_switch_success(
- branch: &str,
- path: &Path,
- created_branch: bool,
- base_branch: Option<&str>,
-) -> String {
- super::format_switch_success_message(branch, path, created_branch, base_branch)
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 6f1509ae3..741b8f66d 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -2,7 +2,6 @@
use crate::commands::process::spawn_detached;
use crate::commands::worktree::{RemoveResult, SwitchResult};
-use crate::output::global::format_switch_success;
use worktrunk::git::GitError;
use worktrunk::git::Repository;
use worktrunk::path::format_path_for_display;
@@ -175,7 +174,9 @@ pub fn handle_switch_output(
if is_directive_mode || has_execute_command || is_configured {
// Shell integration active, --execute provided, or configured - show success
- super::success(format_switch_success(branch, path, false, None))?;
+ super::success(super::format_switch_success_message(
+ branch, path, false, None,
+ ))?;
} else {
// Shell integration not configured - show warning and setup hint
super::warning(format!(
@@ -191,7 +192,7 @@ pub fn handle_switch_output(
base_branch,
} => {
// Creation succeeded - show success
- super::success(format_switch_success(
+ super::success(super::format_switch_success_message(
branch,
path,
*created_branch,
diff --git a/src/output/interactive.rs b/src/output/interactive.rs
index 2391e025c..9bdf3e1c7 100644
--- a/src/output/interactive.rs
+++ b/src/output/interactive.rs
@@ -36,8 +36,8 @@ impl OutputHandler for InteractiveOutput {
stderr().flush()
}
- fn raw(&mut self, content: String) -> io::Result<()> {
- // JSON and structured data go to stdout for piping
+ fn data(&mut self, content: String) -> io::Result<()> {
+ // Structured data (JSON, etc.) goes to stdout for piping
use worktrunk::styling::println;
println!("{content}");
io::stdout().flush()
diff --git a/src/output/mod.rs b/src/output/mod.rs
index 36e075339..7840e9a37 100644
--- a/src/output/mod.rs
+++ b/src/output/mod.rs
@@ -63,8 +63,8 @@ mod traits;
// Re-export the public API
pub use global::{
- OutputMode, blank, change_directory, execute, flush, flush_for_stderr_prompt, gutter, hint,
- info, initialize, print, progress, raw, raw_terminal, shell_integration_hint, success,
+ OutputMode, blank, change_directory, data, execute, flush, flush_for_stderr_prompt, gutter,
+ hint, info, initialize, print, progress, shell_integration_hint, success, table,
terminate_output, warning,
};
// Re-export output handlers
diff --git a/src/output/traits.rs b/src/output/traits.rs
index c87dc5a66..b71186f1d 100644
--- a/src/output/traits.rs
+++ b/src/output/traits.rs
@@ -57,15 +57,19 @@ pub trait OutputHandler {
self.write_message_line("")
}
- /// Emit raw output without emoji decoration
- fn raw(&mut self, content: String) -> io::Result<()> {
+ /// Emit structured data output without emoji decoration
+ ///
+ /// Used for JSON and other pipeable data. In interactive mode, writes to stdout
+ /// for piping. In directive mode, writes to stderr (where user messages go).
+ fn data(&mut self, content: String) -> io::Result<()> {
self.write_message_line(&content)
}
- /// Emit raw terminal output to stderr
+ /// Emit table/UI output to stderr
///
- /// Both modes write to stderr for table output
- fn raw_terminal(&mut self, content: String) -> io::Result<()> {
+ /// Used for table rows and progress indicators that should appear on the same
+ /// stream as progress bars. Both modes write to stderr.
+ fn table(&mut self, content: String) -> io::Result<()> {
use worktrunk::styling::eprintln;
eprintln!("{content}");
io::stderr().flush()
diff --git a/src/path.rs b/src/path.rs
index 723101967..c0155c6b7 100644
--- a/src/path.rs
+++ b/src/path.rs
@@ -1,29 +1,20 @@
use std::path::{Path, PathBuf};
+/// Get the user's home directory.
+///
+/// Uses the `home` crate which handles platform-specific detection:
+/// - Unix: `$HOME` environment variable
+/// - Windows: `USERPROFILE` or `HOMEDRIVE`/`HOMEPATH`
+pub fn home_dir() -> Option<PathBuf> {
+ home::home_dir()
+}
+
/// Format a filesystem path for user-facing output.
///
/// When the path lives under the user's home directory, it is shown with a
/// leading `~` (e.g., `/Users/alex/projects/wt` -> `~/projects/wt`). Paths
/// outside home are returned unchanged.
pub fn format_path_for_display(path: &Path) -> String {
- fn home_dir() -> Option<PathBuf> {
- #[cfg(windows)]
- {
- std::env::var_os("USERPROFILE")
- .map(PathBuf::from)
- .or_else(|| {
- let drive = std::env::var_os("HOMEDRIVE")?;
- let path = std::env::var_os("HOMEPATH")?;
- Some(PathBuf::from(drive).join(path))
- })
- }
-
- #[cfg(not(windows))]
- {
- std::env::var_os("HOME").map(PathBuf::from)
- }
- }
-
if let Some(home) = home_dir()
&& let Ok(stripped) = path.strip_prefix(&home)
{
@@ -41,26 +32,9 @@ pub fn format_path_for_display(path: &Path) -> String {
#[cfg(test)]
mod tests {
- use super::format_path_for_display;
use std::path::PathBuf;
- fn home_dir() -> Option<PathBuf> {
- #[cfg(windows)]
- {
- std::env::var_os("USERPROFILE")
- .map(PathBuf::from)
- .or_else(|| {
- let drive = std::env::var_os("HOMEDRIVE")?;
- let path = std::env::var_os("HOMEPATH")?;
- Some(PathBuf::from(drive).join(path))
- })
- }
-
- #[cfg(not(windows))]
- {
- std::env::var_os("HOME").map(PathBuf::from)
- }
- }
+ use super::{format_path_for_display, home_dir};
#[test]
fn shortens_path_under_home() {
diff --git a/src/shell.rs b/src/shell.rs
index bb4e9c1ee..176747c3d 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -2,9 +2,11 @@ use askama::Template;
use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};
use std::path::PathBuf;
+use crate::path::home_dir;
+
/// Get the user's home directory or return an error
-fn home_dir() -> Result<PathBuf, std::io::Error> {
- home::home_dir().ok_or_else(|| {
+fn home_dir_required() -> Result<PathBuf, std::io::Error> {
+ home_dir().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
"Cannot determine home directory. Set $HOME environment variable",
@@ -28,7 +30,7 @@ impl Shell {
///
/// Returns paths in order of preference. The first existing file should be used.
pub fn config_paths(&self) -> Result<Vec<PathBuf>, std::io::Error> {
- let home = home_dir()?;
+ let home = home_dir_required()?;
Ok(match self {
Self::Bash => {
@@ -60,7 +62,7 @@ impl Shell {
/// (installed by `wt config shell install`) that uses $WORKTRUNK_BIN to bypass
/// the shell function wrapper.
pub fn completion_path(&self) -> Result<PathBuf, std::io::Error> {
- let home = home_dir()?;
+ let home = home_dir_required()?;
// Use etcetera for XDG-compliant paths when available
let strategy = choose_base_strategy().ok();
@@ -120,7 +122,7 @@ impl Shell {
use std::fs;
use std::io::{BufRead, BufReader};
- let home = home_dir()?;
+ let home = home_dir_required()?;
// Check common shell config files for integration patterns
let config_files = vec![
diff --git a/tests/output_system_guard.rs b/tests/output_system_guard.rs
index a592d662d..d6fd282c9 100644
--- a/tests/output_system_guard.rs
+++ b/tests/output_system_guard.rs
@@ -45,7 +45,7 @@ fn check_no_stdout_in_commands() {
panic!(
"stdout writes found in command code (use output::* instead):\n\n{}\n\n\
In directive mode, stdout is reserved for shell script output.\n\
- Use output::info(), output::hint(), output::raw(), etc. instead.",
+ Use output::info(), output::hint(), output::data(), etc. instead.",
violations.join("\n")
);
}