splashboard 2.10.2

A customizable terminal splash screen with plugin-based data sources
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use ratatui::{
    Frame,
    layout::Rect,
    style::Style,
    text::{Line, Span},
    widgets::Paragraph,
};

use crate::options::OptionSchema;
use crate::payload::{BarsData, Body};
use crate::theme::{self, ColorKey, Theme};

use super::{Registry, RenderOptions, Renderer, Shape};

const COLOR_KEYS: &[ColorKey] = &[theme::TEXT, theme::TEXT_DIM];

const OPTION_SCHEMAS: &[OptionSchema] = &[
    OptionSchema {
        name: "max_items",
        type_hint: "positive integer",
        required: false,
        default: Some("all bars"),
        description: "Cap on rendered ranks. Keeps the top N by value when the input has more bars than the cap.",
    },
    OptionSchema {
        name: "style",
        type_hint: "\"number\" | \"medal\" | \"none\"",
        required: false,
        default: Some("\"number\""),
        description: "Rank-prefix glyph. `number` (default) emits `1.` / `2.` / `3.`. `medal` emits πŸ₯‡ / πŸ₯ˆ / πŸ₯‰ for the top three then numbers for the rest. `none` omits the prefix.",
    },
    OptionSchema {
        name: "align",
        type_hint: "\"left\" | \"center\" | \"right\"",
        required: false,
        default: Some("\"left\""),
        description: "Horizontal alignment of the whole ranking within its cell.",
    },
];

const MEDALS: [&str; 3] = ["πŸ₯‡", "πŸ₯ˆ", "πŸ₯‰"];
const COLUMN_GAP: &str = "  ";

/// Rank-prefixed list view for the `Bars` shape. Preserves the fetcher's bar order β€” sort is
/// the fetcher's responsibility, not the renderer's, so that fetchers emitting recency-sorted
/// (`steam_owned_games sort = "recent"`), alphabetical, or any non-value-desc order display as
/// intended. Use `style = "medal"` to highlight the top three with πŸ₯‡/πŸ₯ˆ/πŸ₯‰.
pub struct ListRankingRenderer;

impl Renderer for ListRankingRenderer {
    fn name(&self) -> &str {
        "list_ranking"
    }
    fn description(&self) -> &'static str {
        "Rank-prefixed table preserving the fetcher's bar order: rank prefix (`1.` / `2.` numbers, πŸ₯‡/πŸ₯ˆ/πŸ₯‰ medals, or none), label, and right-aligned value column. The text-first sibling of `chart_bar` β€” pick this when the values matter more than their relative bar lengths. Sort is the fetcher's responsibility; `max_items` truncates from the head."
    }
    fn accepts(&self) -> &[Shape] {
        &[Shape::Bars]
    }
    fn color_keys(&self) -> &[ColorKey] {
        COLOR_KEYS
    }
    fn option_schemas(&self) -> &[OptionSchema] {
        OPTION_SCHEMAS
    }
    fn render(
        &self,
        frame: &mut Frame,
        area: Rect,
        body: &Body,
        opts: &RenderOptions,
        theme: &Theme,
        _registry: &Registry,
    ) {
        if let Body::Bars(d) = body {
            render_ranking(frame, area, d, opts, theme);
        }
    }
}

fn render_ranking(
    frame: &mut Frame,
    area: Rect,
    data: &BarsData,
    opts: &RenderOptions,
    theme: &Theme,
) {
    if area.width == 0 || area.height == 0 {
        return;
    }
    let rows = capped_rows(data, opts.max_items);
    let style = opts.style.as_deref().unwrap_or("number");
    let prefixes: Vec<String> = (0..rows.len()).map(|i| rank_prefix(i, style)).collect();
    let widths = column_widths(&prefixes, &rows);
    // Stretch the label column so the value sits flush against the right edge of
    // the cell instead of leaving dead space, and truncate over-long labels so the
    // value doesn't get clipped off the right.
    let target = align_rect(area, widths.total() as u16, opts.align.as_deref());
    let prefix_part = if widths.prefix > 0 {
        widths.prefix + 1
    } else {
        0
    };
    let fixed = prefix_part + COLUMN_GAP.len() + widths.value;
    let label_width = (target.width as usize).saturating_sub(fixed);
    let effective = Widths {
        prefix: widths.prefix,
        label: label_width,
        value: widths.value,
    };
    let lines: Vec<Line> = rows
        .iter()
        .zip(prefixes.iter())
        .map(|(row, prefix)| compose_line(prefix, &row.label, &row.display, effective, theme))
        .collect();
    frame.render_widget(
        Paragraph::new(lines).style(Style::default().fg(theme.text)),
        target,
    );
}

/// Row view. `display` is what the right column actually prints β€” `Bar.value_label` when
/// present, otherwise the numeric value stringified.
struct Row {
    label: String,
    display: String,
}

/// Build rows in the fetcher's bar order. `max_items` truncates from the head β€” sort is the
/// fetcher's job (lastfm's `chart.getTop*` is server-ranked, steam's `_owned_games` sorts by
/// playtime or recency per its `sort` option, basic_bars carries the user-authored order).
fn capped_rows(data: &BarsData, cap: Option<usize>) -> Vec<Row> {
    let mut rows: Vec<Row> = data
        .bars
        .iter()
        .map(|b| Row {
            label: b.label.clone(),
            display: b.value_label.clone().unwrap_or_else(|| b.value.to_string()),
        })
        .collect();
    if let Some(n) = cap {
        rows.truncate(n);
    }
    rows
}

fn rank_prefix(index: usize, style: &str) -> String {
    match style {
        "none" => String::new(),
        "medal" => MEDALS
            .get(index)
            .map(|m| (*m).to_string())
            .unwrap_or_else(|| format!("{}.", index + 1)),
        _ => format!("{}.", index + 1),
    }
}

#[derive(Debug, Clone, Copy)]
struct Widths {
    prefix: usize,
    label: usize,
    value: usize,
}

impl Widths {
    fn total(self) -> usize {
        let prefix_part = if self.prefix > 0 { self.prefix + 1 } else { 0 };
        prefix_part + self.label + COLUMN_GAP.len() + self.value
    }
}

fn column_widths(prefixes: &[String], rows: &[Row]) -> Widths {
    Widths {
        prefix: prefixes.iter().map(|p| display_width(p)).max().unwrap_or(0),
        label: rows
            .iter()
            .map(|r| r.label.chars().count())
            .max()
            .unwrap_or(0),
        value: rows
            .iter()
            .map(|r| r.display.chars().count())
            .max()
            .unwrap_or(0),
    }
}

fn compose_line<'a>(
    prefix: &str,
    label: &str,
    value: &str,
    widths: Widths,
    theme: &Theme,
) -> Line<'a> {
    let mut spans: Vec<Span<'a>> = Vec::with_capacity(4);
    if widths.prefix > 0 {
        let pad = widths.prefix.saturating_sub(display_width(prefix));
        spans.push(Span::styled(
            format!("{}{prefix} ", " ".repeat(pad)),
            Style::default().fg(theme.text_dim),
        ));
    }
    spans.push(Span::raw(fit_label(label, widths.label)));
    spans.push(Span::raw(COLUMN_GAP));
    spans.push(Span::raw(pad_left(value, widths.value)));
    Line::from(spans)
}

/// Pad with trailing spaces if `label` fits, else truncate from the right (with a single
/// `…` ellipsis) so the value column stays visible. Empty width returns empty.
fn fit_label(label: &str, width: usize) -> String {
    if width == 0 {
        return String::new();
    }
    let n = label.chars().count();
    if n <= width {
        let mut out = String::with_capacity(width);
        out.push_str(label);
        out.extend(std::iter::repeat_n(' ', width - n));
        return out;
    }
    if width == 1 {
        return "…".to_string();
    }
    let take = width - 1;
    let mut out: String = label.chars().take(take).collect();
    out.push('…');
    out
}

fn align_rect(area: Rect, content_width: u16, align: Option<&str>) -> Rect {
    if content_width == 0 || content_width >= area.width {
        return area;
    }
    let offset = match align {
        Some("center") => (area.width - content_width) / 2,
        Some("right") => area.width - content_width,
        _ => return area,
    };
    Rect {
        x: area.x + offset,
        y: area.y,
        width: content_width,
        height: area.height,
    }
}

fn pad_left(s: &str, width: usize) -> String {
    let n = s.chars().count();
    if n >= width {
        return s.to_string();
    }
    let mut out = String::with_capacity(width);
    out.extend(std::iter::repeat_n(' ', width - n));
    out.push_str(s);
    out
}

// Medal codepoints render two cells in every wcwidth-aware terminal; everything else we emit
// here is ASCII (digits, dot, padding spaces). A lookup table keeps the column maths honest
// without pulling in a unicode-width dependency for one renderer.
fn display_width(s: &str) -> usize {
    s.chars()
        .map(|c| {
            if matches!(c, 'πŸ₯‡' | 'πŸ₯ˆ' | 'πŸ₯‰') {
                2
            } else {
                1
            }
        })
        .sum()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::payload::{Bar, BarsData, Body, Payload};
    use crate::render::test_utils::{line_text, render_to_buffer_with_spec};
    use crate::render::{Registry, RenderSpec};

    fn payload(bars: &[(&str, u64)]) -> Payload {
        Payload {
            icon: None,
            status: None,
            format: None,
            body: Body::Bars(BarsData {
                bars: bars
                    .iter()
                    .map(|(l, v)| Bar {
                        label: (*l).into(),
                        value: *v,
                        value_label: None,
                    })
                    .collect(),
            }),
        }
    }

    fn render_with(spec: &RenderSpec, p: &Payload, w: u16, h: u16) -> ratatui::buffer::Buffer {
        let registry = Registry::with_builtins();
        render_to_buffer_with_spec(p, Some(spec), &registry, w, h)
    }

    #[test]
    fn preserves_fetcher_bar_order_with_numeric_prefix() {
        // The fetcher owns sort order β€” renderer just rank-prefixes the bars as they arrive.
        // A recency-sorted owned_games list, a server-ranked lastfm chart, and a user-authored
        // basic_bars list all show in their respective intended order.
        let p = payload(&[("alice", 3), ("bob", 7), ("carol", 5)]);
        let spec = RenderSpec::Short("list_ranking".into());
        let buf = render_with(&spec, &p, 30, 3);
        let row0 = line_text(&buf, 0);
        let row1 = line_text(&buf, 1);
        let row2 = line_text(&buf, 2);
        assert!(
            row0.contains("1.") && row0.contains("alice"),
            "row0: {row0:?}"
        );
        assert!(
            row1.contains("2.") && row1.contains("bob"),
            "row1: {row1:?}"
        );
        assert!(
            row2.contains("3.") && row2.contains("carol"),
            "row2: {row2:?}"
        );
    }

    #[test]
    fn max_items_truncates_from_the_head_preserving_order() {
        #[derive(serde::Deserialize)]
        struct W {
            render: RenderSpec,
        }
        let w: W = toml::from_str(r#"render = { type = "list_ranking", max_items = 2 }"#).unwrap();
        // With auto-sort gone, max_items keeps the first 2 in the fetcher's order β€” not the
        // numerically top 2. A fetcher that wants "top N by value" sorts itself.
        let p = payload(&[("a", 1), ("b", 9), ("c", 5), ("d", 7)]);
        let buf = render_with(&w.render, &p, 30, 4);
        let joined: String = (0..4).map(|y| line_text(&buf, y)).collect();
        assert!(joined.contains("a"), "missing first: {joined:?}");
        assert!(joined.contains("b"), "missing second: {joined:?}");
        assert!(!joined.contains("c"), "third should be capped: {joined:?}");
        assert!(!joined.contains("d"), "fourth should be capped: {joined:?}");
    }

    #[test]
    fn medal_style_decorates_top_three() {
        #[derive(serde::Deserialize)]
        struct W {
            render: RenderSpec,
        }
        let w: W =
            toml::from_str(r#"render = { type = "list_ranking", style = "medal" }"#).unwrap();
        let p = payload(&[("a", 9), ("b", 7), ("c", 5), ("d", 3)]);
        let buf = render_with(&w.render, &p, 30, 4);
        let r0 = line_text(&buf, 0);
        let r3 = line_text(&buf, 3);
        assert!(r0.contains("πŸ₯‡"), "row0: {r0:?}");
        assert!(line_text(&buf, 1).contains("πŸ₯ˆ"));
        assert!(line_text(&buf, 2).contains("πŸ₯‰"));
        // Fourth place falls back to a numeric prefix.
        assert!(r3.contains("4."), "row3: {r3:?}");
    }

    #[test]
    fn value_label_overrides_numeric_value_when_present() {
        // Fetchers pre-format a unit ("80h", "2024-07-03") in `Bar.value_label`. The right
        // column prints that text instead of stringifying `value`, so a recent-launches widget
        // shows dates rather than unix timestamps and a steam top-games widget shows hours
        // rather than raw minutes.
        let payload = Payload {
            icon: None,
            status: None,
            format: None,
            body: Body::Bars(BarsData {
                bars: vec![
                    Bar {
                        label: "Game A".into(),
                        value: 4830,
                        value_label: Some("80h".into()),
                    },
                    Bar {
                        label: "Game B".into(),
                        value: 12_000,
                        value_label: Some("200h".into()),
                    },
                ],
            }),
        };
        let spec = RenderSpec::Short("list_ranking".into());
        let buf = render_with(&spec, &payload, 30, 2);
        let row0 = line_text(&buf, 0);
        let row1 = line_text(&buf, 1);
        assert!(row0.contains("Game A") && row0.contains("80h"), "{row0:?}");
        assert!(row1.contains("Game B") && row1.contains("200h"), "{row1:?}");
        assert!(!row0.contains("4830"), "raw value leaked: {row0:?}");
        assert!(!row1.contains("12000"), "raw value leaked: {row1:?}");
    }

    #[test]
    fn missing_value_label_falls_back_to_numeric_value() {
        let payload = Payload {
            icon: None,
            status: None,
            format: None,
            body: Body::Bars(BarsData {
                bars: vec![Bar {
                    label: "alpha".into(),
                    value: 42,
                    value_label: None,
                }],
            }),
        };
        let spec = RenderSpec::Short("list_ranking".into());
        let buf = render_with(&spec, &payload, 30, 1);
        let row0 = line_text(&buf, 0);
        assert!(row0.contains("42"), "{row0:?}");
    }

    #[test]
    fn style_none_omits_prefix() {
        #[derive(serde::Deserialize)]
        struct W {
            render: RenderSpec,
        }
        let w: W = toml::from_str(r#"render = { type = "list_ranking", style = "none" }"#).unwrap();
        let p = payload(&[("alpha", 3), ("beta", 9)]);
        let buf = render_with(&w.render, &p, 30, 2);
        let r0 = line_text(&buf, 0);
        assert!(!r0.contains("1."), "row0: {r0:?}");
        // First in the fetcher's order (alpha) leads the rendered list now that auto-sort is gone.
        assert!(r0.trim_start().starts_with("alpha"), "row0: {r0:?}");
    }

    #[test]
    fn ties_preserve_input_order() {
        // Stable sort: same value β†’ original order kept (alpha first since it's first in input).
        let p = payload(&[("alpha", 5), ("beta", 5), ("gamma", 5)]);
        let spec = RenderSpec::Short("list_ranking".into());
        let buf = render_with(&spec, &p, 30, 3);
        assert!(line_text(&buf, 0).contains("alpha"));
        assert!(line_text(&buf, 1).contains("beta"));
        assert!(line_text(&buf, 2).contains("gamma"));
    }

    #[test]
    fn align_center_offsets_block() {
        #[derive(serde::Deserialize)]
        struct W {
            render: RenderSpec,
        }
        let w: W =
            toml::from_str(r#"render = { type = "list_ranking", align = "center" }"#).unwrap();
        let p = payload(&[("a", 1)]);
        let buf = render_with(&w.render, &p, 20, 1);
        let row = line_text(&buf, 0);
        // Single short row centered in width 20 should have leading whitespace.
        assert!(row.starts_with(' '), "expected leading spaces: {row:?}");
    }

    #[test]
    fn value_column_right_aligns_across_rows() {
        // Values 142 vs 9: the trailing column should line up so both end at the same column.
        let p = payload(&[("a", 142), ("b", 9)]);
        let spec = RenderSpec::Short("list_ranking".into());
        let buf = render_with(&spec, &p, 30, 2);
        let r0 = line_text(&buf, 0).trim_end().to_string();
        let r1 = line_text(&buf, 1).trim_end().to_string();
        assert!(r0.ends_with("142"), "row0 should end with 142: {r0:?}");
        assert!(r1.ends_with('9'), "row1 should end with 9: {r1:?}");
        assert_eq!(
            r0.len(),
            r1.len(),
            "trailing column should align: {r0:?} vs {r1:?}"
        );
    }

    #[test]
    fn empty_area_does_not_panic() {
        let p = payload(&[("a", 1)]);
        let spec = RenderSpec::Short("list_ranking".into());
        let _ = render_with(&spec, &p, 0, 0);
    }

    #[test]
    fn rank_prefix_falls_back_to_number_past_third_medal() {
        assert_eq!(rank_prefix(0, "medal"), "πŸ₯‡");
        assert_eq!(rank_prefix(2, "medal"), "πŸ₯‰");
        assert_eq!(rank_prefix(3, "medal"), "4.");
        assert_eq!(rank_prefix(0, "number"), "1.");
        assert_eq!(rank_prefix(0, "none"), "");
    }

    #[test]
    fn display_width_counts_medals_as_two_cells() {
        assert_eq!(display_width("πŸ₯‡"), 2);
        assert_eq!(display_width("10."), 3);
    }

    #[test]
    fn fit_label_covers_pad_truncate_and_zero_width_branches() {
        // Zero width collapses to empty regardless of the label.
        assert_eq!(fit_label("anything", 0), "");
        // A label that fits is right-padded with spaces to the column width.
        assert_eq!(fit_label("ab", 5), "ab   ");
        // Width 1 has no room for content beside the ellipsis, so it is the ellipsis alone.
        assert_eq!(fit_label("hello", 1), "…");
        // A longer label keeps `width - 1` chars and appends a single ellipsis.
        assert_eq!(fit_label("hello", 3), "he…");
    }

    #[test]
    fn pad_left_prepends_spaces_only_when_shorter_than_width() {
        assert_eq!(pad_left("9", 4), "   9");
        // Already at or past the width is returned unchanged.
        assert_eq!(pad_left("142", 3), "142");
        assert_eq!(pad_left("longer", 2), "longer");
    }

    #[test]
    fn align_rect_offsets_for_center_and_right_and_passes_through_otherwise() {
        let area = Rect::new(2, 1, 20, 3);
        // Center places the block in the middle of the spare width.
        assert_eq!(align_rect(area, 10, Some("center")).x, 2 + 5);
        // Right pushes it flush against the trailing edge.
        let right = align_rect(area, 10, Some("right"));
        assert_eq!(right.x, 2 + 10);
        assert_eq!(right.width, 10);
        // No align hint, zero content, or content wider than the area all return the area as-is.
        assert_eq!(align_rect(area, 10, None), area);
        assert_eq!(align_rect(area, 0, Some("center")), area);
        assert_eq!(align_rect(area, 99, Some("right")), area);
    }
}