use std::io::IsTerminal;
use std::time::{Duration, Instant};
fn wrap(use_color: bool, code: &str, s: &str) -> String {
if use_color {
format!("\x1b[{code}m{s}\x1b[0m")
} else {
s.to_string()
}
}
const COLOR_BOLD: &str = "1";
const COLOR_DIM: &str = "2";
const COLOR_GREEN: &str = "32";
const COLOR_RED: &str = "31";
const COLOR_CYAN: &str = "36";
pub struct ProgressReporter {
#[allow(dead_code)]
start: Instant,
use_color: bool,
}
impl Default for ProgressReporter {
fn default() -> Self {
Self::new()
}
}
impl ProgressReporter {
pub fn new() -> Self {
Self {
start: Instant::now(),
use_color: std::io::stderr().is_terminal(),
}
}
#[allow(dead_code)]
pub fn with_color(mut self, use_color: bool) -> Self {
self.use_color = use_color;
self
}
pub fn format_phase_start(&self, phase: &str) -> String {
let arrow = wrap(self.use_color, COLOR_CYAN, "▶");
let arrow = wrap(self.use_color, COLOR_DIM, &arrow);
let name = wrap(self.use_color, COLOR_BOLD, &format!("{phase:<10}"));
format!("[tcode] {arrow} {name} starting…")
}
pub fn phase_start(&self, phase: &str) {
eprintln!("\r{}", self.format_phase_start(phase));
}
pub fn format_phase_done(
&self,
phase: &str,
elapsed: Duration,
cost: f32,
note: Option<&str>,
) -> String {
let check = wrap(self.use_color, COLOR_GREEN, "✓");
let name = wrap(self.use_color, COLOR_BOLD, &format!("{phase:<10}"));
let stats = format!("({}, ~${:.2})", format_duration(elapsed), cost);
let stats = wrap(self.use_color, COLOR_DIM, &stats);
match note {
Some(n) if !n.is_empty() => {
format!("[tcode] {check} {name} done {stats} — {n}")
}
_ => format!("[tcode] {check} {name} done {stats}"),
}
}
pub fn phase_done(&self, phase: &str, elapsed: Duration, cost: f32, note: Option<&str>) {
eprintln!("\r{}", self.format_phase_done(phase, elapsed, cost, note));
}
pub fn phase_failed(&self, phase: &str, elapsed: Duration, error: &str) {
let cross = wrap(self.use_color, COLOR_RED, "✗");
let name = wrap(self.use_color, COLOR_BOLD, &format!("{phase:<10}"));
let stats = wrap(
self.use_color,
COLOR_DIM,
&format!("({})", format_duration(elapsed)),
);
let short = error.lines().next().unwrap_or(error);
let short: String = short.chars().take(120).collect();
eprintln!("\r[tcode] {cross} {name} failed {stats} — {short}");
}
pub fn wave_start(&self, wave_index: usize, wave_total: usize, file_count: usize) {
let arrow = wrap(self.use_color, COLOR_DIM, "▶");
let name = wrap(self.use_color, COLOR_BOLD, "code ");
eprintln!(
"\r[tcode] {arrow} {name} wave {wave_index}/{wave_total} ({file_count} file{plural})",
plural = if file_count == 1 { "" } else { "s" },
);
}
pub fn wave_done(&self, wave_index: usize, wave_total: usize, elapsed: Duration) {
let check = wrap(self.use_color, COLOR_GREEN, "✓");
let name = wrap(self.use_color, COLOR_BOLD, "code ");
let stats = wrap(
self.use_color,
COLOR_DIM,
&format!("({})", format_duration(elapsed)),
);
eprintln!("\r[tcode] {check} {name} wave {wave_index}/{wave_total} done {stats}");
}
pub fn workflow_done(&self, total_elapsed: Duration, total_cost: f32) {
let check = wrap(self.use_color, COLOR_GREEN, "✓");
let name = wrap(self.use_color, COLOR_BOLD, "workflow ");
let stats = wrap(
self.use_color,
COLOR_DIM,
&format!(
"({}, ${:.2} total)",
format_duration(total_elapsed),
total_cost
),
);
eprintln!("\r[tcode] {check} {name} complete {stats}");
}
#[allow(dead_code)]
pub fn workflow_started(&self) -> Instant {
self.start
}
}
pub fn format_duration(d: Duration) -> String {
let total = d.as_secs();
let h = total / 3600;
let m = (total % 3600) / 60;
let s = total % 60;
if h > 0 {
format!("{h}h{m:02}m{s:02}s")
} else if m > 0 {
format!("{m}m{s:02}s")
} else {
format!("{s}s")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn progress_reporter_formats_phase_start() {
let r = ProgressReporter::new().with_color(false);
let s = r.format_phase_start("research");
assert!(s.contains("research"), "got: {s}");
assert!(s.contains("starting"), "got: {s}");
assert!(s.starts_with("[tcode]"), "got: {s}");
assert!(!s.contains("\x1b["), "expected no ANSI in: {s}");
}
#[test]
fn progress_reporter_formats_phase_done_no_color() {
let r = ProgressReporter::new().with_color(false);
let s = r.format_phase_done("plan", Duration::from_secs(18), 0.04, None);
assert!(s.contains("plan"), "got: {s}");
assert!(s.contains("done"), "got: {s}");
assert!(s.contains("18s"), "got: {s}");
assert!(s.contains("$0.04"), "got: {s}");
assert!(!s.contains("\x1b["), "expected no ANSI in: {s}");
}
#[test]
fn progress_reporter_phase_done_includes_note() {
let r = ProgressReporter::new().with_color(false);
let s = r.format_phase_done("qa", Duration::from_secs(28), 0.09, Some("35/35 passed"));
assert!(s.contains("35/35 passed"), "got: {s}");
}
#[test]
fn progress_reporter_uses_color_when_enabled() {
let r = ProgressReporter::new().with_color(true);
let s = r.format_phase_start("research");
assert!(s.contains("\x1b["), "expected ANSI in: {s}");
}
#[test]
fn format_duration_renders_human_friendly() {
assert_eq!(format_duration(Duration::from_secs(42)), "42s");
assert_eq!(format_duration(Duration::from_secs(192)), "3m12s");
assert_eq!(format_duration(Duration::from_secs(3912)), "1h05m12s");
assert_eq!(format_duration(Duration::from_secs(0)), "0s");
}
}