tinyharness-ui 0.5.6

Terminal UI library for TinyHarness: ANSI output, prompts and diff display
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::io::{self, Write};

use crate::style::*;

/// Structured output writer — all terminal I/O goes through this.
///
/// Provides plain output methods (`line`, `blank`, `raw`), styled convenience
/// methods (`success`, `error`, `warning`, `info`, `dim`, `bold`), and
/// implements [`Write`] so the standard `write!`/`writeln!` macros work
/// directly with ANSI style constants.
///
/// # Examples
///
/// ```rust
/// use tinyharness_ui::output::Output;
/// use std::io::Write;
///
/// let mut out = Output::stdout();
/// let _ = writeln!(out, "Hello, world!");
/// out.success("Done.").unwrap();
/// ```
pub struct Output {
    writer: Box<dyn Write + Send>,
}

impl Output {
    /// Create an `Output` that writes to stdout.
    pub fn stdout() -> Self {
        Self {
            writer: Box::new(io::stdout()),
        }
    }

    /// Create an `Output` that writes to stderr.
    pub fn stderr() -> Self {
        Self {
            writer: Box::new(io::stderr()),
        }
    }

    /// Create an `Output` with a custom writer (e.g. a `Vec<u8>` for testing).
    pub fn new(writer: Box<dyn Write + Send>) -> Self {
        Self { writer }
    }

    // ── Plain output ──

    /// Write text followed by a newline.
    ///
    /// Returns `io::Result<()>`; callers may use `let _ = out.line("...");`
    /// to ignore errors when writing to stdout/stderr (where failures are rare
    /// and typically fatal anyway).
    pub fn line(&mut self, text: &str) -> io::Result<()> {
        writeln!(self, "{text}")
    }

    /// Write a blank line (just a newline).
    pub fn blank(&mut self) -> io::Result<()> {
        writeln!(self)
    }

    /// Write raw bytes as-is (no newline appended, no styling applied).
    pub fn raw(&mut self, bytes: &[u8]) -> io::Result<()> {
        self.writer.write_all(bytes)
    }

    // ── Styled single-line messages ──

    /// Write a success message (green) followed by a newline.
    pub fn success(&mut self, msg: &str) -> io::Result<()> {
        writeln!(self, "{GREEN}{msg}{RESET}")
    }

    /// Write an error message (red) followed by a newline.
    pub fn error(&mut self, msg: &str) -> io::Result<()> {
        writeln!(self, "{RED}{msg}{RESET}")
    }

    /// Write a warning message (orange) followed by a newline.
    pub fn warning(&mut self, msg: &str) -> io::Result<()> {
        writeln!(self, "{ORANGE}{msg}{RESET}")
    }

    /// Write an info message (blue) followed by a newline.
    pub fn info(&mut self, msg: &str) -> io::Result<()> {
        writeln!(self, "{BLUE}{msg}{RESET}")
    }

    /// Write a dim/gray message followed by a newline.
    pub fn dim(&mut self, msg: &str) -> io::Result<()> {
        writeln!(self, "{GRAY}{msg}{RESET}")
    }

    /// Write a bold message followed by a newline.
    pub fn bold(&mut self, msg: &str) -> io::Result<()> {
        writeln!(self, "{BOLD}{msg}{RESET}")
    }

    /// Write a line with custom prefix, text, and suffix, followed by a newline.
    ///
    /// Typically used with style constants:
    /// ```ignore
    /// out.styled_line(BOLD, "Header:", RESET);
    /// ```
    pub fn styled_line(&mut self, prefix: &str, text: &str, suffix: &str) -> io::Result<()> {
        writeln!(self, "{prefix}{text}{suffix}")
    }
}

impl Write for Output {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.writer.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.writer.flush()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use std::sync::{Arc, Mutex};

    /// A writer that captures output into a shared `Vec<u8>` so tests can
    /// inspect what was written.
    #[derive(Clone)]
    struct TestWriter {
        buf: Arc<Mutex<Vec<u8>>>,
    }

    impl TestWriter {
        fn new() -> (Self, Arc<Mutex<Vec<u8>>>) {
            let buf = Arc::new(Mutex::new(Vec::new()));
            let writer = TestWriter { buf: buf.clone() };
            (writer, buf)
        }
    }

    impl Write for TestWriter {
        fn write(&mut self, data: &[u8]) -> io::Result<usize> {
            self.buf.lock().unwrap().extend_from_slice(data);
            Ok(data.len())
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    /// Helper: create an `Output` and the shared buffer it writes into.
    fn captured_output() -> (Output, Arc<Mutex<Vec<u8>>>) {
        let (writer, buf) = TestWriter::new();
        let output = Output::new(Box::new(writer));
        (output, buf)
    }

    /// Get captured output as a String (both raw bytes and ANSI-stripped).
    fn captured_string(buf: &Arc<Mutex<Vec<u8>>>) -> String {
        String::from_utf8(buf.lock().unwrap().clone()).unwrap()
    }

    /// Strip ANSI SGR sequences from a string for content assertions.
    fn strip_ansi(s: &str) -> String {
        let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
        re.replace_all(s, "").to_string()
    }

    // ── Plain output tests ──

    #[test]
    fn line_writes_text_with_newline() {
        let (mut out, buf) = captured_output();
        out.line("hello").unwrap();
        let raw = captured_string(&buf);
        assert_eq!(raw, "hello\n");
    }

    #[test]
    fn line_empty_string() {
        let (mut out, buf) = captured_output();
        out.line("").unwrap();
        assert_eq!(captured_string(&buf), "\n");
    }

    #[test]
    fn blank_writes_newline() {
        let (mut out, buf) = captured_output();
        out.blank().unwrap();
        assert_eq!(captured_string(&buf), "\n");
    }

    #[test]
    fn raw_writes_bytes_exactly() {
        let (mut out, buf) = captured_output();
        out.raw(b"no newline").unwrap();
        assert_eq!(captured_string(&buf), "no newline");
    }

    #[test]
    fn raw_empty_bytes() {
        let (mut out, buf) = captured_output();
        out.raw(b"").unwrap();
        assert!(captured_string(&buf).is_empty());
    }

    #[test]
    fn raw_no_newline_appended() {
        let (mut out, buf) = captured_output();
        out.raw(b"abc").unwrap();
        // Should end exactly with 'c', no trailing newline
        assert!(!captured_string(&buf).ends_with('\n'));
    }

    // ── Styled method tests ──

    #[test]
    fn success_wraps_in_green() {
        let (mut out, buf) = captured_output();
        out.success("OK").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(GREEN), "should contain GREEN ANSI code");
        assert!(raw.contains(RESET), "should contain RESET");
        assert_eq!(strip_ansi(&raw), "OK\n");
    }

    #[test]
    fn error_wraps_in_red() {
        let (mut out, buf) = captured_output();
        out.error("FAIL").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(RED), "should contain RED ANSI code");
        assert!(raw.contains(RESET), "should contain RESET");
        assert_eq!(strip_ansi(&raw), "FAIL\n");
    }

    #[test]
    fn warning_wraps_in_orange() {
        let (mut out, buf) = captured_output();
        out.warning("careful").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(ORANGE), "should contain ORANGE ANSI code");
        assert!(raw.contains(RESET), "should contain RESET");
        assert_eq!(strip_ansi(&raw), "careful\n");
    }

    #[test]
    fn info_wraps_in_blue() {
        let (mut out, buf) = captured_output();
        out.info("note").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(BLUE), "should contain BLUE ANSI code");
        assert!(raw.contains(RESET), "should contain RESET");
        assert_eq!(strip_ansi(&raw), "note\n");
    }

    #[test]
    fn dim_wraps_in_gray() {
        let (mut out, buf) = captured_output();
        out.dim("subtle").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(GRAY), "should contain GRAY ANSI code");
        assert!(raw.contains(RESET), "should contain RESET");
        assert_eq!(strip_ansi(&raw), "subtle\n");
    }

    #[test]
    fn bold_wraps_in_bold() {
        let (mut out, buf) = captured_output();
        out.bold("loud").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(BOLD), "should contain BOLD ANSI code");
        assert!(raw.contains(RESET), "should contain RESET");
        assert_eq!(strip_ansi(&raw), "loud\n");
    }

    #[test]
    fn styled_line_custom_prefix_and_suffix() {
        let (mut out, buf) = captured_output();
        out.styled_line("[", "body", "]").unwrap();
        assert_eq!(captured_string(&buf), "[body]\n");
    }

    #[test]
    fn styled_line_with_ansi_codes() {
        let (mut out, buf) = captured_output();
        out.styled_line(BOLD, "Header", RESET).unwrap();
        let raw = captured_string(&buf);
        assert!(raw.starts_with(BOLD));
        assert!(raw.trim_end().ends_with(RESET));
        assert_eq!(strip_ansi(&raw), "Header\n");
    }

    // ── Write trait tests ──

    #[test]
    fn writeln_macro_works() {
        let (mut out, buf) = captured_output();
        let _ = writeln!(out, "formatted {}", 42);
        assert_eq!(captured_string(&buf), "formatted 42\n");
    }

    #[test]
    fn write_macro_no_newline() {
        let (mut out, buf) = captured_output();
        let _ = write!(out, "no newline");
        assert_eq!(captured_string(&buf), "no newline");
    }

    #[test]
    fn writeln_with_ansi_constants() {
        let (mut out, buf) = captured_output();
        let _ = writeln!(out, "{GREEN}ok{RESET}");
        let raw = captured_string(&buf);
        assert!(raw.contains(GREEN));
        assert!(raw.contains(RESET));
        assert_eq!(strip_ansi(&raw), "ok\n");
    }

    #[test]
    fn write_then_flush() {
        let (mut out, buf) = captured_output();
        let _ = write!(out, "partial");
        // flush should succeed (it's a no-op on our TestWriter)
        assert!(out.flush().is_ok());
        assert_eq!(captured_string(&buf), "partial");
    }

    #[test]
    fn writeln_empty() {
        let (mut out, buf) = captured_output();
        let _ = writeln!(out);
        assert_eq!(captured_string(&buf), "\n");
    }

    // ── Chaining / multiple writes ──

    #[test]
    fn multiple_operations_append() {
        let (mut out, buf) = captured_output();
        out.line("first").unwrap();
        out.success("ok").unwrap();
        let _ = writeln!(out, "{BOLD}bold{RESET}");
        let raw = captured_string(&buf);
        let plain = strip_ansi(&raw);
        assert_eq!(plain, "first\nok\nbold\n");
    }

    #[test]
    fn blank_between_lines() {
        let (mut out, buf) = captured_output();
        out.line("a").unwrap();
        out.blank().unwrap();
        out.line("b").unwrap();
        assert_eq!(captured_string(&buf), "a\n\nb\n");
    }

    // ── Edge cases ──

    #[test]
    fn success_empty_message() {
        let (mut out, buf) = captured_output();
        out.success("").unwrap();
        let raw = captured_string(&buf);
        assert!(raw.contains(GREEN));
        assert!(raw.contains(RESET));
        assert_eq!(strip_ansi(&raw), "\n");
    }

    #[test]
    fn unicode_text() {
        let (mut out, buf) = captured_output();
        out.line("zażółć gęślą jaźń 🦀").unwrap();
        let raw = captured_string(&buf);
        assert_eq!(raw, "zażółć gęślą jaźń 🦀\n");
    }

    #[test]
    fn long_text_no_panic() {
        let (mut out, buf) = captured_output();
        let long = "x".repeat(10_000);
        out.line(&long).unwrap();
        assert_eq!(captured_string(&buf).len(), 10_001); // + newline
    }

    #[test]
    fn stdout_constructs() {
        // Just verify the constructor doesn't panic.
        let _out = Output::stdout();
    }

    #[test]
    fn stderr_constructs() {
        // Just verify the constructor doesn't panic.
        let _out = Output::stderr();
    }

    #[test]
    fn new_with_boxed_writer() {
        // Use new() with a plain Vec<u8> — ensures the constructor accepts any Write + Send.
        let buf = Box::new(Vec::new());
        let mut out = Output::new(buf);
        out.line("test").unwrap();
    }
}