splashboard 1.4.0

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
use std::sync::OnceLock;
use std::time::Instant;

use ratatui::{Frame, layout::Rect, style::Color};
use tachyonfx::{Duration as FxDuration, Effect, EffectRenderer, EffectTimer, Interpolation, fx};

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

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

#[derive(Debug, Clone, Default, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct Options {
    #[serde(default)]
    pub font_sequence: Option<Vec<String>>,
}

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

const DEFAULT_DURATION_MS: u64 = 1800;

const DEFAULT_SEQUENCE: &[&str] = &["small", "banner", "ansi_shadow"];

const OPTION_SCHEMAS: &[OptionSchema] = &[
    OptionSchema {
        name: "font_sequence",
        type_hint: "array of font names (e.g. [\"small\", \"banner\", \"ansi_shadow\"])",
        required: false,
        default: Some("[\"small\", \"banner\", \"ansi_shadow\"]"),
        description: "Figlet fonts to step through. Each phase renders the text in the next font and quickly crossfades to the one after. The final entry is what the static resting frame shows — pick the font you'd normally use for the hero.",
    },
    OptionSchema {
        name: "duration_ms",
        type_hint: "milliseconds (u64)",
        required: false,
        default: Some("1800"),
        description: "Total morph duration across every phase. Split evenly between phases; kept short so the motion lands inside the 2s ANIMATION_WINDOW.",
    },
    OptionSchema {
        name: "color",
        type_hint: "theme token name (e.g. \"panel_title\", \"text\")",
        required: false,
        default: Some("\"text\""),
        description: "Foreground colour token forwarded to text_ascii. Any `[theme]` key name is accepted.",
    },
    OptionSchema {
        name: "align",
        type_hint: "\"left\" | \"center\" | \"right\"",
        required: false,
        default: Some("\"left\""),
        description: "Horizontal alignment forwarded to text_ascii.",
    },
];

/// Animated hero that steps the text through a sequence of figlet fonts, letting each phase
/// crossfade into the next. The last font is the resting frame — once the window closes the
/// output matches what a plain `text_ascii { style = "figlet", font = <last> }` would produce.
///
/// Delegates rendering to `text_ascii` with a per-phase `font` override, so the same figlet
/// word-wrap + alignment logic is reused. Only `Text` is supported.
pub struct AnimatedFigletMorphRenderer;

impl Renderer for AnimatedFigletMorphRenderer {
    fn name(&self) -> &str {
        "animated_figlet_morph"
    }
    fn description(&self) -> &'static str {
        "Hero text rendered through a sequence of figlet fonts, crossfading from one to the next so the typography visibly reshapes itself before settling on the final font. Accepts `Text` only; the resting frame matches a static `text_ascii` render in the last font of the sequence."
    }
    fn accepts(&self) -> &[Shape] {
        &[Shape::Text]
    }
    fn animates(&self) -> bool {
        true
    }
    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,
    ) {
        render_morph(frame, area, body, opts, theme, registry);
    }
    fn natural_height(
        &self,
        body: &Body,
        opts: &RenderOptions,
        max_width: u16,
        registry: &Registry,
    ) -> u16 {
        let Some(text_ascii) = registry.get("text_ascii") else {
            return 1;
        };
        // Height is whatever the tallest font in the sequence would need — otherwise the
        // widget's row would be sized for the resting font and the earlier phases would clip.
        sequence(opts)
            .iter()
            .map(|font| {
                let forwarded = text_ascii_opts(opts, font.as_str());
                text_ascii.natural_height(body, &forwarded, max_width, registry)
            })
            .max()
            .unwrap_or(1)
    }
}

fn render_morph(
    frame: &mut Frame,
    area: Rect,
    body: &Body,
    opts: &RenderOptions,
    theme: &Theme,
    registry: &Registry,
) {
    let Some(text_ascii) = registry.get("text_ascii") else {
        return;
    };
    let seq = sequence(opts);
    let total = opts.duration_ms.unwrap_or(DEFAULT_DURATION_MS).max(1) as u32;
    let elapsed = elapsed_since_start_ms();
    let (phase, phase_elapsed, phase_len) = phase_for(elapsed, total, seq.len());
    let font = seq[phase].as_str();
    let forwarded = text_ascii_opts(opts, font);
    text_ascii.render(frame, area, body, &forwarded, theme, registry);

    if elapsed >= total {
        return;
    }
    // Crossfade window inside each phase: fade_in at the start, fade_out at the end. The
    // resting phase (last) skips the fade_out so the final frame is a clean static render.
    let fade_ms = phase_len.min(260);
    if phase_elapsed < fade_ms {
        let mut effect = fx::fade_from_fg(
            Color::Black,
            EffectTimer::from_ms(fade_ms, Interpolation::QuadOut),
        );
        apply(frame, area, &mut effect, phase_elapsed);
    } else if phase + 1 < seq.len() && phase_elapsed + fade_ms >= phase_len {
        let mut effect = fx::fade_to_fg(
            Color::Black,
            EffectTimer::from_ms(fade_ms, Interpolation::QuadIn),
        );
        let into_fade = phase_elapsed + fade_ms - phase_len;
        apply(frame, area, &mut effect, into_fade);
    }
}

fn apply(frame: &mut Frame, area: Rect, effect: &mut Effect, elapsed_ms: u32) {
    frame.render_effect(effect, area, FxDuration::from_millis(elapsed_ms));
}

fn sequence(opts: &RenderOptions) -> Vec<String> {
    let specific: Options = opts.parse_specific();
    match specific.font_sequence {
        Some(list) if !list.is_empty() => list,
        _ => DEFAULT_SEQUENCE.iter().map(|s| (*s).to_string()).collect(),
    }
}

fn phase_for(elapsed: u32, total: u32, phase_count: usize) -> (usize, u32, u32) {
    let count = phase_count.max(1) as u32;
    let phase_len = total / count;
    let last_idx = (count - 1) as usize;
    if elapsed >= total || phase_len == 0 {
        return (last_idx, 0, phase_len.max(1));
    }
    let idx = (elapsed / phase_len).min(count - 1) as usize;
    let phase_elapsed = elapsed - (idx as u32) * phase_len;
    (idx, phase_elapsed, phase_len)
}

fn text_ascii_opts(opts: &RenderOptions, font: &str) -> RenderOptions {
    RenderOptions {
        style: Some("figlet".into()),
        color: opts.color.clone(),
        align: opts.align.clone(),
        ..RenderOptions::default()
    }
    .with_extra("font", font)
}

fn elapsed_since_start_ms() -> u32 {
    let elapsed = process_start().elapsed().as_millis();
    elapsed.min(u32::MAX as u128) as u32
}

fn process_start() -> Instant {
    static START: OnceLock<Instant> = OnceLock::new();
    *START.get_or_init(Instant::now)
}

#[cfg(test)]
mod tests {
    use std::thread;
    use std::time::Duration;

    use ratatui::{Terminal, backend::TestBackend};

    use super::*;
    use crate::payload::{Payload, TextData};
    use crate::render::{
        RenderSpec,
        test_utils::{line_text, render_to_buffer_with_spec},
    };
    use crate::theme::Theme;

    fn text_body() -> Body {
        Body::Text(TextData { value: "hi".into() })
    }

    #[test]
    fn renderer_contract_exposes_description_theme_keys_and_options() {
        let renderer = AnimatedFigletMorphRenderer;
        assert!(renderer.description().contains("crossfading"));
        assert_eq!(
            renderer
                .color_keys()
                .iter()
                .map(|key| key.name)
                .collect::<Vec<_>>(),
            vec!["text", "panel_title"]
        );
        assert_eq!(
            renderer
                .option_schemas()
                .iter()
                .map(|schema| schema.name)
                .collect::<Vec<_>>(),
            vec!["font_sequence", "duration_ms", "color", "align"]
        );
    }

    #[test]
    fn phase_for_picks_last_on_overflow() {
        let (idx, _, _) = phase_for(5_000, 1_500, 3);
        assert_eq!(idx, 2);
    }

    #[test]
    fn phase_for_returns_start_of_first_phase() {
        let (idx, elapsed, len) = phase_for(0, 900, 3);
        assert_eq!(idx, 0);
        assert_eq!(elapsed, 0);
        assert_eq!(len, 300);
    }

    #[test]
    fn phase_for_advances_on_boundary() {
        let (idx, _, _) = phase_for(310, 900, 3);
        assert_eq!(idx, 1);
    }

    #[test]
    fn phase_for_guards_zero_length_phases() {
        assert_eq!(phase_for(0, 2, 3), (2, 0, 1));
    }

    #[test]
    fn sequence_falls_back_to_default_when_empty() {
        let opts = RenderOptions::default().with_extra("font_sequence", Vec::<String>::new());
        let want: Vec<String> = DEFAULT_SEQUENCE.iter().map(|s| (*s).to_string()).collect();
        assert_eq!(sequence(&opts), want);
    }

    #[test]
    fn sequence_uses_custom_font_sequence_when_present() {
        let opts = RenderOptions::default().with_extra("font_sequence", vec!["small", "doom"]);
        assert_eq!(
            sequence(&opts),
            vec!["small".to_string(), "doom".to_string()]
        );
    }

    #[test]
    fn natural_height_returns_one_without_text_ascii() {
        let renderer = AnimatedFigletMorphRenderer;
        assert_eq!(
            renderer.natural_height(
                &text_body(),
                &RenderOptions::default(),
                60,
                &Registry::default()
            ),
            1
        );
    }

    #[test]
    fn natural_height_uses_tallest_font_in_sequence() {
        let renderer = AnimatedFigletMorphRenderer;
        let registry = Registry::with_builtins();
        let opts = RenderOptions::default().with_extra("font_sequence", vec!["small", "banner"]);
        let text_ascii = registry.get("text_ascii").unwrap();
        let expected = ["small", "banner"]
            .into_iter()
            .map(|font| {
                text_ascii.natural_height(
                    &text_body(),
                    &text_ascii_opts(&opts, font),
                    60,
                    &registry,
                )
            })
            .max()
            .unwrap();
        assert_eq!(
            renderer.natural_height(&text_body(), &opts, 60, &registry),
            expected
        );
    }

    #[test]
    fn text_ascii_opts_forward_common_fields_and_font() {
        let opts = RenderOptions {
            align: Some("center".into()),
            color: Some("panel_title".into()),
            ..RenderOptions::default()
        };
        let forwarded = text_ascii_opts(&opts, "doom");
        assert_eq!(forwarded.style.as_deref(), Some("figlet"));
        assert_eq!(forwarded.align.as_deref(), Some("center"));
        assert_eq!(forwarded.color.as_deref(), Some("panel_title"));
        assert_eq!(forwarded.extra_str("font"), Some("doom"));
    }

    #[test]
    fn render_returns_early_when_registry_lacks_text_ascii() {
        let body = text_body();
        let mut terminal = Terminal::new(TestBackend::new(20, 4)).unwrap();
        terminal
            .draw(|frame| {
                render_morph(
                    frame,
                    frame.area(),
                    &body,
                    &RenderOptions::default(),
                    &Theme::default(),
                    &Registry::default(),
                );
            })
            .unwrap();
        let buffer = terminal.backend().buffer().clone();
        assert!(line_text(&buffer, 0).trim().is_empty());
    }

    #[test]
    fn render_skips_crossfade_after_duration_has_elapsed() {
        process_start();
        thread::sleep(Duration::from_millis(2));
        let payload = Payload {
            icon: None,
            status: None,
            format: None,
            body: text_body(),
        };
        let spec = RenderSpec::Full {
            type_name: "animated_figlet_morph".into(),
            options: RenderOptions {
                duration_ms: Some(1),
                ..RenderOptions::default()
            },
        };
        let registry = super::super::Registry::with_builtins();
        let buffer = render_to_buffer_with_spec(&payload, Some(&spec), &registry, 60, 14);
        let has_visible_text =
            (0..buffer.area.height).any(|y| !line_text(&buffer, y).trim().is_empty());
        assert!(has_visible_text);
    }

    #[test]
    fn render_applies_fade_in_at_start_of_a_phase() {
        let body = text_body();
        let registry = Registry::with_builtins();
        let theme = Theme::default();
        let elapsed = elapsed_since_start_ms().max(1) as u64;
        let duration_ms = elapsed * DEFAULT_SEQUENCE.len() as u64;
        let opts = RenderOptions {
            duration_ms: Some(duration_ms),
            ..RenderOptions::default()
        };
        let mut terminal = Terminal::new(TestBackend::new(60, 14)).unwrap();
        terminal
            .draw(|frame| {
                render_morph(frame, frame.area(), &body, &opts, &theme, &registry);
            })
            .unwrap();
        let buffer = terminal.backend().buffer().clone();
        assert!((0..buffer.area.height).any(|y| !line_text(&buffer, y).trim().is_empty()));
    }

    #[test]
    fn render_applies_fade_out_near_end_of_a_phase() {
        let body = text_body();
        let registry = Registry::with_builtins();
        let theme = Theme::default();
        let elapsed = elapsed_since_start_ms().max(1);
        let phase_len = elapsed.saturating_add(200);
        let duration_ms = phase_len as u64 * DEFAULT_SEQUENCE.len() as u64;
        let opts = RenderOptions {
            duration_ms: Some(duration_ms),
            ..RenderOptions::default()
        };
        let mut terminal = Terminal::new(TestBackend::new(60, 14)).unwrap();
        terminal
            .draw(|frame| {
                render_morph(frame, frame.area(), &body, &opts, &theme, &registry);
            })
            .unwrap();
        let buffer = terminal.backend().buffer().clone();
        assert!((0..buffer.area.height).any(|y| !line_text(&buffer, y).trim().is_empty()));
    }
}