1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::path::Path;
use std::time::Duration;
use super::{Renderer, Writer, role_glyph};
use crate::PathDisplayExt;
use crate::output::{Role, Verbosity, strip_ansi};
/// Inputs to a single Status line. Builders convert to this for rendering.
pub struct StatusFields<'a> {
pub role: Role,
pub subject: &'a str,
pub detail: Option<&'a str>,
pub duration: Option<Duration>,
pub target: Option<&'a Path>,
}
impl Renderer {
/// Top-level status dispatcher. Routes to the topmost open section's
/// pending-statuses buffer when one exists (so subjects can be
/// right-padded to a common column at section close); otherwise writes
/// immediately.
pub fn render_status(&self, w: &dyn Writer, depth: usize, f: &StatusFields<'_>) {
// Status(Fail) is shown even at Quiet.
if self.verbosity == Verbosity::Quiet && f.role != Role::Fail {
return;
}
// Buffer when a section is open AND this status's depth is inside
// (not equal to) the section's header_depth. The depth==header_depth
// case happens for re-routed top-level emits via `enforce_top_level_emit`;
// those should render immediately so the warning shape stays inline.
let buffered = {
let mut s = self.state.lock().unwrap_or_else(|e| e.into_inner());
let mut did_buffer = false;
if let Some(top) = s.section_stack.last_mut()
&& depth > top.header_depth
{
// Inside the section's child region — buffer.
top.pending_statuses.push(super::section::BufferedStatus {
role: f.role,
subject: f.subject.to_string(),
detail: f.detail.map(|d| d.to_string()),
duration: f.duration,
target: f.target.map(|p| p.to_path_buf()),
depth,
});
did_buffer = true;
}
did_buffer
};
if buffered {
// Header emission must still happen so the section's header
// appears before any of its children. This is idempotent — only
// the first call writes anything.
self.flush_pending_section_headers(w);
return;
}
self.render_status_immediate(w, depth, f);
self.mark_top_level_blank_if_at_root();
}
/// Actually emit a Status line, without buffering. Used by the immediate
/// path AND by `flush_pending_statuses` when a section closes.
pub(crate) fn render_status_immediate(
&self,
w: &dyn Writer,
depth: usize,
f: &StatusFields<'_>,
) {
if self.verbosity == Verbosity::Quiet && f.role != Role::Fail {
return;
}
self.flush_pending_section_headers(w);
let (icon_opt, style) = role_glyph(&self.theme, f.role);
let mut line = String::new();
if let Some(icon) = icon_opt {
line.push_str(&style.apply_to(icon).to_string());
line.push(' ');
}
line.push_str(&style.apply_to(f.subject).to_string());
// Field order: subject — detail (target). Detail comes first (with
// em-dash glue), then target in parens. Duration trails last as its
// own (Ns) parens block.
if let Some(detail) = f.detail {
line.push_str(" — ");
// Sanitize at the renderer boundary: detail may carry external
// tool stderr or `format!("{e}")` content with embedded ANSI
// escapes. A stray `\x1b[0m` would prematurely terminate the
// role styling above; foreign color escapes would paint
// subsequent terminal output until the next reset.
line.push_str(&strip_ansi(detail));
}
if let Some(target) = f.target {
let dim = self.theme.muted.apply_to(format!(" ({})", target.posix()));
line.push_str(&dim.to_string());
}
if let Some(d) = f.duration {
let secs = d.as_secs_f64();
let dim = self.theme.muted.apply_to(format!(" ({:.1}s)", secs));
line.push_str(&dim.to_string());
}
self.write_line(w, depth, &line);
}
}
#[cfg(test)]
mod tests {
use std::sync::{Arc, Mutex};
use super::super::StringSink;
use super::*;
use crate::output::Theme;
use crate::output::strip_ansi;
fn capture() -> (Renderer, StringSink, Arc<Mutex<String>>) {
let buf = Arc::new(Mutex::new(String::new()));
let sink = StringSink(buf.clone());
let r = Renderer::new(Theme::default(), Verbosity::Normal);
(r, sink, buf)
}
#[test]
fn ok_status_renders_check_glyph() {
let (r, sink, buf) = capture();
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Ok,
subject: "done",
detail: None,
duration: None,
target: None,
},
);
let out = strip_ansi(&buf.lock().unwrap());
assert!(out.contains("✓ done"), "got: {out:?}");
}
#[test]
fn info_role_has_no_icon() {
let (r, sink, buf) = capture();
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Info,
subject: "note",
detail: None,
duration: None,
target: None,
},
);
let out = strip_ansi(&buf.lock().unwrap());
assert_eq!(out.trim_end(), "note");
}
#[test]
fn detail_appended_with_em_dash() {
let (r, sink, buf) = capture();
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Fail,
subject: "/tmp/foo",
detail: Some("permission denied"),
duration: None,
target: None,
},
);
let out = strip_ansi(&buf.lock().unwrap());
assert!(
out.contains("✗ /tmp/foo — permission denied"),
"got: {out:?}"
);
}
#[test]
fn duration_trailed_in_parens() {
let (r, sink, buf) = capture();
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Ok,
subject: "done",
detail: None,
duration: Some(std::time::Duration::from_millis(1234)),
target: None,
},
);
let out = strip_ansi(&buf.lock().unwrap());
assert!(out.contains("(1.2s)"), "got: {out:?}");
}
#[test]
fn fail_shown_even_at_quiet() {
let buf = Arc::new(Mutex::new(String::new()));
let sink = StringSink(buf.clone());
let r = Renderer::new(Theme::default(), Verbosity::Quiet);
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Fail,
subject: "boom",
detail: None,
duration: None,
target: None,
},
);
let out = strip_ansi(&buf.lock().unwrap());
assert!(
out.contains("boom"),
"Fail must render at Quiet; got: {out:?}"
);
}
#[test]
fn ok_suppressed_at_quiet() {
let buf = Arc::new(Mutex::new(String::new()));
let sink = StringSink(buf.clone());
let r = Renderer::new(Theme::default(), Verbosity::Quiet);
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Ok,
subject: "done",
detail: None,
duration: None,
target: None,
},
);
assert!(buf.lock().unwrap().is_empty());
}
#[test]
fn detail_strips_ansi_to_prevent_terminal_paint() {
let (r, sink, buf) = capture();
let detail = "upstream: \x1b[31mred\x1b[0m text \x1b[1mbold\x1b[0m";
r.render_status(
&sink,
0,
&StatusFields {
role: Role::Fail,
subject: "sync failed",
detail: Some(detail),
duration: None,
target: None,
},
);
let raw = buf.lock().unwrap().clone();
let visible = strip_ansi(&raw);
assert!(
visible.contains("sync failed — upstream: red text bold"),
"visible composition mismatch; got: {visible:?}"
);
// The renderer's own SGR styles the Fail glyph + subject (bold red),
// so a blanket `!raw.contains("\\x1b[")` is too strict. Pick a SGR
// code the renderer would never emit for Fail (foreground red `31`)
// to prove the detail's escapes were sanitized away.
assert!(
!raw.contains("\x1b[31m"),
"detail's red SGR must be stripped before push_str; got raw: {raw:?}"
);
// And the stray `\x1b[0m` mid-detail must not survive — it would
// otherwise close the renderer's subject styling prematurely.
let detail_segment = raw.rsplit(" — ").next().unwrap_or("");
assert!(
!detail_segment.contains('\u{1b}'),
"detail segment must contain no ANSI escapes; got: {detail_segment:?}"
);
}
}