tui-skeleton 0.3.0

Animated skeleton loading widgets for Ratatui
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use std::time::Instant;

use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Color, Style};
use ratatui::text::Line;
use ratatui::widgets::{Paragraph, Row, Table, Widget};
use ratatui_core::buffer::Buffer;
use tui_pantry::Ingredient;

use crate::{
    AnimationMode, SkeletonBarChart, SkeletonBrailleBar, SkeletonHBarChart, SkeletonList,
    SkeletonTable, SkeletonText,
};

/// Full cycle: 5s skeleton, 5s content, repeat.
const PHASE_MS: u64 = 5000;
const CYCLE_MS: u64 = PHASE_MS * 2;

pub fn ingredients() -> Vec<Box<dyn Ingredient>> {
    let modes: &[(AnimationMode, bool, &str)] = &[
        (AnimationMode::Breathe, false, "Breathe (default)"),
        (AnimationMode::Sweep, false, "Sweep"),
        (AnimationMode::Plasma, false, "Plasma"),
        (AnimationMode::Noise, false, "Noise"),
        (AnimationMode::Breathe, true, "Braille Breathe"),
        (AnimationMode::Sweep, true, "Braille Sweep"),
        (AnimationMode::Plasma, true, "Braille Plasma"),
    ];

    let mut out: Vec<Box<dyn Ingredient>> = Vec::new();

    for &(mode, braille, name) in modes {
        out.push(Box::new(LoadingTable::new(mode, braille, name)));
        out.push(Box::new(LoadingArticle::new(mode, braille, name)));
        out.push(Box::new(LoadingSidebar::new(mode, braille, name)));
        out.push(Box::new(LoadingDashboard::new(mode, braille, name)));
        out.push(Box::new(LoadingGauges::new(mode, braille, name)));
    }

    out
}

fn elapsed_ms(epoch: Instant) -> u64 {
    epoch.elapsed().as_millis() as u64
}

fn is_skeleton_phase(elapsed: u64) -> bool {
    (elapsed % CYCLE_MS) < PHASE_MS
}

// ── Loading Table ──
//
// Header row is always visible — only row data is unknown.

const TABLE_COLS: [Constraint; 4] = [
    Constraint::Percentage(25),
    Constraint::Percentage(25),
    Constraint::Percentage(25),
    Constraint::Percentage(25),
];

const TABLE_HEADER: [&str; 4] = ["Node", "Region", "CPU", "Status"];

struct LoadingTable {
    epoch: Instant,
    mode: AnimationMode,
    braille: bool,
    variant: &'static str,
}

impl LoadingTable {
    fn new(mode: AnimationMode, braille: bool, variant: &'static str) -> Self {
        Self {
            epoch: Instant::now(),
            mode,
            braille,
            variant,
        }
    }
}

impl Ingredient for LoadingTable {
    fn tab(&self) -> &str {
        "Panes"
    }
    fn group(&self) -> &str {
        "Data Table"
    }
    fn name(&self) -> &str {
        self.variant
    }
    fn source(&self) -> &str {
        "tui_skeleton::use_cases"
    }
    fn description(&self) -> &str {
        "Header always visible; row data replaced by skeleton during loading."
    }
    fn animated(&self) -> bool {
        true
    }

    fn render(&self, area: Rect, buf: &mut Buffer) {
        let ms = elapsed_ms(self.epoch);
        let header = Row::new(TABLE_HEADER).style(Style::new().bold());

        if is_skeleton_phase(ms) {
            // Header is known; skeleton fills the data rows below it.
            let [header_area, body_area] =
                Layout::vertical([Constraint::Length(1), Constraint::Fill(1)]).areas(area);

            Table::new(std::iter::empty::<Row>(), TABLE_COLS)
                .header(header)
                .render(header_area, buf);

            SkeletonTable::new(ms)
                .mode(self.mode)
                .braille(self.braille)
                .columns(&TABLE_COLS)
                .rows(5)
                .render(body_area, buf);
        } else {
            let rows = [
                Row::new(["use1a", "us-east-1a", "34%", "Online"]),
                Row::new(["usw2b", "us-west-2b", "61%", "Online"]),
                Row::new(["euc1", "eu-central-1", "88%", "Degraded"]),
                Row::new(["aps1", "ap-south-1", "22%", "Online"]),
                Row::new(["euw1", "eu-west-1", "", "Offline"]),
            ];

            Table::new(rows, TABLE_COLS)
                .header(header)
                .render(area, buf);
        }
    }
}

// ── Loading Article ──
//
// Title is always visible — body text is unknown.

const ARTICLE_WIDTHS: &[f32] = &[1.0, 1.0, 0.85, 1.0, 0.6];

struct LoadingArticle {
    epoch: Instant,
    mode: AnimationMode,
    braille: bool,
    variant: &'static str,
}

impl LoadingArticle {
    fn new(mode: AnimationMode, braille: bool, variant: &'static str) -> Self {
        Self {
            epoch: Instant::now(),
            mode,
            braille,
            variant,
        }
    }
}

impl Ingredient for LoadingArticle {
    fn tab(&self) -> &str {
        "Panes"
    }
    fn group(&self) -> &str {
        "Article"
    }
    fn name(&self) -> &str {
        self.variant
    }
    fn source(&self) -> &str {
        "tui_skeleton::use_cases"
    }
    fn description(&self) -> &str {
        "Title always visible; body text replaced by skeleton during loading."
    }
    fn animated(&self) -> bool {
        true
    }

    fn render(&self, area: Rect, buf: &mut Buffer) {
        let ms = elapsed_ms(self.epoch);
        let constrained = Rect::new(area.x, area.y, area.width.min(52), area.height.min(7));

        let [title_area, _, body_area] = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Fill(1),
        ])
        .areas(constrained);

        Line::from("Lorem Ipsum")
            .style(Style::new().bold())
            .render(title_area, buf);

        if is_skeleton_phase(ms) {
            SkeletonText::new(ms)
                .mode(self.mode)
                .braille(self.braille)
                .line_widths(ARTICLE_WIDTHS)
                .render(body_area, buf);
        } else {
            Paragraph::new(vec![
                Line::from("Dolor sit amet, consectetur adipiscing elit, sed"),
                Line::from("do eiusmod tempor incididunt ut labore et dolore"),
                Line::from("magna aliqua. Ut enim ad minim veniam,"),
                Line::from("quis nostrud exercitation ullamco laboris nisi"),
                Line::from("ut aliquip ex ea commodo."),
            ])
            .render(body_area, buf);
        }
    }
}

// ── Loading Sidebar ──
//
// Section header is always visible — menu items are the data.

struct LoadingSidebar {
    epoch: Instant,
    mode: AnimationMode,
    braille: bool,
    variant: &'static str,
}

impl LoadingSidebar {
    fn new(mode: AnimationMode, braille: bool, variant: &'static str) -> Self {
        Self {
            epoch: Instant::now(),
            mode,
            braille,
            variant,
        }
    }
}

impl Ingredient for LoadingSidebar {
    fn tab(&self) -> &str {
        "Panes"
    }
    fn group(&self) -> &str {
        "Sidebar"
    }
    fn name(&self) -> &str {
        self.variant
    }
    fn source(&self) -> &str {
        "tui_skeleton::use_cases"
    }
    fn description(&self) -> &str {
        "Section header always visible; menu items replaced by skeleton during loading."
    }
    fn animated(&self) -> bool {
        true
    }

    fn render(&self, area: Rect, buf: &mut Buffer) {
        let ms = elapsed_ms(self.epoch);

        let [header_area, _, body_area] = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Fill(1),
        ])
        .areas(area);

        Line::from("Navigation")
            .style(Style::new().bold())
            .render(header_area, buf);

        if is_skeleton_phase(ms) {
            SkeletonList::new(ms)
                .mode(self.mode)
                .braille(self.braille)
                .items(5)
                .render(body_area, buf);
        } else {
            let items = ["Dashboard", "Network", "Models", "Logs", "Settings"];

            for (i, item) in items.iter().enumerate() {
                let y = body_area.y + (i as u16) * 2;

                if y >= body_area.bottom() {
                    break;
                }

                Line::from(*item).render(Rect::new(body_area.x, y, body_area.width, 1), buf);
            }
        }
    }
}

// ── Loading Dashboard (composite) ──
//
// Layout structure, bar labels, KV keys, and table header are always
// visible. Chart bars, KV values, and table rows are unknown.

const DASHBOARD_TABLE_COLS: [Constraint; 3] = [
    Constraint::Percentage(30),
    Constraint::Percentage(40),
    Constraint::Percentage(30),
];

const DASHBOARD_HEADER: [&str; 3] = ["Node", "Region", "Status"];

const BAR_LABELS: [&str; 6] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const KV_KEYS: [&str; 4] = ["Nodes", "Uptime", "Jobs", "Memory"];

struct LoadingDashboard {
    epoch: Instant,
    mode: AnimationMode,
    braille: bool,
    variant: &'static str,
}

impl LoadingDashboard {
    fn new(mode: AnimationMode, braille: bool, variant: &'static str) -> Self {
        Self {
            epoch: Instant::now(),
            mode,
            braille,
            variant,
        }
    }
}

impl Ingredient for LoadingDashboard {
    fn tab(&self) -> &str {
        "Panes"
    }
    fn group(&self) -> &str {
        "Dashboard"
    }
    fn name(&self) -> &str {
        self.variant
    }
    fn source(&self) -> &str {
        "tui_skeleton::use_cases"
    }
    fn description(&self) -> &str {
        "Layout chrome always visible; data regions replaced by skeletons during loading."
    }
    fn animated(&self) -> bool {
        true
    }

    fn render(&self, area: Rect, buf: &mut Buffer) {
        let ms = elapsed_ms(self.epoch);

        let [top, bottom] =
            Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)]).areas(area);

        let [chart_area, kv_area] =
            Layout::horizontal([Constraint::Percentage(60), Constraint::Percentage(40)]).areas(top);

        let [table_area] = Layout::horizontal([Constraint::Percentage(100)]).areas(bottom);

        if is_skeleton_phase(ms) {
            // Bar chart: labels on x-axis are known, bars are skeleton.
            let [bars_area, labels_area] =
                Layout::vertical([Constraint::Fill(1), Constraint::Length(1)]).areas(chart_area);

            SkeletonBarChart::new(ms)
                .mode(self.mode)
                .braille(self.braille)
                .render(bars_area, buf);

            render_bar_labels(labels_area, buf);

            // KV table: keys are known, values are skeleton.
            render_kv_skeleton(kv_area, buf, ms, self.mode, self.braille);

            // Table: header is known, rows are skeleton.
            let [header_area, body_area] =
                Layout::vertical([Constraint::Length(1), Constraint::Fill(1)]).areas(table_area);

            Table::new(std::iter::empty::<Row>(), DASHBOARD_TABLE_COLS)
                .header(Row::new(DASHBOARD_HEADER).style(Style::new().bold()))
                .render(header_area, buf);

            SkeletonTable::new(ms)
                .mode(self.mode)
                .braille(self.braille)
                .columns(&DASHBOARD_TABLE_COLS)
                .rows(4)
                .render(body_area, buf);
        } else {
            render_simple_bars(chart_area, buf);
            render_simple_kv(kv_area, buf);

            let header = Row::new(DASHBOARD_HEADER).style(Style::new().bold());
            let rows = [
                Row::new(["use1a", "us-east-1a", "Online"]),
                Row::new(["usw2b", "us-west-2b", "Online"]),
                Row::new(["euc1", "eu-central-1", "Degraded"]),
                Row::new(["euw1", "eu-west-1", "Offline"]),
            ];

            Table::new(rows, DASHBOARD_TABLE_COLS)
                .header(header)
                .render(table_area, buf);
        }
    }
}

// ── Loading Gauges (braille bars) ──
//
// Labels are always visible — only the bar data is unknown.

const GAUGE_LABELS: [&str; 4] = ["CPU", "Memory", "Disk", "Network"];
const GAUGE_VALUES: [f32; 4] = [0.62, 0.84, 0.38, 0.51];
const GAUGE_LABEL_WIDTH: u16 = 8;

struct LoadingGauges {
    epoch: Instant,
    mode: AnimationMode,
    braille: bool,
    variant: &'static str,
}

impl LoadingGauges {
    fn new(mode: AnimationMode, braille: bool, variant: &'static str) -> Self {
        Self {
            epoch: Instant::now(),
            mode,
            braille,
            variant,
        }
    }
}

impl Ingredient for LoadingGauges {
    fn tab(&self) -> &str {
        "Panes"
    }
    fn group(&self) -> &str {
        "Gauges"
    }
    fn name(&self) -> &str {
        self.variant
    }
    fn source(&self) -> &str {
        "tui_skeleton::use_cases"
    }
    fn description(&self) -> &str {
        "Labels always visible; gauge bars replaced by skeleton during loading."
    }
    fn animated(&self) -> bool {
        true
    }

    fn render(&self, area: Rect, buf: &mut Buffer) {
        let ms = elapsed_ms(self.epoch);

        for (i, &label) in GAUGE_LABELS.iter().enumerate() {
            let y = area.y + (i as u16) * 2;

            if y >= area.bottom() {
                break;
            }

            // Label is always visible.
            Line::from(format!("{label:<8}"))
                .style(Style::new().bold())
                .render(
                    Rect::new(area.x, y, GAUGE_LABEL_WIDTH.min(area.width), 1),
                    buf,
                );

            let bar_x = area.x + GAUGE_LABEL_WIDTH;
            let bar_width = area.width.saturating_sub(GAUGE_LABEL_WIDTH);

            if bar_width == 0 {
                continue;
            }

            let bar_area = Rect::new(bar_x, y, bar_width, 1);

            if is_skeleton_phase(ms) {
                let use_braille = self.braille || self.mode == AnimationMode::Noise;

                if use_braille {
                    SkeletonBrailleBar::new(ms)
                        .mode(self.mode)
                        .bars(1)
                        .fills(&[1.0])
                        .render(bar_area, buf);
                } else {
                    SkeletonHBarChart::new(ms)
                        .mode(self.mode)
                        .bars(1)
                        .bar_height(1)
                        .widths(&[1.0])
                        .render(bar_area, buf);
                }
            } else {
                render_braille_gauge(bar_area, buf, GAUGE_VALUES[i]);
            }
        }
    }
}

// ── Helpers ──

fn render_bar_labels(area: Rect, buf: &mut Buffer) {
    let bar_width = 3u16;
    let stride = bar_width + 1;

    for (i, label) in BAR_LABELS.iter().enumerate() {
        let x = area.x + (i as u16) * stride;

        if x + bar_width > area.right() {
            break;
        }

        for (ci, ch) in label.chars().take(bar_width as usize).enumerate() {
            buf[(x + ci as u16, area.y)].set_char(ch);
        }
    }
}

/// Varying value widths so KV skeletons look like different-length text.
const KV_VALUE_WIDTHS: [f32; 4] = [0.25, 0.55, 0.20, 0.45];

fn render_kv_skeleton(area: Rect, buf: &mut Buffer, ms: u64, mode: AnimationMode, braille: bool) {
    let key_width = 8u16;
    let value_start = key_width + 2;

    for (i, &key) in KV_KEYS.iter().enumerate() {
        let y = area.y + (i as u16) * 2;

        if y >= area.bottom() {
            break;
        }

        // Key is always visible.
        Line::from(format!("{key}:"))
            .style(Style::new().bold())
            .render(Rect::new(area.x, y, key_width, 1), buf);

        // Value is skeleton with varying width.
        let val_x = area.x + value_start;
        let val_width = area.width.saturating_sub(value_start);

        if val_width > 0 {
            let frac = KV_VALUE_WIDTHS[i % KV_VALUE_WIDTHS.len()];

            SkeletonHBarChart::new(ms)
                .mode(mode)
                .braille(braille)
                .bars(1)
                .bar_height(1)
                .widths(&[frac])
                .render(Rect::new(val_x, y, val_width, 1), buf);
        }
    }
}

fn render_simple_bars(area: Rect, buf: &mut Buffer) {
    let values = [60u16, 85, 45, 95, 70, 55];
    let max = values.iter().copied().max().unwrap_or(1);
    let bar_width = 3u16;
    let stride = bar_width + 1;

    let [bars_area, labels_area] =
        Layout::vertical([Constraint::Fill(1), Constraint::Length(1)]).areas(area);

    for (i, (label, &val)) in BAR_LABELS.iter().zip(&values).enumerate() {
        let x = bars_area.x + (i as u16) * stride;

        if x + bar_width > bars_area.right() {
            break;
        }

        let bar_height = ((bars_area.height as u32) * val as u32 / max as u32) as u16;
        let bar_top = bars_area.y + bars_area.height - bar_height;

        for dy in 0..bar_height {
            for dx in 0..bar_width {
                buf[(x + dx, bar_top + dy)]
                    .set_char('')
                    .set_style(Style::default().fg(Color::Rgb(74, 222, 128)));
            }
        }

        for (ci, ch) in label.chars().take(bar_width as usize).enumerate() {
            buf[(x + ci as u16, labels_area.y)].set_char(ch);
        }
    }
}

fn render_simple_kv(area: Rect, buf: &mut Buffer) {
    let pairs = [
        ("Nodes", "6"),
        ("Uptime", "14d 3h"),
        ("Jobs", "25"),
        ("Memory", "18.4 GB"),
    ];

    for (i, (key, val)) in pairs.iter().enumerate() {
        let y = area.y + (i as u16) * 2;

        if y >= area.bottom() {
            break;
        }

        let key_line = Line::from(format!("{key}:")).style(Style::new().bold());
        key_line.render(Rect::new(area.x, y, area.width, 1), buf);

        let val_x = area.x + key.len() as u16 + 2;

        if val_x < area.right() {
            Line::from(*val).render(Rect::new(val_x, y, area.right() - val_x, 1), buf);
        }
    }
}

fn render_braille_gauge(area: Rect, buf: &mut Buffer, frac: f32) {
    let fill_color = Color::Rgb(99, 102, 241);
    let empty_color = Color::Rgb(60, 60, 60);
    let filled = ((frac * area.width as f32) as u16).min(area.width);

    for col in 0..area.width {
        let x = area.x + col;
        let glyph = match col {
            0 => "\u{28BE}",
            c if c == area.width - 1 => "\u{2877}",
            _ => "\u{28FF}",
        };
        let fg = if col < filled {
            fill_color
        } else {
            empty_color
        };

        buf[(x, area.y)]
            .set_symbol(glyph)
            .set_style(Style::default().fg(fg));
    }
}