rs-rich-ext 0.0.13

Extensions for the `rich` Rust port: diagnostics, structured data, clap and tracing integration, diffs, workflow renderables and a plugin registry
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
//! KPI cards: a label, a value, its change and its trend.

use rich::measure::Measurement;
use rich::{Console, ConsoleOptions, Renderable, Segment};

use super::{
    cells, has_colour, lines_to_segments, theme_style, truncate, Charset, Line, Sparkline, Status,
    ValueFormat,
};

/// The narrowest card that keeps its border.
const MIN_BORDERED: usize = 5;

/// A change, as written on the card.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Delta {
    Absolute(f64),
    Percent(f64),
}

impl Delta {
    fn value(self) -> f64 {
        match self {
            Delta::Absolute(v) | Delta::Percent(v) => v,
        }
    }
}

/// One key number: a label, the value, its change with a direction, an
/// optional sparkline and an optional [`Status`].
///
/// ```text
/// ╭───────────────────────╮
/// │ Requests         ✓ ok │
/// │ 12.4k/s               │
/// │ ▲ +8.49% vs last week │
/// │ ▁▂▂▄▆▅▃▆█▇▅▃          │
/// ╰───────────────────────╯
/// ```
///
/// - The delta is an arrow and a signed number: `▲ +8.5%`, `▼ -120`,
///   `= 0` (`^`, `v` and `=` in ASCII). It is styled `chart.delta.good` or
///   `chart.delta.bad` by whether the change is for the better
///   ([`higher_is_better`](Self::higher_is_better), on by default), and
///   the arrow and sign carry the direction without colour.
/// - The status is a symbol and a word (`✓ ok`, `! warning`,
///   `✗ critical`), right-aligned on the label's line.
/// - The card measures to its content, so cards sit side by side in a
///   `Columns` or a `Table` grid; [`width`](Self::width) fixes the width and
///   [`expand`](Self::expand) fills what it is given, so a row of cards
///   lines up. Every card with the same parts has the same height.
/// - Given less width the label is cut, then the other lines; the
///   sparkline resamples. Below 5 cells the border goes.
///
/// ```
/// use rich::Console;
/// use rich_ext::chart::{Charset, KpiCard, Status};
///
/// let console = Console::builder().width(40).color_system(None).build();
/// let card = KpiCard::new("Errors", 42.0)
///     .previous(56.0)
///     .higher_is_better(false)
///     .caption("vs yesterday")
///     .status(Status::Warning);
/// assert_eq!(
///     console.render_export(&card),
///     concat!(
///         "╭─────────────────────╮\n",
///         "│ Errors    ! warning │\n",
///         "│ 42                  │\n",
///         "│ ▼ -25% vs yesterday │\n",
///         "╰─────────────────────╯\n",
///     )
/// );
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct KpiCard {
    label: String,
    value: f64,
    format: ValueFormat,
    unit: String,
    delta: Option<Delta>,
    caption: Option<String>,
    higher_is_better: bool,
    sparkline: Option<Sparkline>,
    status: Option<Status>,
    width: Option<usize>,
    expand: bool,
    border: bool,
    charset: Charset,
}

impl KpiCard {
    /// A card called `label` showing `value`.
    pub fn new(label: impl Into<String>, value: f64) -> Self {
        KpiCard {
            label: label.into(),
            value,
            format: ValueFormat::Compact,
            unit: String::new(),
            delta: None,
            caption: None,
            higher_is_better: true,
            sparkline: None,
            status: None,
            width: None,
            expand: false,
            border: true,
            charset: Charset::Auto,
        }
    }

    /// How the value and an absolute delta are written (default
    /// [`ValueFormat::Compact`]).
    pub fn format(mut self, format: ValueFormat) -> Self {
        self.format = format;
        self
    }

    /// Written right after the value, such as `%`, `/s` or ` ms`.
    pub fn unit(mut self, unit: impl Into<String>) -> Self {
        self.unit = unit.into();
        self
    }

    /// The change as an amount, written in the value's format.
    pub fn delta(mut self, delta: f64) -> Self {
        self.delta = Some(Delta::Absolute(delta));
        self
    }

    /// The change as a percentage.
    pub fn delta_percent(mut self, percent: f64) -> Self {
        self.delta = Some(Delta::Percent(percent));
        self
    }

    /// The change from `previous` to the value, as a percentage (as an
    /// amount when `previous` is 0).
    pub fn previous(mut self, previous: f64) -> Self {
        let change = self.value - previous;
        self.delta = Some(if previous != 0.0 && previous.is_finite() {
            Delta::Percent(change / previous.abs() * 100.0)
        } else {
            Delta::Absolute(change)
        });
        self
    }

    /// Words after the delta, such as `vs last week`.
    pub fn caption(mut self, caption: impl Into<String>) -> Self {
        self.caption = Some(caption.into());
        self
    }

    /// Whether a rise is good news (default on); off for errors, latency
    /// and costs.
    pub fn higher_is_better(mut self, higher: bool) -> Self {
        self.higher_is_better = higher;
        self
    }

    /// A sparkline under the delta.
    pub fn sparkline(mut self, sparkline: Sparkline) -> Self {
        self.sparkline = Some(sparkline);
        self
    }

    /// A sparkline of `values` under the delta.
    pub fn trend(self, values: impl IntoIterator<Item = f64>) -> Self {
        self.sparkline(Sparkline::new(values))
    }

    /// The status, right-aligned on the label's line.
    pub fn status(mut self, status: Status) -> Self {
        self.status = Some(status);
        self
    }

    /// Fix the card's width, border included.
    pub fn width(mut self, width: usize) -> Self {
        self.width = Some(width.max(1));
        self
    }

    /// Fill the width given (default off: the content's width).
    pub fn expand(mut self, expand: bool) -> Self {
        self.expand = expand;
        self
    }

    /// Draw the border (default on).
    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }

    /// Glyphs to draw with: [`Charset::Ascii`] asks for ASCII; anything else
    /// draws box drawing and arrows (ASCII on an ASCII-only console). A
    /// sparkline keeps its own charset unless this asks for ASCII.
    pub fn charset(mut self, charset: Charset) -> Self {
        self.charset = charset;
        self
    }

    fn value_text(&self) -> String {
        let value = self.format.format(self.value);
        if self.value.is_finite() {
            format!("{value}{}", self.unit)
        } else {
            value
        }
    }

    fn delta_text(&self, ascii: bool) -> Option<String> {
        let delta = self.delta?;
        let v = delta.value();
        // NaN, or a change too large for an `f64`: flat, with no sign.
        let direction = v.is_finite().then(|| v.partial_cmp(&0.0)).flatten();
        let arrow = match (direction, ascii) {
            (Some(std::cmp::Ordering::Greater), false) => "▲",
            (Some(std::cmp::Ordering::Greater), true) => "^",
            (Some(std::cmp::Ordering::Less), false) => "▼",
            (Some(std::cmp::Ordering::Less), true) => "v",
            _ => "=",
        };
        let number = match delta {
            Delta::Absolute(v) => self.format.format(v),
            Delta::Percent(v) => {
                let text = ValueFormat::Compact.format(v);
                if v.is_finite() {
                    format!("{text}%")
                } else {
                    text
                }
            }
        };
        let sign = if v > 0.0 && v.is_finite() && number != "0" && number != "0%" {
            "+"
        } else {
            ""
        };
        Some(format!("{arrow} {sign}{number}"))
    }

    fn delta_key(&self) -> &'static str {
        let v = self.delta.map_or(0.0, Delta::value);
        if !v.is_finite() || v == 0.0 {
            "chart.delta.flat"
        } else if (v > 0.0) == self.higher_is_better {
            "chart.delta.good"
        } else {
            "chart.delta.bad"
        }
    }

    fn charsets(&self, console: &Console, options: &ConsoleOptions) -> (bool, Option<Charset>) {
        let ascii = self.charset.resolve(console, options, Charset::Blocks) == Charset::Ascii;
        let spark = self.sparkline.as_ref().map(|s| {
            if ascii {
                Charset::Ascii
            } else {
                s.resolved_charset(console, options)
            }
        });
        (ascii, spark)
    }

    /// The content's natural width, border not included.
    fn inner_width(&self, ascii: bool, spark: Option<Charset>) -> usize {
        let header = cells(&self.label)
            + self.status.map_or(0, |s| {
                cells(&s.text(ascii)) + usize::from(!self.label.is_empty())
            });
        let delta = match (self.delta_text(ascii), &self.caption) {
            (Some(d), Some(c)) => cells(&d) + 1 + cells(c),
            (Some(d), None) => cells(&d),
            (None, Some(c)) => cells(c),
            (None, None) => 0,
        };
        let spark = match (&self.sparkline, spark) {
            (Some(s), Some(charset)) => s.natural_width(charset),
            _ => 0,
        };
        header
            .max(cells(&self.value_text()))
            .max(delta)
            .max(spark)
            .max(1)
    }

    fn natural_width(&self, ascii: bool, spark: Option<Charset>) -> usize {
        self.inner_width(ascii, spark) + if self.border { 4 } else { 0 }
    }

    fn lines(&self, console: &Console, options: &ConsoleOptions) -> Vec<Line> {
        let given = options.max_width;
        let (ascii, spark) = self.charsets(console, options);
        let colour = has_colour(console);
        let total = if self.expand {
            given
        } else {
            self.width
                .unwrap_or_else(|| self.natural_width(ascii, spark))
                .min(given)
        };
        let border = self.border && total >= MIN_BORDERED;
        let inner = if border { total - 4 } else { total };
        let style = |key: &str| colour.then(|| theme_style(console, key));

        let mut content = Vec::new();
        // The label, and the status on the right.
        let mut header = Line::new();
        let status = self.status.map(|s| truncate(&s.text(ascii), inner, ascii));
        let status_w = status.as_deref().map_or(0, cells);
        let label_room = inner.saturating_sub(status_w + usize::from(status_w > 0));
        let label = truncate(&self.label, label_room, ascii);
        header.push(&label, style("chart.kpi.label"));
        if let (Some(text), Some(s)) = (&status, self.status) {
            header.pad(inner - cells(&label) - status_w);
            header.push(text, style(s.key()));
        }
        content.push(header);
        let mut value = Line::new();
        value.push(
            &truncate(&self.value_text(), inner, ascii),
            style("chart.kpi.value"),
        );
        content.push(value);
        let delta = self.delta_text(ascii);
        if delta.is_some() || self.caption.is_some() {
            let mut line = Line::new();
            if let Some(d) = &delta {
                line.push(&truncate(d, inner, ascii), style(self.delta_key()));
            }
            if let Some(caption) = &self.caption {
                let room = inner.saturating_sub(line.width() + usize::from(line.width() > 0));
                if room > 0 {
                    if line.width() > 0 {
                        line.pad(1);
                    }
                    line.push(&truncate(caption, room, ascii), style("chart.label"));
                }
            }
            content.push(line);
        }
        if let (Some(s), Some(charset)) = (&self.sparkline, spark) {
            content.push(s.line(console, charset, inner));
        }

        if !border {
            for line in &mut content {
                line.crop(inner);
                let fill = inner - line.width();
                line.pad(fill);
            }
            return content;
        }
        let edge = style("chart.kpi.border");
        let (tl, tr, bl, br, h, v) = if ascii {
            ('+', '+', '+', '+', '-', '|')
        } else {
            ('╭', '╮', '╰', '╯', '─', '│')
        };
        let rule = |l: char, r: char| {
            let mut line = Line::new();
            line.push(
                &format!("{l}{}{r}", h.to_string().repeat(total - 2)),
                edge.clone(),
            );
            line
        };
        let mut out = vec![rule(tl, tr)];
        for mut body in content {
            body.crop(inner);
            let fill = inner - body.width();
            body.pad(fill);
            let mut line = Line::new();
            line.push(&format!("{v} "), edge.clone());
            line.append(body);
            line.push(&format!(" {v}"), edge.clone());
            out.push(line);
        }
        out.push(rule(bl, br));
        out
    }
}

impl Renderable for KpiCard {
    fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        lines_to_segments(self.lines(console, options), options.max_width)
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        let (ascii, spark) = self.charsets(console, options);
        let max = if self.expand {
            options.max_width
        } else {
            self.width
                .unwrap_or_else(|| self.natural_width(ascii, spark))
        };
        let min = self.width.unwrap_or(if self.border { 8 } else { 4 });
        Measurement::new(min.min(max), max)
            .with_maximum(options.max_width)
            .normalize()
    }
}