1use std::io::{self, Write};
2
3use crate::style::*;
4
5pub struct Output {
23 writer: Box<dyn Write + Send>,
24}
25
26impl Output {
27 pub fn stdout() -> Self {
29 Self {
30 writer: Box::new(io::stdout()),
31 }
32 }
33
34 pub fn stderr() -> Self {
36 Self {
37 writer: Box::new(io::stderr()),
38 }
39 }
40
41 pub fn new(writer: Box<dyn Write + Send>) -> Self {
43 Self { writer }
44 }
45
46 pub fn line(&mut self, text: &str) -> io::Result<()> {
54 writeln!(self, "{text}")
55 }
56
57 pub fn blank(&mut self) -> io::Result<()> {
59 writeln!(self)
60 }
61
62 pub fn raw(&mut self, bytes: &[u8]) -> io::Result<()> {
64 self.writer.write_all(bytes)
65 }
66
67 pub fn success(&mut self, msg: &str) -> io::Result<()> {
71 writeln!(self, "{GREEN}{msg}{RESET}")
72 }
73
74 pub fn error(&mut self, msg: &str) -> io::Result<()> {
76 writeln!(self, "{RED}{msg}{RESET}")
77 }
78
79 pub fn warning(&mut self, msg: &str) -> io::Result<()> {
81 writeln!(self, "{ORANGE}{msg}{RESET}")
82 }
83
84 pub fn info(&mut self, msg: &str) -> io::Result<()> {
86 writeln!(self, "{BLUE}{msg}{RESET}")
87 }
88
89 pub fn dim(&mut self, msg: &str) -> io::Result<()> {
91 writeln!(self, "{GRAY}{msg}{RESET}")
92 }
93
94 pub fn bold(&mut self, msg: &str) -> io::Result<()> {
96 writeln!(self, "{BOLD}{msg}{RESET}")
97 }
98
99 pub fn styled_line(&mut self, prefix: &str, text: &str, suffix: &str) -> io::Result<()> {
106 writeln!(self, "{prefix}{text}{suffix}")
107 }
108}
109
110impl Write for Output {
111 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
112 self.writer.write(buf)
113 }
114
115 fn flush(&mut self) -> io::Result<()> {
116 self.writer.flush()
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123 use std::io::Write;
124 use std::sync::{Arc, Mutex};
125
126 #[derive(Clone)]
129 struct TestWriter {
130 buf: Arc<Mutex<Vec<u8>>>,
131 }
132
133 impl TestWriter {
134 fn new() -> (Self, Arc<Mutex<Vec<u8>>>) {
135 let buf = Arc::new(Mutex::new(Vec::new()));
136 let writer = TestWriter { buf: buf.clone() };
137 (writer, buf)
138 }
139 }
140
141 impl Write for TestWriter {
142 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
143 self.buf.lock().unwrap().extend_from_slice(data);
144 Ok(data.len())
145 }
146
147 fn flush(&mut self) -> io::Result<()> {
148 Ok(())
149 }
150 }
151
152 fn captured_output() -> (Output, Arc<Mutex<Vec<u8>>>) {
154 let (writer, buf) = TestWriter::new();
155 let output = Output::new(Box::new(writer));
156 (output, buf)
157 }
158
159 fn captured_string(buf: &Arc<Mutex<Vec<u8>>>) -> String {
161 String::from_utf8(buf.lock().unwrap().clone()).unwrap()
162 }
163
164 fn strip_ansi(s: &str) -> String {
166 let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
167 re.replace_all(s, "").to_string()
168 }
169
170 #[test]
173 fn line_writes_text_with_newline() {
174 let (mut out, buf) = captured_output();
175 out.line("hello").unwrap();
176 let raw = captured_string(&buf);
177 assert_eq!(raw, "hello\n");
178 }
179
180 #[test]
181 fn line_empty_string() {
182 let (mut out, buf) = captured_output();
183 out.line("").unwrap();
184 assert_eq!(captured_string(&buf), "\n");
185 }
186
187 #[test]
188 fn blank_writes_newline() {
189 let (mut out, buf) = captured_output();
190 out.blank().unwrap();
191 assert_eq!(captured_string(&buf), "\n");
192 }
193
194 #[test]
195 fn raw_writes_bytes_exactly() {
196 let (mut out, buf) = captured_output();
197 out.raw(b"no newline").unwrap();
198 assert_eq!(captured_string(&buf), "no newline");
199 }
200
201 #[test]
202 fn raw_empty_bytes() {
203 let (mut out, buf) = captured_output();
204 out.raw(b"").unwrap();
205 assert!(captured_string(&buf).is_empty());
206 }
207
208 #[test]
209 fn raw_no_newline_appended() {
210 let (mut out, buf) = captured_output();
211 out.raw(b"abc").unwrap();
212 assert!(!captured_string(&buf).ends_with('\n'));
214 }
215
216 #[test]
219 fn success_wraps_in_green() {
220 let (mut out, buf) = captured_output();
221 out.success("OK").unwrap();
222 let raw = captured_string(&buf);
223 assert!(raw.contains(GREEN), "should contain GREEN ANSI code");
224 assert!(raw.contains(RESET), "should contain RESET");
225 assert_eq!(strip_ansi(&raw), "OK\n");
226 }
227
228 #[test]
229 fn error_wraps_in_red() {
230 let (mut out, buf) = captured_output();
231 out.error("FAIL").unwrap();
232 let raw = captured_string(&buf);
233 assert!(raw.contains(RED), "should contain RED ANSI code");
234 assert!(raw.contains(RESET), "should contain RESET");
235 assert_eq!(strip_ansi(&raw), "FAIL\n");
236 }
237
238 #[test]
239 fn warning_wraps_in_orange() {
240 let (mut out, buf) = captured_output();
241 out.warning("careful").unwrap();
242 let raw = captured_string(&buf);
243 assert!(raw.contains(ORANGE), "should contain ORANGE ANSI code");
244 assert!(raw.contains(RESET), "should contain RESET");
245 assert_eq!(strip_ansi(&raw), "careful\n");
246 }
247
248 #[test]
249 fn info_wraps_in_blue() {
250 let (mut out, buf) = captured_output();
251 out.info("note").unwrap();
252 let raw = captured_string(&buf);
253 assert!(raw.contains(BLUE), "should contain BLUE ANSI code");
254 assert!(raw.contains(RESET), "should contain RESET");
255 assert_eq!(strip_ansi(&raw), "note\n");
256 }
257
258 #[test]
259 fn dim_wraps_in_gray() {
260 let (mut out, buf) = captured_output();
261 out.dim("subtle").unwrap();
262 let raw = captured_string(&buf);
263 assert!(raw.contains(GRAY), "should contain GRAY ANSI code");
264 assert!(raw.contains(RESET), "should contain RESET");
265 assert_eq!(strip_ansi(&raw), "subtle\n");
266 }
267
268 #[test]
269 fn bold_wraps_in_bold() {
270 let (mut out, buf) = captured_output();
271 out.bold("loud").unwrap();
272 let raw = captured_string(&buf);
273 assert!(raw.contains(BOLD), "should contain BOLD ANSI code");
274 assert!(raw.contains(RESET), "should contain RESET");
275 assert_eq!(strip_ansi(&raw), "loud\n");
276 }
277
278 #[test]
279 fn styled_line_custom_prefix_and_suffix() {
280 let (mut out, buf) = captured_output();
281 out.styled_line("[", "body", "]").unwrap();
282 assert_eq!(captured_string(&buf), "[body]\n");
283 }
284
285 #[test]
286 fn styled_line_with_ansi_codes() {
287 let (mut out, buf) = captured_output();
288 out.styled_line(BOLD, "Header", RESET).unwrap();
289 let raw = captured_string(&buf);
290 assert!(raw.starts_with(BOLD));
291 assert!(raw.trim_end().ends_with(RESET));
292 assert_eq!(strip_ansi(&raw), "Header\n");
293 }
294
295 #[test]
298 fn writeln_macro_works() {
299 let (mut out, buf) = captured_output();
300 let _ = writeln!(out, "formatted {}", 42);
301 assert_eq!(captured_string(&buf), "formatted 42\n");
302 }
303
304 #[test]
305 fn write_macro_no_newline() {
306 let (mut out, buf) = captured_output();
307 let _ = write!(out, "no newline");
308 assert_eq!(captured_string(&buf), "no newline");
309 }
310
311 #[test]
312 fn writeln_with_ansi_constants() {
313 let (mut out, buf) = captured_output();
314 let _ = writeln!(out, "{GREEN}ok{RESET}");
315 let raw = captured_string(&buf);
316 assert!(raw.contains(GREEN));
317 assert!(raw.contains(RESET));
318 assert_eq!(strip_ansi(&raw), "ok\n");
319 }
320
321 #[test]
322 fn write_then_flush() {
323 let (mut out, buf) = captured_output();
324 let _ = write!(out, "partial");
325 assert!(out.flush().is_ok());
327 assert_eq!(captured_string(&buf), "partial");
328 }
329
330 #[test]
331 fn writeln_empty() {
332 let (mut out, buf) = captured_output();
333 let _ = writeln!(out);
334 assert_eq!(captured_string(&buf), "\n");
335 }
336
337 #[test]
340 fn multiple_operations_append() {
341 let (mut out, buf) = captured_output();
342 out.line("first").unwrap();
343 out.success("ok").unwrap();
344 let _ = writeln!(out, "{BOLD}bold{RESET}");
345 let raw = captured_string(&buf);
346 let plain = strip_ansi(&raw);
347 assert_eq!(plain, "first\nok\nbold\n");
348 }
349
350 #[test]
351 fn blank_between_lines() {
352 let (mut out, buf) = captured_output();
353 out.line("a").unwrap();
354 out.blank().unwrap();
355 out.line("b").unwrap();
356 assert_eq!(captured_string(&buf), "a\n\nb\n");
357 }
358
359 #[test]
362 fn success_empty_message() {
363 let (mut out, buf) = captured_output();
364 out.success("").unwrap();
365 let raw = captured_string(&buf);
366 assert!(raw.contains(GREEN));
367 assert!(raw.contains(RESET));
368 assert_eq!(strip_ansi(&raw), "\n");
369 }
370
371 #[test]
372 fn unicode_text() {
373 let (mut out, buf) = captured_output();
374 out.line("zażółć gęślą jaźń 🦀").unwrap();
375 let raw = captured_string(&buf);
376 assert_eq!(raw, "zażółć gęślą jaźń 🦀\n");
377 }
378
379 #[test]
380 fn long_text_no_panic() {
381 let (mut out, buf) = captured_output();
382 let long = "x".repeat(10_000);
383 out.line(&long).unwrap();
384 assert_eq!(captured_string(&buf).len(), 10_001); }
386
387 #[test]
388 fn stdout_constructs() {
389 let _out = Output::stdout();
391 }
392
393 #[test]
394 fn stderr_constructs() {
395 let _out = Output::stderr();
397 }
398
399 #[test]
400 fn new_with_boxed_writer() {
401 let buf = Box::new(Vec::new());
403 let mut out = Output::new(buf);
404 out.line("test").unwrap();
405 }
406}