tinsel 1.0.1

Clean terminal output with a little sparkle: live step lists, spinners, tables, trees, prompts and full-screen apps.
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
475
476
477
478
479
480
481
482
483
484
485
//! One-line messages, headings, callouts and error reports.

use std::fmt;

use crate::block::Block;
use crate::output::{self, Val};
use crate::style::palette::{AMBER, BLUSH, LINE, MUTED, RED, ROSE, SAGE, SOFT, TEXT};
use crate::style::{Rgb, bold, paint};
use crate::term::content_width;
use crate::text;

/// The kind of a one-line message. The free functions ([`success`], [`info`], …) print
/// straight away; use a `Tone` when you want the line as a string instead.
///
/// ```
/// use tinsel::Tone;
///
/// let line = Tone::Success.line("Deployed my-app");
/// assert!(tinsel::text::strip_ansi(&line).contains("Deployed my-app"));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Tone {
    /// `●` in rose: something finished.
    Success,
    /// `›` in blush: something worth knowing.
    Info,
    /// `▲` in amber: something to look at.
    Warn,
    /// `✕` in red: something went wrong.
    Error,
    /// `→` in muted grey: what to do next.
    Hint,
}

impl Tone {
    /// The message as a styled line, without printing it.
    pub fn line(self, msg: &str) -> String {
        let (glyph, color, text_color): (&str, Rgb, Rgb) = match self {
            Tone::Success => ("●", ROSE, TEXT),
            Tone::Info => ("›", BLUSH, TEXT),
            Tone::Warn => ("▲", AMBER, TEXT),
            Tone::Error => ("✕", RED, TEXT),
            Tone::Hint => ("→", MUTED, SOFT),
        };
        format!("  {}  {}", paint(color, glyph), paint(text_color, msg))
    }

    /// Print the message: errors to stderr, everything else to stdout (or the
    /// [`Target`](crate::Target)). In
    /// [`Output::Quiet`](crate::Output::Quiet) only warnings and errors print.
    pub fn print(self, msg: &str) {
        if output::json() {
            return output::emit("message", &[("level", Val::Str(self.name())), ("text", Val::Str(msg))]);
        }
        if output::quiet() && !matches!(self, Tone::Warn | Tone::Error) {
            return;
        }
        let line = self.line(msg);
        let stderr = self == Tone::Error;
        if crate::display::print_above(&[&line], stderr) {
            return;
        }
        if stderr {
            output::err_line(&line);
        } else {
            output::line(&line);
        }
    }
}

impl Tone {
    fn name(self) -> &'static str {
        match self {
            Tone::Success => "success",
            Tone::Info => "info",
            Tone::Warn => "warn",
            Tone::Error => "error",
            Tone::Hint => "hint",
        }
    }
}

/// Prints `●  msg`: something finished.
pub fn success(msg: &str) {
    Tone::Success.print(msg);
}

/// Prints `›  msg`: something worth knowing.
pub fn info(msg: &str) {
    Tone::Info.print(msg);
}

/// Prints `▲  msg`: something to look at.
pub fn warn(msg: &str) {
    Tone::Warn.print(msg);
}

/// Prints `✕  msg` to stderr: something went wrong.
pub fn error(msg: &str) {
    Tone::Error.print(msg);
}

/// Prints `→  msg`, quieter than the rest: what to do next.
pub fn hint(msg: &str) {
    Tone::Hint.print(msg);
}

/// Prints `2/5  Building`, for scripts that announce steps as they go rather than drawing a
/// live list.
pub fn counter(n: usize, total: usize, msg: &str) {
    if output::json() {
        let fields = [
            ("n", Val::Num(n as f64)),
            ("total", Val::Num(total as f64)),
            ("text", Val::Str(msg)),
        ];
        return output::emit("counter", &fields);
    }
    if output::quiet() {
        return;
    }
    let w = total.to_string().len();
    let line = format!("  {}  {}", paint(MUTED, &format!("{n:>w$}/{total}")), paint(TEXT, msg));
    if !crate::display::print_above(&[&line], false) {
        output::line(&line);
    }
}

/// Prints a heading with an optional quieter subtitle beside it, with a blank line either side.
pub fn title(text: &str, subtitle: Option<&str>) {
    if output::json() {
        let sub = subtitle.unwrap_or_default();
        return output::emit("title", &[("text", Val::Str(text)), ("subtitle", Val::Str(sub))]);
    }
    if output::quiet() {
        return;
    }
    let sub = subtitle.map(|s| format!("  {}", paint(SOFT, s))).unwrap_or_default();
    let heading = format!("\n  {}{sub}\n", bold(&paint(ROSE, text)));
    if !crate::display::print_above(&[&heading], false) {
        output::line(&heading);
    }
}

/// A thin divider across the content width, with an optional label in it.
///
/// ```
/// let line = tinsel::rule("build");
/// assert!(tinsel::text::strip_ansi(&line).starts_with("  ── build ──"));
/// ```
pub fn rule(label: &str) -> String {
    let w = content_width().saturating_sub(2);
    if label.is_empty() {
        return format!("  {}", paint(LINE, &"─".repeat(w)));
    }
    let rest = w.saturating_sub(text::width(label) + 4);
    format!(
        "  {} {} {}",
        paint(LINE, "──"),
        paint(SOFT, label),
        paint(LINE, &"─".repeat(rest))
    )
}

/// The tone of a [`callout`] block, which sets its colour.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Callout {
    /// Rose: worth knowing.
    Note,
    /// Sage: a helpful suggestion.
    Tip,
    /// Amber: be careful.
    Warning,
    /// Red: something destructive or broken.
    Danger,
}

impl Callout {
    fn color(self) -> Rgb {
        match self {
            Callout::Note => ROSE,
            Callout::Tip => SAGE,
            Callout::Warning => AMBER,
            Callout::Danger => RED,
        }
    }
}

/// A block set off by a coloured bar on the left, with a title and a body wrapped to fit.
///
/// ```
/// use tinsel::{Callout, callout};
///
/// callout(Callout::Tip, "Faster rebuilds", "Pass --incremental to reuse the last build.").print();
/// ```
pub fn callout(kind: Callout, title: &str, body: &str) -> Block {
    let bar = paint(kind.color(), "▎");
    let mut out = Block::new();
    out.push(format!("  {bar} {}", bold(&paint(kind.color(), title))));
    for line in text::wrap(body, content_width().saturating_sub(6)) {
        out.push(format!("  {bar} {}", paint(SOFT, &line)));
    }
    out
}

/// An error, what caused it (outermost first), and a hint at the fix.
///
/// ```
/// use tinsel::Report;
///
/// let report = Report::new("couldn't deploy my-app")
///     .cause("AccessDenied: iam:PassRole")
///     .cause("the role has no trust policy for Lambda")
///     .hint("attach one with `my-app role fix`");
/// report.eprint();
/// assert!(report.lines().plain().contains("╰─ AccessDenied"));
/// ```
///
/// ```text
///   ✕  couldn't deploy my-app
///      ╰─ AccessDenied: iam:PassRole
///         ╰─ the role has no trust policy for Lambda
///   →  attach one with `my-app role fix`
/// ```
#[derive(Clone, Debug, Default)]
pub struct Report {
    title: String,
    causes: Vec<String>,
    code: Option<crate::layout::Code>,
    hint: Option<String>,
}

impl Report {
    /// A report headed by what went wrong.
    pub fn new(title: impl Into<String>) -> Self {
        Report {
            title: title.into(),
            ..Self::default()
        }
    }

    /// A report for any error, with its whole `source()` chain as causes.
    ///
    /// ```
    /// let err = std::io::Error::other("disk full");
    /// let report = tinsel::Report::from_error("couldn't save", &err);
    /// assert!(report.lines().plain().contains("disk full"));
    /// ```
    pub fn from_error(title: impl Into<String>, err: &dyn std::error::Error) -> Self {
        let mut report = Report::new(title).cause(err.to_string());
        let mut source = err.source();
        while let Some(e) = source {
            report = report.cause(e.to_string());
            source = e.source();
        }
        report
    }

    /// Add the next cause, one level deeper than the last.
    #[must_use]
    pub fn cause(mut self, cause: impl Into<String>) -> Self {
        self.causes.push(cause.into());
        self
    }

    /// Show where the problem is, as a [`Code`](crate::Code) excerpt under the causes.
    ///
    /// ```
    /// use tinsel::{Code, Report};
    ///
    /// let config = "[server]\nport = \"eighty\"\n";
    /// Report::new("couldn't read app.toml")
    ///     .cause("port must be a number")
    ///     .code(Code::around(config, 2, 1).path("app.toml").caret(8, 8, "not a number"))
    ///     .hint("use a port like 8080")
    ///     .eprint();
    /// ```
    #[must_use]
    pub fn code(mut self, code: crate::layout::Code) -> Self {
        self.code = Some(code);
        self
    }

    /// What to do about it.
    #[must_use]
    pub fn hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(hint.into());
        self
    }

    /// The report as lines.
    pub fn lines(&self) -> Block {
        let mut out = Block::new();
        out.push(format!("  {}  {}", paint(RED, "✕"), bold(&paint(TEXT, &self.title))));
        for (depth, cause) in self.causes.iter().enumerate() {
            let indent = " ".repeat(5 + depth * 3);
            out.push(format!("{indent}{} {}", paint(LINE, "╰─"), paint(SOFT, cause)));
        }
        if let Some(code) = &self.code {
            for line in code.lines() {
                out.push(format!("   {line}"));
            }
        }
        if let Some(h) = &self.hint {
            out.push(Tone::Hint.line(h));
        }
        out
    }

    /// Print the report to stderr, where errors belong. As JSON it's an `"error"` event
    /// with `title`, `causes` and `hint`.
    pub fn eprint(&self) {
        if output::json() {
            let hint = self.hint.as_deref().unwrap_or_default();
            let code: Vec<String> = self
                .code
                .iter()
                .flat_map(|c| c.lines().plain().lines().map(String::from).collect::<Vec<_>>())
                .collect();
            let fields = [
                ("title", Val::Str(&self.title)),
                ("causes", Val::List(&self.causes)),
                ("code", Val::List(&code)),
                ("hint", Val::Str(hint)),
            ];
            return output::emit("error", &fields);
        }
        self.lines().eprint();
    }
}

impl fmt::Display for Report {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.lines().fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::output::{Output, capture, with_output};

    #[test]
    fn messages_print_as_lines_events_or_not_at_all() {
        crate::set_color_mode(crate::ColorMode::None);
        let out = capture(|| {
            success("built");
            info("cached");
            warn("slow");
            hint("try again");
            error("broke");
        });
        for want in ["  ●  built\n", "  ›  cached\n", "  ▲  slow\n", "  →  try again\n"] {
            assert!(out.contains(want), "{want:?} in {out:?}");
        }
        assert!(out.contains("broke\n"), "errors go to stderr, and are caught too");

        let quiet = capture(|| {
            with_output(Output::Quiet, || {
                Tone::Success.print("q-msg");
                Tone::Warn.print("q-msg");
            })
        });
        assert!(quiet.contains("▲  q-msg") && !quiet.contains("●  q-msg"), "{quiet:?}");

        let json = capture(|| with_output(Output::Json, || error("j-msg")));
        assert!(
            json.contains(r#"{"event":"message","level":"error","text":"j-msg"}"#),
            "{json}"
        );
    }

    #[test]
    fn counters_and_titles() {
        crate::set_color_mode(crate::ColorMode::None);
        let out = capture(|| {
            counter(2, 10, "c-pretty");
            title("t-pretty", Some("sub"));
            title("t-bare", None);
        });
        assert!(out.contains("   2/10  c-pretty\n"), "{out:?}");
        assert!(
            out.contains("\n  t-pretty  sub\n\n") && out.contains("\n  t-bare\n\n"),
            "{out:?}"
        );

        let quiet = capture(|| {
            with_output(Output::Quiet, || {
                counter(1, 2, "c-quiet");
                title("t-quiet", None);
            })
        });
        assert!(!quiet.contains("c-quiet") && !quiet.contains("t-quiet"));

        let json = capture(|| {
            with_output(Output::Json, || {
                counter(1, 2, "c-json");
                title("t-json", None);
            })
        });
        assert!(
            json.contains(r#"{"event":"counter","n":1,"total":2,"text":"c-json"}"#),
            "{json}"
        );
        assert!(
            json.contains(r#"{"event":"title","text":"t-json","subtitle":""}"#),
            "{json}"
        );
    }

    #[test]
    fn a_rule_without_a_label_is_just_a_line() {
        crate::set_color_mode(crate::ColorMode::None);
        let line = rule("");
        assert!(line.trim().chars().all(|c| c == '─') && line.len() > 10);
    }

    #[test]
    fn every_callout_has_its_colour() {
        let colours: Vec<Rgb> = [Callout::Note, Callout::Tip, Callout::Warning, Callout::Danger]
            .iter()
            .map(|c| c.color())
            .collect();
        assert_eq!(colours, [ROSE, SAGE, AMBER, RED]);
    }

    #[derive(Debug)]
    struct Wrapped(std::io::Error);

    impl fmt::Display for Wrapped {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("couldn't read the config")
        }
    }

    impl std::error::Error for Wrapped {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            Some(&self.0)
        }
    }

    #[test]
    fn reports_follow_the_whole_chain_and_show_code() {
        crate::set_color_mode(crate::ColorMode::None);
        let report = Report::from_error("couldn't start", &Wrapped(std::io::Error::other("disk full")))
            .code(crate::layout::Code::around("port = x\n", 1, 0).caret(8, 1, "here"));
        let text = report.to_string();
        assert!(
            text.contains("╰─ couldn't read the config") && text.contains("╰─ disk full"),
            "{text}"
        );
        assert!(text.contains("port = x") && text.contains("here"), "{text}");
        report.eprint();

        let json = capture(|| with_output(Output::Json, || report.clone().hint("fix it").eprint()));
        assert!(json.contains(r#""event":"error","title":"couldn't start""#), "{json}");
        assert!(
            json.contains(r#""hint":"fix it""#) && json.contains("port = x"),
            "{json}"
        );
    }

    #[test]
    fn a_rule_carries_its_label() {
        crate::set_color_mode(crate::ColorMode::None);
        assert!(rule("build").starts_with("  ── build ──"));
    }

    #[test]
    fn every_level_is_named_in_events() {
        let json = capture(|| {
            with_output(Output::Json, || {
                for print in [success, info, warn, hint] {
                    print("x");
                }
            })
        });
        for level in ["success", "info", "warn", "hint"] {
            assert!(json.contains(&format!(r#""level":"{level}""#)), "{level} in {json}");
        }
    }
}