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
//! Bundled 16x16 weather sprites for the `weather` fetcher's `PixelArt` shape. Sprites are
//! authored as ASCII grids — one character per pixel — paired with a palette mapping each
//! character to an RGBA colour. Decoding happens once on first use and the resulting
//! [`PixelArtData`] values are cached in process for the lifetime of the binary.
//!
//! Why ASCII-in-source vs. bundled PNG: the diff stays text-only, sprite edits show up cleanly
//! in PR review, and there is no PNG decode at runtime beyond the constant-time palette lookup
//! used here.

use std::collections::HashMap;
use std::sync::OnceLock;

use crate::payload::{PixelArtData, PixelColor};

use super::common::weather_description;

pub const SPRITE_SIZE: usize = 16;

pub struct SpriteCatalog {
    pub clear: PixelArtData,
    pub mostly_clear: PixelArtData,
    pub partly_cloudy: PixelArtData,
    pub overcast: PixelArtData,
    pub fog: PixelArtData,
    pub rain: PixelArtData,
    pub snow: PixelArtData,
    pub thunderstorm: PixelArtData,
}

pub fn catalog() -> &'static SpriteCatalog {
    static CATALOG: OnceLock<SpriteCatalog> = OnceLock::new();
    CATALOG.get_or_init(build_catalog)
}

/// Pick the sprite matching a WMO code (Open-Meteo's standard table). Falls back to `overcast`
/// for codes outside the bundled set so the renderer always has something to draw.
pub fn sprite_for_code(code: u16) -> PixelArtData {
    let c = catalog();
    let key = match code {
        0 => &c.clear,
        1 => &c.mostly_clear,
        2 => &c.partly_cloudy,
        3 => &c.overcast,
        45 | 48 => &c.fog,
        51..=57 | 61..=67 | 80..=82 => &c.rain,
        71..=77 | 85 | 86 => &c.snow,
        95..=99 => &c.thunderstorm,
        _ => &c.overcast,
    };
    let mut sprite = key.clone();
    let (_, label) = weather_description(code);
    sprite.label = Some(label.into());
    sprite
}

fn build_catalog() -> SpriteCatalog {
    SpriteCatalog {
        clear: decode(SPRITE_CLEAR, palette_clear()),
        mostly_clear: decode(SPRITE_MOSTLY_CLEAR, palette_mostly_clear()),
        partly_cloudy: decode(SPRITE_PARTLY_CLOUDY, palette_partly_cloudy()),
        overcast: decode(SPRITE_OVERCAST, palette_overcast()),
        fog: decode(SPRITE_FOG, palette_fog()),
        rain: decode(SPRITE_RAIN, palette_rain()),
        snow: decode(SPRITE_SNOW, palette_snow()),
        thunderstorm: decode(SPRITE_THUNDERSTORM, palette_thunderstorm()),
    }
}

fn decode(grid: &[&str], palette: HashMap<char, PixelColor>) -> PixelArtData {
    let pixels = grid
        .iter()
        .map(|row| {
            row.chars()
                .map(|c| palette.get(&c).copied().unwrap_or(PixelColor::TRANSPARENT))
                .collect()
        })
        .collect();
    PixelArtData {
        pixels,
        label: None,
    }
}

// ---- Palette helpers ----

const SUN_RIM: PixelColor = PixelColor::opaque(255, 213, 79);
const SUN_CORE: PixelColor = PixelColor::opaque(255, 167, 38);
const CLOUD_EDGE: PixelColor = PixelColor::opaque(189, 195, 199);
const CLOUD_FILL: PixelColor = PixelColor::opaque(236, 240, 241);
const CLOUD_DARK: PixelColor = PixelColor::opaque(127, 140, 141);
const RAINDROP: PixelColor = PixelColor::opaque(52, 152, 219);
const SNOW: PixelColor = PixelColor::opaque(245, 245, 245);
const BOLT: PixelColor = PixelColor::opaque(255, 235, 59);
const FOG: PixelColor = PixelColor::opaque(176, 190, 197);

fn palette_clear() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('Y', SUN_RIM),
        ('O', SUN_CORE),
    ])
}

fn palette_mostly_clear() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('Y', SUN_RIM),
        ('O', SUN_CORE),
        ('c', CLOUD_EDGE),
        ('w', CLOUD_FILL),
    ])
}

fn palette_partly_cloudy() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('Y', SUN_RIM),
        ('O', SUN_CORE),
        ('c', CLOUD_EDGE),
        ('w', CLOUD_FILL),
    ])
}

fn palette_overcast() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('c', CLOUD_EDGE),
        ('w', CLOUD_FILL),
        ('d', CLOUD_DARK),
    ])
}

fn palette_fog() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('f', FOG),
        ('w', CLOUD_FILL),
    ])
}

fn palette_rain() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('c', CLOUD_EDGE),
        ('w', CLOUD_FILL),
        ('d', CLOUD_DARK),
        ('r', RAINDROP),
    ])
}

fn palette_snow() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('c', CLOUD_EDGE),
        ('w', CLOUD_FILL),
        ('d', CLOUD_DARK),
        ('s', SNOW),
    ])
}

fn palette_thunderstorm() -> HashMap<char, PixelColor> {
    HashMap::from([
        ('.', PixelColor::TRANSPARENT),
        ('c', CLOUD_EDGE),
        ('w', CLOUD_FILL),
        ('d', CLOUD_DARK),
        ('b', BOLT),
    ])
}

// ---- Sprite grids (16x16) ----

const SPRITE_CLEAR: &[&str] = &[
    "................",
    "................",
    ".....YYYYYY.....",
    "...YYYOOOOYYY...",
    "..YYOOOOOOOOYY..",
    "..YOOOOOOOOOOY..",
    ".YOOOOOOOOOOOOY.",
    ".YOOOOOOOOOOOOY.",
    ".YOOOOOOOOOOOOY.",
    ".YOOOOOOOOOOOOY.",
    "..YOOOOOOOOOOY..",
    "..YYOOOOOOOOYY..",
    "...YYYOOOOYYY...",
    ".....YYYYYY.....",
    "................",
    "................",
];

const SPRITE_MOSTLY_CLEAR: &[&str] = &[
    "................",
    "....YYYY........",
    "...YYOOYY.......",
    "..YYOOOOYY......",
    "..YOOOOOOY......",
    "..YOOOOOOY......",
    "...YOOOOY..ccc..",
    "....YYYY..cwwwc.",
    "........ccwwwwc.",
    "........cwwwwwwc",
    ".........ccwwwc.",
    "..........ccc...",
    "................",
    "................",
    "................",
    "................",
];

const SPRITE_PARTLY_CLOUDY: &[&str] = &[
    "................",
    "................",
    "....cccc........",
    "...cwwwwc.......",
    "..cwwwwwwc......",
    ".cwwwwwwwwc.....",
    ".cwwwwwwwwc.....",
    ".cwwwwwwwwc.....",
    "..cwwwwwwwccc...",
    "...cccccccwwwc..",
    "........cwwwwwc.",
    "........cwwwwwc.",
    ".........cwwwc..",
    "..........ccc...",
    "................",
    "................",
];

const SPRITE_OVERCAST: &[&str] = &[
    "................",
    "................",
    "....cccccc......",
    "...cwwwwwwc.....",
    "..cwwwwwwwwc....",
    ".cwwwwwwwwwwc...",
    ".cwwwwwwwwwwc...",
    ".cwwwwwwwwwwccc.",
    "..cccccccccwwwc.",
    "...ddddddcwwwwc.",
    "..ddwwwwwwwwwwc.",
    ".ddwwwwwwwwwwc..",
    ".dwwwwwwwwwwc...",
    ".ddwwwwwwwwc....",
    "..dddddddddd....",
    "................",
];

// Bands are two pixel rows thick so each one packs into a single half-block cell as solid
// fog rather than a fg/bg pinstripe. Horizontal indent varies per band for a drifting feel.
const SPRITE_FOG: &[&str] = &[
    "................",
    "................",
    "..fffffffffff...",
    "..fffffffffff...",
    "................",
    "................",
    ".fffffffffffff..",
    ".fffffffffffff..",
    "................",
    "................",
    "...fffffffffff..",
    "...fffffffffff..",
    "................",
    "................",
    ".fffffffffffff..",
    ".fffffffffffff..",
];

const SPRITE_RAIN: &[&str] = &[
    "................",
    "....cccccc......",
    "...cwwwwwwc.....",
    "..cwwwwwwwwcc...",
    ".cwwwwwwwwwwc...",
    ".cwwwwwwwwwwc...",
    ".dddddddddddd...",
    "................",
    "..r..r..r..r....",
    ".r..r..r..r..r..",
    "................",
    "..r..r..r..r....",
    ".r..r..r..r..r..",
    "................",
    "..r..r..r..r....",
    "................",
];

const SPRITE_SNOW: &[&str] = &[
    "................",
    "....cccccc......",
    "...cwwwwwwc.....",
    "..cwwwwwwwwcc...",
    ".cwwwwwwwwwwc...",
    ".cwwwwwwwwwwc...",
    ".dddddddddddd...",
    "................",
    "..s..s..s..s....",
    ".s..s..s..s..s..",
    "................",
    "..s..s..s..s....",
    ".s..s..s..s..s..",
    "................",
    "..s..s..s..s....",
    "................",
];

const SPRITE_THUNDERSTORM: &[&str] = &[
    "................",
    "....cccccc......",
    "...cwwwwwwc.....",
    "..cwwwwwwwwcc...",
    ".cwwwwwwwwwwc...",
    ".cwwwwwwwwwwc...",
    ".dddddddddddd...",
    "................",
    "......bbbb......",
    ".....bbbb.......",
    "....bbbb........",
    "...bbbbbbb......",
    "......bbb.......",
    ".....bb.........",
    "....bb..........",
    "................",
];

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

    fn assert_grid_is_16x16(grid: &[&str]) {
        assert_eq!(grid.len(), SPRITE_SIZE, "row count");
        for (i, row) in grid.iter().enumerate() {
            assert_eq!(row.chars().count(), SPRITE_SIZE, "row {i} width");
        }
    }

    #[test]
    fn every_sprite_grid_is_16x16() {
        for grid in [
            SPRITE_CLEAR,
            SPRITE_MOSTLY_CLEAR,
            SPRITE_PARTLY_CLOUDY,
            SPRITE_OVERCAST,
            SPRITE_FOG,
            SPRITE_RAIN,
            SPRITE_SNOW,
            SPRITE_THUNDERSTORM,
        ] {
            assert_grid_is_16x16(grid);
        }
    }

    #[test]
    fn decoded_catalog_has_uniform_dimensions() {
        let c = catalog();
        for sprite in [
            &c.clear,
            &c.mostly_clear,
            &c.partly_cloudy,
            &c.overcast,
            &c.fog,
            &c.rain,
            &c.snow,
            &c.thunderstorm,
        ] {
            assert_eq!(sprite.pixels.len(), SPRITE_SIZE);
            for row in &sprite.pixels {
                assert_eq!(row.len(), SPRITE_SIZE);
            }
        }
    }

    #[test]
    fn sprite_for_code_maps_known_wmo_buckets() {
        // Spot-check one code per bucket; weather_description's bucket table is the source of
        // truth and is itself tested in common.rs.
        for (code, expected_label) in [
            (0u16, "clear"),
            (1, "mostly clear"),
            (2, "partly cloudy"),
            (3, "overcast"),
            (45, "fog"),
            (61, "rain"),
            (71, "snow"),
            (95, "thunderstorm"),
        ] {
            let s = sprite_for_code(code);
            assert_eq!(s.label.as_deref(), Some(expected_label), "WMO {code}");
        }
    }

    #[test]
    fn unknown_code_falls_back_to_overcast_sprite() {
        let s = sprite_for_code(7777);
        // Same pixel grid as the overcast sprite.
        assert_eq!(s.pixels, catalog().overcast.pixels);
    }

    #[test]
    fn palette_characters_only_use_declared_keys() {
        // Walks every grid and confirms every non-transparent glyph has a palette entry. Catches
        // typos like accidentally using `0` instead of `O` in a sprite.
        let cases: &[(&[&str], HashMap<char, PixelColor>)] = &[
            (SPRITE_CLEAR, palette_clear()),
            (SPRITE_MOSTLY_CLEAR, palette_mostly_clear()),
            (SPRITE_PARTLY_CLOUDY, palette_partly_cloudy()),
            (SPRITE_OVERCAST, palette_overcast()),
            (SPRITE_FOG, palette_fog()),
            (SPRITE_RAIN, palette_rain()),
            (SPRITE_SNOW, palette_snow()),
            (SPRITE_THUNDERSTORM, palette_thunderstorm()),
        ];
        for (grid, palette) in cases {
            for (y, row) in grid.iter().enumerate() {
                for (x, ch) in row.chars().enumerate() {
                    assert!(
                        palette.contains_key(&ch),
                        "char {ch:?} at ({x},{y}) has no palette entry"
                    );
                }
            }
        }
    }
}