bullet_stream 0.11.0

Bulletproof printing for bullet point text
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use crate::ansi_escape::ANSI;
use crate::background_printer::PrintGuard;
use crate::util::{
    format_stream_writer, mpsc_stream_to_output, prefix_first_rest_lines, prefix_lines,
    ParagraphInspectWrite, TrailingParagraph, TrailingParagraphSend,
};
use crate::{ansi_escape, background_printer, duration_format, state, style, Print};
use std::fmt::{Debug, Formatter};
use std::io::{self, Write};
use std::mem;
use std::sync::Arc;
use std::time::Instant;

pub(crate) fn h1<W: TrailingParagraph>(writer: &mut W, s: impl AsRef<str>) {
    if !writer.trailing_paragraph() {
        writeln!(writer).expect("writer open");
    }

    writeln!(
        writer,
        "{}",
        ansi_escape::wrap_ansi_escape_each_line(
            &ANSI::BoldPurple,
            format!("# {}", s.as_ref().trim()),
        ),
    )
    .expect("writer open");

    if !writer.trailing_paragraph() {
        writeln!(writer).expect("writer open");
    }
    writer.flush().expect("writer open");
}

pub(crate) fn h2<W: TrailingParagraph>(writer: &mut W, s: impl AsRef<str>) {
    if !writer.trailing_paragraph() {
        writeln!(writer).expect("writer open");
    }

    writeln!(
        writer,
        "{}",
        ansi_escape::wrap_ansi_escape_each_line(
            &ANSI::BoldPurple,
            format!("## {}", s.as_ref().trim()),
        ),
    )
    .expect("writer open");

    if !writer.trailing_paragraph() {
        writeln!(writer).expect("writer open");
    }
    writer.flush().expect("writer open");
}

pub(crate) fn h3<W: TrailingParagraph>(writer: &mut W, s: impl AsRef<str>) {
    if !writer.trailing_paragraph() {
        writeln!(writer).expect("writer open");
    }

    writeln!(writer, "### {}", s.as_ref().trim()).expect("writer open");

    if !writer.trailing_paragraph() {
        writeln!(writer).expect("writer open");
    }
    writer.flush().expect("writer open");
}

pub(crate) fn bullet<W: Write>(writer: &mut W, s: impl AsRef<str>) {
    writeln!(
        writer,
        "{}",
        prefix_first_rest_lines("- ", "  ", s.as_ref().trim())
    )
    .expect("writer open");
    writer.flush().expect("writer open");
}

pub(crate) fn plain<W: Write>(writer: &mut W, s: impl AsRef<str>) {
    writeln!(writer, "{}", s.as_ref().trim_end()).expect("writer open");
    writer.flush().expect("writer open");
}

pub(crate) fn sub_bullet<W: Write>(writer: &mut W, s: impl AsRef<str>) {
    writeln!(writer, "{}", sub_bullet_prefix(s)).expect("writer open");
    writer.flush().expect("writer open");
}

pub(crate) fn sub_bullet_prefix(s: impl AsRef<str>) -> String {
    prefix_first_rest_lines("  - ", "    ", s.as_ref().trim())
}

#[cfg(feature = "fun_run")]
pub(crate) fn sub_stream_cmd<W: TrailingParagraphSend>(
    writer: &mut W,
    mut command: impl fun_run::CommandWithName,
) -> Result<fun_run::NamedOutput, fun_run::CmdError> {
    sub_stream_with(
        writer,
        crate::style::running_command(command.name()),
        |stdout, stderr| command.stream_output(stdout, stderr),
    )
}

#[cfg(feature = "fun_run")]
pub fn sub_time_cmd<W>(
    writer: ParagraphInspectWrite<W>,
    mut command: impl fun_run::CommandWithName,
) -> Result<fun_run::NamedOutput, fun_run::CmdError>
where
    W: Write + Send + Sync + 'static,
{
    let timer = sub_start_timer(
        writer,
        Instant::now(),
        style::running_command(command.name()),
    );
    let output = command.named_output();
    timer.done();
    output
}

pub(crate) fn sub_stream_with<W, T, F>(writer: &mut W, s: impl AsRef<str>, mut f: F) -> T
where
    W: TrailingParagraphSend,
    F: FnMut(Box<dyn Write + Send + Sync>, Box<dyn Write + Send + Sync>) -> T,
    T: 'static,
{
    sub_bullet(writer, s);
    writeln!(writer).expect("writer open");

    let duration = Instant::now();
    mpsc_stream_to_output(
        |sender| {
            f(
                // The Senders are boxed to hide the types from the caller so it can be changed
                // in the future. They only need to know they have a `Write + Send + Sync` type.
                Box::new(format_stream_writer(sender.clone())),
                Box::new(format_stream_writer(sender.clone())),
            )
        },
        move |recv| {
            // When it receives input, it writes it to the current `Write` value.
            //
            // When the senders close their channel this loop will exit
            for message in recv {
                writer.write_all(&message).expect("Writer to not be closed");
            }

            if !writer.trailing_paragraph() {
                writeln!(writer).expect("Writer to not be closed");
            }

            sub_bullet(
                writer,
                format!(
                    "Done {}",
                    style::details(duration_format::human(&duration.elapsed()))
                ),
            )
        },
    )
}

pub(crate) fn sub_start_timer<W>(
    writer: ParagraphInspectWrite<W>,
    started: Instant,
    s: impl AsRef<str>,
) -> Print<state::Background<W>>
where
    W: Write + Send + Sync + 'static,
{
    let guard = sub_start_print_interval(writer, s);

    Print {
        started: Some(started),
        state: state::Background {
            started: Instant::now(),
            write: guard,
        },
    }
}

pub(crate) fn sub_start_print_interval<W: Write + Send + Sync + 'static>(
    mut writer: W,
    s: impl AsRef<str>,
) -> PrintGuard<W> {
    // Do not emit a newline after the message
    write!(&mut writer, "{}", sub_bullet_prefix(s)).expect("writer not to be closed");
    writer.flush().expect("Output error: UI writer closed");

    background_printer::print_interval(
        writer,
        std::time::Duration::from_secs(1),
        ansi_escape::wrap_ansi_escape_each_line(&ANSI::Dim, " ."),
        ansi_escape::wrap_ansi_escape_each_line(&ANSI::Dim, "."),
        ansi_escape::wrap_ansi_escape_each_line(&ANSI::Dim, ". "),
        "(Error)".to_string(),
    )
}

pub(crate) fn all_done<W: Write>(writer: &mut W, started: &Option<Instant>) {
    if let Some(started) = started {
        bullet(
            writer,
            format!(
                "Done (finished in {})",
                duration_format::human(&started.elapsed())
            ),
        );
    } else {
        bullet(writer, "Done");
    }
}

pub(crate) fn write_paragraph<W: TrailingParagraph>(io: &mut W, color: &ANSI, s: impl AsRef<str>) {
    let contents = s.as_ref().trim();

    if !io.trailing_paragraph() {
        writeln!(io).expect("writer open");
    }

    writeln!(
        io,
        "{}",
        ansi_escape::wrap_ansi_escape_each_line(
            color,
            prefix_lines(contents, |_, line| {
                // Avoid adding trailing whitespace to the line, if there was none already.
                // The `\n` case is required since `prefix_lines` uses `str::split_inclusive`,
                // which preserves any trailing newline characters if present.
                if line.is_empty() || line == "\n" {
                    String::from("!")
                } else {
                    String::from("! ")
                }
            }),
        ),
    )
    .expect("writer open");
    writeln!(io).expect("writer open");
    io.flush().expect("writer open");
}

pub(crate) fn warning<W: TrailingParagraph>(writer: &mut W, s: impl AsRef<str>) {
    write_paragraph(writer, &ANSI::Yellow, s);
}

pub(crate) fn error<W: TrailingParagraph>(writer: &mut W, s: impl AsRef<str>) {
    write_paragraph(writer, &ANSI::Red, s);
}

pub(crate) fn important<W: TrailingParagraph>(writer: &mut W, s: impl AsRef<str>) {
    write_paragraph(writer, &ANSI::BoldCyan, s);
}

/// Constructs a writer that buffers written data until given marker byte is encountered and
/// then applies the given mapping function to the data before passing the result to the wrapped
/// writer.
pub fn mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>(
    w: W,
    marker_byte: u8,
    f: F,
) -> MappedWrite<W> {
    MappedWrite::new(w, marker_byte, f)
}

/// Constructs a writer that buffers written data until an ASCII/UTF-8 newline byte (`b'\n'`) is
/// encountered and then applies the given mapping function to the data before passing the result to
/// the wrapped writer.
pub fn line_mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>(
    w: W,
    f: F,
) -> MappedWrite<W> {
    mapped(w, b'\n', f)
}

/// A mapped writer that was created with the [`mapped`] or [`line_mapped`] function.
#[derive(Clone)]
pub struct MappedWrite<W: io::Write> {
    // To support unwrapping the inner `Write` while also implementing `Drop` for final cleanup, we need to wrap the
    // `W` value so we can replace it in memory during unwrap. Without the wrapping `Option` we'd need to have a way
    // to construct a bogus `W` value which would require additional trait bounds for `W`. `Clone` and/or `Default`
    // come to mind. Not only would this clutter the API, but for most values that implement `Write`, `Clone` or
    // `Default` are hard to implement correctly as they most often involve system resources such as file handles.
    //
    // This semantically means that a `MappedWrite` can exist without an inner `Write`, but users of `MappedWrite` can
    // never construct such a `MappedWrite` as it only represents a state that happens during `MappedWrite::unwrap`.
    //
    // See: https://rustwiki.org/en/error-index/#E0509
    inner: Option<W>,
    marker_byte: u8,
    buffer: Vec<u8>,
    mapping_fn: Arc<dyn (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send>,
}

impl<W> MappedWrite<W>
where
    W: io::Write,
{
    fn new<F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>(
        w: W,
        marker_byte: u8,
        f: F,
    ) -> MappedWrite<W> {
        MappedWrite {
            inner: Some(w),
            marker_byte,
            buffer: Vec::new(),
            mapping_fn: Arc::new(f),
        }
    }

    pub fn unwrap(mut self) -> W {
        // See `Drop` implementation. This logic cannot be de-duplicated (i.e. by using unwrap in `Drop`) as we would
        // end up in illegal states.
        if self.inner.is_some() {
            let _result = self.map_and_write_current_buffer();
        }

        if let Some(inner) = self.inner.take() {
            inner
        } else {
            // Since `unwrap` is the only function that will cause `self.inner` to be `None` and `unwrap` itself
            // consumes the `MappedWrite`, we can be sure that this case never happens.
            unreachable!("self.inner will never be None")
        }
    }

    fn map_and_write_current_buffer(&mut self) -> io::Result<()> {
        match self.inner {
            Some(ref mut inner) => inner.write_all(&(self.mapping_fn)(mem::take(&mut self.buffer))),
            None => Ok(()),
        }
    }
}

impl<W: io::Write> io::Write for MappedWrite<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        for byte in buf {
            self.buffer.push(*byte);

            if *byte == self.marker_byte {
                self.map_and_write_current_buffer()?;
            }
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        match self.inner {
            Some(ref mut inner) => inner.flush(),
            None => Ok(()),
        }
    }
}

impl<W: io::Write> Drop for MappedWrite<W> {
    fn drop(&mut self) {
        // Drop implementations must not panic. We intentionally ignore the potential error here.
        let _result = self.map_and_write_current_buffer();
    }
}

impl<W: io::Write + Debug> Debug for MappedWrite<W> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MappedWrite")
            .field("inner", &self.inner)
            .field("marker_byte", &self.marker_byte)
            .field("buffer", &self.buffer)
            .field("mapping_fn", &"Fn()")
            .finish()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{strip_ansi, util::LockedWriter, write::line_mapped};
    use indoc::formatdoc;
    use pretty_assertions::assert_eq;
    use std::process::Command;

    #[test]
    fn plain_ending_newline() {
        let writer = LockedWriter::new(Vec::new());
        let reader = writer.clone();
        let mut writer = ParagraphInspectWrite::new(writer);

        let input = formatdoc! {"
            Accidental newline
        "};

        assert!(input.ends_with("\n"));
        plain(&mut writer, input);
        h2(&mut writer, "Then a header");
        drop(writer);

        assert_eq!(
            formatdoc! {"
                Accidental newline

                ## Then a header

            "},
            strip_ansi(String::from_utf8_lossy(&reader.unwrap()))
        );
    }

    #[test]
    fn test_mapped_write() {
        let mut output = Vec::new();

        let mut input = "foo\nbar\nbaz".as_bytes();
        std::io::copy(
            &mut input,
            &mut line_mapped(&mut output, |line| line.repeat(2)),
        )
        .unwrap();

        assert_eq!(output, "foo\nfoo\nbar\nbar\nbazbaz".as_bytes());
    }

    #[test]
    fn test_stream_cmd() {
        let writer = LockedWriter::new(Vec::new());
        let reader = writer.clone();
        self::sub_stream_cmd(
            &mut ParagraphInspectWrite::new(writer),
            Command::new("bash").arg("-c").arg("echo hello"),
        )
        .unwrap();

        let expected = formatdoc! {"
            - Running `bash -c \"echo hello\"`

                hello

            - Done (< 0.1s)

        "};
        assert_eq!(
            expected
                .trim_start()
                .lines()
                .map(|line| if line.is_empty() {
                    String::new()
                } else {
                    format!("  {line}")
                })
                .collect::<Vec<String>>()
                .join("\n"),
            strip_ansi(String::from_utf8_lossy(&reader.unwrap()))
        )
    }

    #[test]
    fn test_time_cmd() {
        let writer = LockedWriter::new(Vec::new());
        let reader = writer.clone();
        self::sub_time_cmd(
            ParagraphInspectWrite::new(writer),
            Command::new("bash").arg("-c").arg("echo hello"),
        )
        .unwrap();

        let expected = "- Running `bash -c \"echo hello\"` ... (< 0.1s)";
        assert_eq!(
            expected.trim(),
            strip_ansi(String::from_utf8_lossy(&reader.unwrap())).trim()
        )
    }
}