1use std::io::IsTerminal;
21use std::time::{Duration, Instant};
22
23fn wrap(use_color: bool, code: &str, s: &str) -> String {
31 if use_color {
32 format!("\x1b[{code}m{s}\x1b[0m")
33 } else {
34 s.to_string()
35 }
36}
37
38const COLOR_BOLD: &str = "1";
39const COLOR_DIM: &str = "2";
40const COLOR_GREEN: &str = "32";
41const COLOR_RED: &str = "31";
42const COLOR_CYAN: &str = "36";
43
44pub struct ProgressReporter {
54 #[allow(dead_code)]
55 start: Instant,
56 use_color: bool,
57}
58
59impl Default for ProgressReporter {
60 fn default() -> Self {
61 Self::new()
62 }
63}
64
65impl ProgressReporter {
66 pub fn new() -> Self {
72 Self {
73 start: Instant::now(),
74 use_color: std::io::stderr().is_terminal(),
75 }
76 }
77
78 #[allow(dead_code)]
80 pub fn with_color(mut self, use_color: bool) -> Self {
81 self.use_color = use_color;
82 self
83 }
84
85 pub fn format_phase_start(&self, phase: &str) -> String {
88 let arrow = wrap(self.use_color, COLOR_CYAN, "▶");
89 let arrow = wrap(self.use_color, COLOR_DIM, &arrow);
90 let name = wrap(self.use_color, COLOR_BOLD, &format!("{phase:<10}"));
91 format!("[tcode] {arrow} {name} starting…")
92 }
93
94 pub fn phase_start(&self, phase: &str) {
96 eprintln!("\r{}", self.format_phase_start(phase));
97 }
98
99 pub fn format_phase_done(
101 &self,
102 phase: &str,
103 elapsed: Duration,
104 cost: f32,
105 note: Option<&str>,
106 ) -> String {
107 let check = wrap(self.use_color, COLOR_GREEN, "✓");
108 let name = wrap(self.use_color, COLOR_BOLD, &format!("{phase:<10}"));
109 let stats = format!("({}, ~${:.2})", format_duration(elapsed), cost);
110 let stats = wrap(self.use_color, COLOR_DIM, &stats);
111 match note {
112 Some(n) if !n.is_empty() => {
113 format!("[tcode] {check} {name} done {stats} — {n}")
114 }
115 _ => format!("[tcode] {check} {name} done {stats}"),
116 }
117 }
118
119 pub fn phase_done(&self, phase: &str, elapsed: Duration, cost: f32, note: Option<&str>) {
121 eprintln!("\r{}", self.format_phase_done(phase, elapsed, cost, note));
122 }
123
124 pub fn phase_failed(&self, phase: &str, elapsed: Duration, error: &str) {
126 let cross = wrap(self.use_color, COLOR_RED, "✗");
127 let name = wrap(self.use_color, COLOR_BOLD, &format!("{phase:<10}"));
128 let stats = wrap(
129 self.use_color,
130 COLOR_DIM,
131 &format!("({})", format_duration(elapsed)),
132 );
133 let short = error.lines().next().unwrap_or(error);
135 let short: String = short.chars().take(120).collect();
136 eprintln!("\r[tcode] {cross} {name} failed {stats} — {short}");
137 }
138
139 pub fn wave_start(&self, wave_index: usize, wave_total: usize, file_count: usize) {
143 let arrow = wrap(self.use_color, COLOR_DIM, "▶");
144 let name = wrap(self.use_color, COLOR_BOLD, "code ");
145 eprintln!(
146 "\r[tcode] {arrow} {name} wave {wave_index}/{wave_total} ({file_count} file{plural})",
147 plural = if file_count == 1 { "" } else { "s" },
148 );
149 }
150
151 pub fn wave_done(&self, wave_index: usize, wave_total: usize, elapsed: Duration) {
153 let check = wrap(self.use_color, COLOR_GREEN, "✓");
154 let name = wrap(self.use_color, COLOR_BOLD, "code ");
155 let stats = wrap(
156 self.use_color,
157 COLOR_DIM,
158 &format!("({})", format_duration(elapsed)),
159 );
160 eprintln!("\r[tcode] {check} {name} wave {wave_index}/{wave_total} done {stats}");
161 }
162
163 pub fn workflow_done(&self, total_elapsed: Duration, total_cost: f32) {
165 let check = wrap(self.use_color, COLOR_GREEN, "✓");
166 let name = wrap(self.use_color, COLOR_BOLD, "workflow ");
167 let stats = wrap(
168 self.use_color,
169 COLOR_DIM,
170 &format!(
171 "({}, ${:.2} total)",
172 format_duration(total_elapsed),
173 total_cost
174 ),
175 );
176 eprintln!("\r[tcode] {check} {name} complete {stats}");
177 }
178
179 #[allow(dead_code)]
182 pub fn workflow_started(&self) -> Instant {
183 self.start
184 }
185}
186
187pub fn format_duration(d: Duration) -> String {
195 let total = d.as_secs();
196 let h = total / 3600;
197 let m = (total % 3600) / 60;
198 let s = total % 60;
199 if h > 0 {
200 format!("{h}h{m:02}m{s:02}s")
201 } else if m > 0 {
202 format!("{m}m{s:02}s")
203 } else {
204 format!("{s}s")
205 }
206}
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211
212 #[test]
213 fn progress_reporter_formats_phase_start() {
214 let r = ProgressReporter::new().with_color(false);
215 let s = r.format_phase_start("research");
216 assert!(s.contains("research"), "got: {s}");
217 assert!(s.contains("starting"), "got: {s}");
218 assert!(s.starts_with("[tcode]"), "got: {s}");
219 assert!(!s.contains("\x1b["), "expected no ANSI in: {s}");
221 }
222
223 #[test]
224 fn progress_reporter_formats_phase_done_no_color() {
225 let r = ProgressReporter::new().with_color(false);
226 let s = r.format_phase_done("plan", Duration::from_secs(18), 0.04, None);
227 assert!(s.contains("plan"), "got: {s}");
228 assert!(s.contains("done"), "got: {s}");
229 assert!(s.contains("18s"), "got: {s}");
230 assert!(s.contains("$0.04"), "got: {s}");
231 assert!(!s.contains("\x1b["), "expected no ANSI in: {s}");
232 }
233
234 #[test]
235 fn progress_reporter_phase_done_includes_note() {
236 let r = ProgressReporter::new().with_color(false);
237 let s = r.format_phase_done("qa", Duration::from_secs(28), 0.09, Some("35/35 passed"));
238 assert!(s.contains("35/35 passed"), "got: {s}");
239 }
240
241 #[test]
242 fn progress_reporter_uses_color_when_enabled() {
243 let r = ProgressReporter::new().with_color(true);
244 let s = r.format_phase_start("research");
245 assert!(s.contains("\x1b["), "expected ANSI in: {s}");
246 }
247
248 #[test]
249 fn format_duration_renders_human_friendly() {
250 assert_eq!(format_duration(Duration::from_secs(42)), "42s");
251 assert_eq!(format_duration(Duration::from_secs(192)), "3m12s");
252 assert_eq!(format_duration(Duration::from_secs(3912)), "1h05m12s");
253 assert_eq!(format_duration(Duration::from_secs(0)), "0s");
254 }
255}