mirador 0.16.0

An opinionated personal dashboard for your terminal: world clocks, a calendar, weather, tasks, notes, a market watchlist, and live CPU and network graphs.
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
//! The display and utility "typefaces", plus weather art.
//!
//! A terminal has exactly one font, so a dashboard that wants typographic
//! hierarchy has to manufacture it. Two faces live here:
//!
//! - [`BigText`] draws scalable block numerals for the chronometer.
//! - [`utility`] uppercases text for labels, which callers set bold. Weight
//!   and case separate a label from its value without relying on colour alone
//!   — which matters, because a user's terminal theme can render our colours
//!   as almost anything.
//!
//! The numerals use run-length rows rather than a table of pre-drawn strings,
//! an idea taken from `clock-tui`'s "bricks" font. Each glyph is five rows, and
//! each row is a list of alternating off/on runs over a six-unit grid. Thirteen
//! glyphs fit in a few lines, and integer scaling comes for free — which is the
//! whole reason to do it this way rather than hand-drawing 3x5 strings.

/// Glyph grid width in units, before scaling.
const CELL_W: u16 = 6;
/// Glyph grid height in units, before scaling.
const CELL_H: u16 = 5;
/// Columns between glyphs. Deliberately not scaled: at large sizes a scaled
/// gap makes the digits read as separate objects rather than as one number.
const GAP: u16 = 2;

/// Run-length rows for one glyph: five rows, each alternating off/on runs
/// starting with off. `[0, 6]` is a full bar; `[2, 2]` is a centred stub.
type Glyph = [&'static [u16]; 5];

fn glyph(c: char) -> Glyph {
    match c {
        '0' => [
            &[0, 6],
            &[0, 2, 2, 2],
            &[0, 2, 2, 2],
            &[0, 2, 2, 2],
            &[0, 6],
        ],
        '1' => [&[0, 4], &[2, 2], &[2, 2], &[2, 2], &[0, 6]],
        '2' => [&[0, 6], &[4, 2], &[0, 6], &[0, 2], &[0, 6]],
        '3' => [&[0, 6], &[4, 2], &[0, 6], &[4, 2], &[0, 6]],
        '4' => [&[0, 2, 2, 2], &[0, 2, 2, 2], &[0, 6], &[4, 2], &[4, 2]],
        '5' => [&[0, 6], &[0, 2], &[0, 6], &[4, 2], &[0, 6]],
        '6' => [&[0, 6], &[0, 2], &[0, 6], &[0, 2, 2, 2], &[0, 6]],
        '7' => [&[0, 6], &[4, 2], &[4, 2], &[4, 2], &[4, 2]],
        '8' => [&[0, 6], &[0, 2, 2, 2], &[0, 6], &[0, 2, 2, 2], &[0, 6]],
        '9' => [&[0, 6], &[0, 2, 2, 2], &[0, 6], &[4, 2], &[0, 6]],
        ':' => [&[], &[2, 2], &[], &[2, 2], &[]],
        '.' => [&[], &[], &[], &[], &[2, 2]],
        '-' => [&[], &[], &[0, 6], &[], &[]],
        // Unknown characters render blank but still occupy a cell, so a
        // malformed time string cannot break alignment.
        _ => [&[], &[], &[], &[], &[]],
    }
}

/// A block-numeral string rendered at an integer scale.
#[derive(Debug, Clone)]
pub struct BigText {
    /// One string per output row, all the same display width.
    pub rows: Vec<String>,
    pub width: u16,
    pub height: u16,
}

impl BigText {
    /// Render `text` at `scale`. A scale of 1 gives glyphs 6x5 cells.
    pub fn new(text: &str, scale: u16) -> Self {
        let scale = scale.max(1);
        let height = CELL_H * scale;
        let width = width_of(text, scale);
        let mut rows = vec![String::with_capacity(width as usize); height as usize];

        let mut pen = 0u16;
        for (index, c) in text.chars().enumerate() {
            if index > 0 {
                pen += GAP;
            }
            let g = glyph(c);
            for (unit_row, runs) in g.iter().enumerate() {
                // Each logical row is repeated `scale` times vertically.
                for repeat in 0..scale {
                    let row = usize::from(scale) * unit_row + usize::from(repeat);
                    let line = &mut rows[row];
                    pad_to(line, pen);

                    let mut cursor = pen;
                    let mut on = false;
                    for run in *runs {
                        let cells = run * scale;
                        if on {
                            pad_to(line, cursor);
                            for _ in 0..cells {
                                line.push('');
                            }
                        }
                        cursor += cells;
                        on = !on;
                    }
                }
            }
            pen += CELL_W * scale;
        }

        for row in &mut rows {
            pad_to(row, width);
        }

        Self {
            rows,
            width,
            height,
        }
    }
}

/// Extend `line` with spaces until it is `target` display columns wide.
fn pad_to(line: &mut String, target: u16) {
    let current = line.chars().count();
    for _ in current..usize::from(target) {
        line.push(' ');
    }
}

/// Columns `text` will occupy at `scale`.
///
/// Saturating rather than wrapping. The arithmetic is `u16` and every factor
/// comes from the caller, so a string of about ten thousand characters overflows
/// and panics in a debug build. Nothing passes one — the two callers hand it
/// `"00:00:00"` and a formatted clock — but a width that reports "wider than the
/// terminal" for absurd input is the right answer, and `fitting_scale` then
/// returns `None` and the panel falls back, which is what it does anyway.
pub fn width_of(text: &str, scale: u16) -> u16 {
    let scale = scale.max(1);
    let count = u16::try_from(text.chars().count()).unwrap_or(u16::MAX);
    if count == 0 {
        return 0;
    }
    count
        .saturating_mul(CELL_W)
        .saturating_mul(scale)
        .saturating_add(count.saturating_sub(1).saturating_mul(GAP))
}

/// The largest scale at which `text` fits in `available` columns, or `None` if
/// even scale 1 will not fit.
pub fn fitting_scale(text: &str, available: u16, max_scale: u16) -> Option<u16> {
    (1..=max_scale.max(1))
        .rev()
        .find(|scale| width_of(text, *scale) <= available)
}

/// Render text in the utility face: uppercase, normally spaced.
///
/// `utility("next hours")` becomes `NEXT HOURS`.
///
/// **The result can be wider than the input**, so measure it rather than the
/// argument. Uppercasing is not a one-to-one map: `ß` becomes `SS` and `ffl`
/// becomes `FFL`, so ten characters in can be thirty cells out. Every caller
/// today either passes a literal or truncates afterwards; the one that took
/// unbounded text is bounded at its source now, in `feed`.
///
/// This face used to be letterspaced — `N E X T  H O U R S` — on the theory
/// that tracking separates a label from data without leaning on colour. In
/// practice it read as stretched rather than as engraved, and it cost width
/// that narrow panels could not spare. Weight and colour carry the distinction
/// instead: callers pair this with `Modifier::BOLD` and `theme.label`.
pub fn utility(text: &str) -> String {
    let mut out = String::new();
    for word in text.split_whitespace() {
        if !out.is_empty() {
            out.push(' ');
        }
        out.extend(word.chars().flat_map(char::to_uppercase));
    }
    out
}

/// Broad weather categories, one per piece of art.
///
/// Deliberately coarser than the WMO code list: art that distinguished
/// "moderate" from "dense" drizzle would be illegible at this size.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sky {
    Clear,
    PartlyCloudy,
    Overcast,
    Fog,
    Drizzle,
    Rain,
    Showers,
    Snow,
    Thunder,
    Unknown,
}

/// Classify a WMO 4677 code, which is what Open-Meteo reports.
pub fn sky(code: u8) -> Sky {
    match code {
        0 | 1 => Sky::Clear,
        2 => Sky::PartlyCloudy,
        3 => Sky::Overcast,
        45 | 48 => Sky::Fog,
        51..=57 => Sky::Drizzle,
        61..=67 => Sky::Rain,
        80..=82 => Sky::Showers,
        71..=77 | 85 | 86 => Sky::Snow,
        95..=99 => Sky::Thunder,
        _ => Sky::Unknown,
    }
}

/// Rows in the weather art.
pub const ART_HEIGHT: usize = 4;
/// Columns in the weather art. Every row is padded to this.
pub const ART_WIDTH: usize = 12;

/// Four-row art for a sky condition.
///
/// Restricted to ASCII plus a handful of box-drawing characters, so it renders
/// in a plain terminal font with no Nerd Font or emoji fallback.
pub fn art(sky: Sky) -> [&'static str; ART_HEIGHT] {
    match sky {
        Sky::Clear => [
            r"   \   /    ",
            r"    .-.     ",
            r" --(   )--  ",
            r"   /   \    ",
        ],
        Sky::PartlyCloudy => [
            r"  \ /  .-.  ",
            r" - o -(   ) ",
            r"  / \ (___) ",
            r"            ",
        ],
        Sky::Overcast => [
            r"    .--.    ",
            r"  .(    ).  ",
            r" (___.__)_) ",
            r"            ",
        ],
        Sky::Fog => [
            r"  _ - _ - _ ",
            r"   _ - _ -  ",
            r"  _ - _ - _ ",
            r"            ",
        ],
        Sky::Drizzle => [
            r"    .-.     ",
            r"   (   ).   ",
            r"  (___(__)  ",
            r"   ' ' ' '  ",
        ],
        Sky::Rain => [
            r"    .-.     ",
            r"   (   ).   ",
            r"  (___(__)  ",
            r"  ' ' ' ' ' ",
        ],
        Sky::Showers => [
            r"  _ /''.-.  ",
            r"   \_(   ). ",
            r"   (___(__) ",
            r"    ' ' ' ' ",
        ],
        Sky::Snow => [
            r"    .-.     ",
            r"   (   ).   ",
            r"  (___(__)  ",
            r"   *  *  *  ",
        ],
        Sky::Thunder => [
            r"    .-.     ",
            r"   (   ).   ",
            r"  (___(__)  ",
            r"   /_  /_   ",
        ],
        Sky::Unknown => [
            r"            ",
            r"    .---.   ",
            r"   ( ? ? )  ",
            r"    `---'   ",
        ],
    }
}

/// An emoji mark for a sky condition, used in the hourly table.
///
/// Every glyph here is deliberately chosen so that `unicode-width` reports the
/// same cell count the terminal actually draws. That sounds pedantic and is
/// not: several of the obvious weather emoji — U+1F327 rain cloud, U+1F328
/// snow cloud, U+2601 cloud, U+1F32B fog — report width 1 but render as two
/// cells, which
/// silently shifts every column after them and puts values under the wrong
/// headers. The set below is restricted to glyphs that measure 2 and draw 2.
///
/// `every_sky_mark_has_a_predictable_display_width` asserts this. Plain code
/// rather than an intra-doc link: the test is behind `cfg(test)`, so rustdoc
/// cannot resolve a link to it and `-D warnings` turns that into a build
/// failure.
pub fn mark(sky: Sky) -> &'static str {
    match sky {
        Sky::Clear => "🌞",
        Sky::PartlyCloudy => "",
        // Every cloud emoji in the obvious range measures 1 and draws 2, so
        // overcast and fog borrow neighbours that measure honestly: a dark
        // disc for a flat grey sky, and the "foggy" pictograph for fog.
        Sky::Overcast => "🌑",
        Sky::Fog => "🌁",
        Sky::Drizzle | Sky::Rain | Sky::Showers => "",
        Sky::Snow => "",
        Sky::Thunder => "",
        Sky::Unknown => "",
    }
}

/// A short human label for a sky condition.
pub fn describe(sky: Sky) -> &'static str {
    match sky {
        Sky::Clear => "clear",
        Sky::PartlyCloudy => "partly cloudy",
        Sky::Overcast => "overcast",
        Sky::Fog => "fog",
        Sky::Drizzle => "drizzle",
        Sky::Rain => "rain",
        Sky::Showers => "showers",
        Sky::Snow => "snow",
        Sky::Thunder => "thunderstorm",
        Sky::Unknown => "unknown",
    }
}

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

    #[test]
    fn rendered_rows_are_rectangular_at_every_scale() {
        for scale in 1..=4 {
            let big = BigText::new("12:34", scale);
            assert_eq!(big.rows.len(), usize::from(big.height));
            for row in &big.rows {
                assert_eq!(
                    row.chars().count(),
                    usize::from(big.width),
                    "ragged row at scale {scale}"
                );
            }
        }
    }

    #[test]
    fn height_and_width_scale_linearly() {
        let one = BigText::new("00", 1);
        let two = BigText::new("00", 2);
        assert_eq!(two.height, one.height * 2);
        // Width doubles except for the unscaled inter-glyph gap.
        assert_eq!(two.width, one.width * 2 - GAP);
    }

    #[test]
    fn reported_width_matches_what_is_rendered() {
        for text in ["0", "12:34", "23:59:59", ""] {
            for scale in 1..=3 {
                let big = BigText::new(text, scale);
                assert_eq!(width_of(text, scale), big.width, "`{text}` at {scale}");
                if !text.is_empty() {
                    assert_eq!(big.rows[0].chars().count(), usize::from(big.width));
                }
            }
        }
    }

    #[test]
    fn a_scale_of_zero_is_treated_as_one() {
        assert_eq!(BigText::new("1", 0).height, BigText::new("1", 1).height);
        assert_eq!(width_of("1", 0), width_of("1", 1));
    }

    #[test]
    fn empty_input_produces_no_width() {
        let big = BigText::new("", 2);
        assert_eq!(big.width, 0);
        assert_eq!(width_of("", 2), 0);
    }

    #[test]
    fn digits_actually_draw_something() {
        for c in "0123456789".chars() {
            let big = BigText::new(&c.to_string(), 1);
            let filled: usize = big.rows.iter().map(|r| r.matches('').count()).sum();
            assert!(filled > 0, "digit {c} rendered blank");
        }
        // The colon is sparse but non-empty; unknown glyphs are blank by design.
        assert!(BigText::new(":", 1).rows.join("").contains(''));
        assert!(!BigText::new("x", 1).rows.join("").contains(''));
    }

    #[test]
    fn fitting_scale_picks_the_largest_that_fits() {
        let text = "12:34";
        let w2 = width_of(text, 2);
        assert_eq!(fitting_scale(text, w2, 4), Some(2));
        assert_eq!(fitting_scale(text, w2 - 1, 4), Some(1));
        assert_eq!(fitting_scale(text, 0, 4), None);
        // Never exceeds the requested ceiling.
        assert_eq!(fitting_scale(text, 10_000, 3), Some(3));
    }

    #[test]
    fn fitting_scale_result_always_fits() {
        for available in 0..120u16 {
            if let Some(scale) = fitting_scale("12:34", available, 5) {
                assert!(width_of("12:34", scale) <= available);
            }
        }
    }

    #[test]
    fn the_utility_face_uppercases_without_letterspacing() {
        assert_eq!(utility("local"), "LOCAL");
        assert_eq!(utility("next hours"), "NEXT HOURS");
        assert_eq!(utility("a"), "A");
        assert_eq!(utility(""), "");
    }

    #[test]
    fn the_utility_face_collapses_runs_of_whitespace_and_trims() {
        assert_eq!(utility("  due   date  "), "DUE DATE");
    }

    /// The face must not cost width any more. It was letterspaced once, which
    /// more than doubled every label and pushed them out of narrow panels.
    #[test]
    fn the_utility_face_is_never_wider_than_the_text_it_renders() {
        for text in ["load", "per core", "next hours", "session", "due date"] {
            assert_eq!(
                utility(text).chars().count(),
                text.split_whitespace().collect::<Vec<_>>().join(" ").len(),
                "`{text}` changed width"
            );
        }
    }

    #[test]
    fn every_sky_has_rectangular_art_of_the_declared_size() {
        for s in [
            Sky::Clear,
            Sky::PartlyCloudy,
            Sky::Overcast,
            Sky::Fog,
            Sky::Drizzle,
            Sky::Rain,
            Sky::Showers,
            Sky::Snow,
            Sky::Thunder,
            Sky::Unknown,
        ] {
            let rows = art(s);
            assert_eq!(rows.len(), ART_HEIGHT, "{s:?} has the wrong height");
            for row in rows {
                assert_eq!(
                    row.chars().count(),
                    ART_WIDTH,
                    "{s:?} row `{row}` is not {ART_WIDTH} wide"
                );
            }
            assert!(!describe(s).is_empty(), "{s:?} needs a label");
        }
    }

    /// Every sky mark must measure the width it actually draws.
    ///
    /// This is the guard against the whole class of bug where a wide emoji
    /// measured as one cell shunts every later column sideways. If a future
    /// glyph swap breaks this, the table silently misaligns — so it is asserted
    /// rather than left to inspection.
    #[test]
    fn every_sky_mark_has_a_predictable_display_width() {
        use unicode_width::UnicodeWidthStr;
        for s in [
            Sky::Clear,
            Sky::PartlyCloudy,
            Sky::Overcast,
            Sky::Fog,
            Sky::Drizzle,
            Sky::Rain,
            Sky::Showers,
            Sky::Snow,
            Sky::Thunder,
            Sky::Unknown,
        ] {
            let mark = mark(s);
            let width = UnicodeWidthStr::width(mark);
            assert_eq!(
                width, 2,
                "{s:?} mark `{mark}` measures {width}; emoji that measure 1 but \
                 draw 2 break every column after them"
            );
        }
    }

    #[test]
    fn every_code_open_meteo_emits_maps_to_real_art() {
        let codes = [
            0, 1, 2, 3, 45, 48, 51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 71, 73, 75, 77, 80, 81, 82,
            85, 86, 95, 96, 99,
        ];
        for code in codes {
            assert_ne!(sky(code), Sky::Unknown, "code {code} is unclassified");
            assert!(!mark(sky(code)).is_empty(), "code {code} has no mark");
        }
    }

    #[test]
    fn unrecognised_codes_degrade_rather_than_panic() {
        assert_eq!(sky(200), Sky::Unknown);
        assert_eq!(describe(sky(200)), "unknown");
        assert_eq!(mark(sky(200)), "");
    }
}