opencrabs 0.3.43

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Usage dashboard card renderers
//!
//! Each card is a self-contained render function that takes data + area and draws into a Frame.

use super::data::{
    ActivityStats, CacheStats, DailyStats, DashboardData, ModelEntry, ProjectStats, ToolStats,
    fmt_cost, fmt_tokens,
};
use ratatui::{
    Frame,
    layout::{Alignment, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Wrap},
};

const LABEL: Style = Style::new().fg(Color::DarkGray);
const BOLD: Style = Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD);
const DIM: Style = Style::new().fg(Color::DarkGray);
const ACCENT: Style = Style::new().fg(Color::Rgb(215, 100, 20));

fn card_block(title: &str, focused: bool) -> Block<'_> {
    let border_color = if focused {
        Color::Rgb(215, 100, 20)
    } else {
        Color::DarkGray
    };
    Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .title(Span::styled(
            format!(" {} ", title),
            if focused { ACCENT } else { LABEL },
        ))
}

// ── Summary Bar ──────────────────────────────────────────────────────────────

pub fn render_summary(f: &mut Frame, data: &DashboardData, area: Rect, period_label: &str) {
    let s = &data.summary;
    let version = crate::VERSION;
    let line = Line::from(vec![
        Span::styled(format!("v{version}  "), ACCENT),
        Span::styled("Tokens: ", LABEL),
        Span::styled(fmt_tokens(s.total_tokens), BOLD),
        Span::styled("  Cost: ", LABEL),
        Span::styled(fmt_cost(s.total_cost), BOLD),
        Span::styled("  Sessions: ", LABEL),
        Span::styled(format!("{}", s.session_count), BOLD),
        Span::styled("  Calls: ", LABEL),
        Span::styled(format!("{}", s.call_count), BOLD),
        Span::styled(format!("  [{}]", period_label), ACCENT),
    ]);
    let block = Block::default()
        .borders(Borders::BOTTOM)
        .border_style(Style::default().fg(Color::DarkGray));
    let paragraph = Paragraph::new(vec![line])
        .block(block)
        .alignment(Alignment::Center);
    f.render_widget(paragraph, area);
}

// ── Daily Activity ───────────────────────────────────────────────────────────

pub fn render_daily(f: &mut Frame, daily: &[DailyStats], area: Rect, focused: bool) {
    let block = card_block("Daily Activity", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if daily.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    let max_tokens = daily.iter().map(|d| d.tokens).max().unwrap_or(1);

    // Compute actual cost column width from data
    let max_cost_len = daily
        .iter()
        .map(|d| fmt_tokens(d.tokens).len())
        .max()
        .unwrap_or(8);
    let date_width = 6usize; // " 04-15 "
    let data_cols = max_cost_len + 2; // " {cost}"
    let bar_width = inner.width.saturating_sub((date_width + data_cols) as u16) as usize;

    let mut lines: Vec<Line> = Vec::new();
    // Show most recent days first (reversed), N that fit
    let visible = (inner.height as usize).min(daily.len());
    let start = daily.len().saturating_sub(visible);

    for day in daily[start..].iter().rev() {
        let bar_len = if max_tokens > 0 && bar_width > 0 {
            ((day.tokens as f64 / max_tokens as f64) * bar_width as f64).ceil() as usize
        } else {
            0
        };
        let bar_len = bar_len.max(1).min(bar_width);
        let bar: String = "\u{2584}".repeat(bar_len); // ▄ lower-half block for visual separation
        let pad: String = " ".repeat(bar_width.saturating_sub(bar_len));
        // Show short date (MM-DD)
        let short_date = if day.date.len() >= 10 {
            &day.date[5..10]
        } else {
            &day.date
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:>5} ", short_date), DIM),
            Span::styled(bar, ACCENT),
            Span::raw(pad),
            Span::styled(
                format!(" {:>width$}", fmt_tokens(day.tokens), width = max_cost_len),
                LABEL,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── By Project ───────────────────────────────────────────────────────────────

pub fn render_projects(f: &mut Frame, projects: &[ProjectStats], area: Rect, focused: bool) {
    let block = card_block("By Project", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if projects.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    // Compute column widths from actual data
    let max_cost_len = projects
        .iter()
        .map(|p| fmt_cost(p.cost).len())
        .max()
        .unwrap_or(6);
    let max_tok_len = projects
        .iter()
        .map(|p| fmt_tokens(p.tokens).len())
        .max()
        .unwrap_or(6);
    let max_sess_len = projects
        .iter()
        .map(|p| p.sessions.to_string().len())
        .max()
        .unwrap_or(1);

    // Data columns get guaranteed space; name column fills whatever is left
    let spacing = 2;
    let cost_width = max_cost_len;
    let tok_width = max_tok_len;
    let sess_width = max_sess_len;
    // +1 for leading space on name, +1 for trailing 's' on sessions
    let fixed = cost_width + tok_width + sess_width + spacing * 3 + 2;
    let name_width = (inner.width as usize).saturating_sub(fixed).max(4);

    let mut lines: Vec<Line> = Vec::new();
    let visible = (inner.height as usize).min(projects.len());
    for proj in projects.iter().take(visible) {
        let name = if proj.project.len() > name_width {
            format!(
                "{}...",
                proj.project
                    .chars()
                    .take(name_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            proj.project.clone()
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", name, width = name_width), BOLD),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", fmt_cost(proj.cost), width = cost_width),
                LABEL,
            ),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", fmt_tokens(proj.tokens), width = tok_width),
                DIM,
            ),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}s", proj.sessions, width = sess_width),
                DIM,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── By Model ─────────────────────────────────────────────────────────────────

pub fn render_models(f: &mut Frame, models: &[ModelEntry], area: Rect, focused: bool) {
    let block = card_block("By Model", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if models.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    // Compute column widths from ALL visible data (parent + child rows)
    let visible = (inner.height as usize).min(models.len());
    let all_models: Vec<&ModelEntry> = models.iter().take(visible).collect();

    let max_cost_len = all_models
        .iter()
        .flat_map(|m| {
            let parent_cost = fmt_cost(m.cost);
            let mut costs = if m.estimated {
                vec![format!("~{}", parent_cost)]
            } else {
                vec![parent_cost]
            };
            for v in &m.variants {
                costs.push(fmt_cost(v.cost));
            }
            costs.iter().map(|s| s.len()).collect::<Vec<_>>()
        })
        .max()
        .unwrap_or(6);

    let max_tok_len = all_models
        .iter()
        .flat_map(|m| {
            let mut lens = vec![fmt_tokens(m.tokens).len()];
            for v in &m.variants {
                lens.push(fmt_tokens(v.tokens).len());
            }
            lens
        })
        .max()
        .unwrap_or(6);

    let max_calls_len = all_models
        .iter()
        .flat_map(|m| {
            let mut lens = vec![m.calls.to_string().len()];
            for v in &m.variants {
                lens.push(v.calls.to_string().len());
            }
            lens
        })
        .max()
        .unwrap_or(1);

    // Data columns get guaranteed space; name column fills whatever is left
    let spacing = 2;
    let cost_width = max_cost_len;
    let tok_width = max_tok_len;
    let calls_width = max_calls_len;
    let fixed = cost_width + tok_width + calls_width + spacing * 3 + 1;
    let name_width = (inner.width as usize).saturating_sub(fixed).max(4);

    let mut lines: Vec<Line> = Vec::new();
    for m in all_models.iter() {
        // Render the parent-row name as the normalized kebab-case id so
        // every entry in the dashboard follows one consistent pattern
        // (`family-X.Y-tier`, all lowercase, dashes between segments).
        // Prettified labels ("Qwen 3.7 Max") belong in /models and
        // onboarding, not the usage ledger.
        let display = m.model.clone();
        let name = if display.len() > name_width {
            format!(
                "{}...",
                display
                    .chars()
                    .take(name_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            display
        };

        // Parent row (bold model name, no prefix)
        let cost_style = if m.estimated { ACCENT } else { LABEL };
        let cost_str = if m.estimated {
            format!("~{}", fmt_cost(m.cost))
        } else {
            fmt_cost(m.cost)
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", name, width = name_width), BOLD),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", cost_str, width = cost_width),
                cost_style,
            ),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", fmt_tokens(m.tokens), width = tok_width),
                DIM,
            ),
            Span::raw("  "),
            Span::styled(format!("{:>width$} req", m.calls, width = calls_width), DIM),
        ]));

        // Child rows (variant breakdown) — only show the tree when
        // there's something MEANINGFUL to break down. A variant
        // whose name is a cosmetic alias of the parent (only
        // separator/case differences, e.g. `qwen3.7-max` vs
        // `qwen-3.7-max`) carries zero information and just
        // duplicates the parent line visually.
        let meaningful_variants: Vec<&_> = m
            .variants
            .iter()
            .filter(|v| {
                crate::usage::data::normalize_model_for_grouping(&v.name) != m.model
                    && !crate::usage::data::is_cosmetic_alias_of_parent(&v.name, &m.model)
            })
            .collect();
        let has_real_variants = !meaningful_variants.is_empty();

        if has_real_variants {
            // Tree prefix: box-drawing glyph + 1 trailing space = 3 display
            // chars. The `├─` / `└─` glyphs are themselves the depth signal;
            // adding leading whitespace just shoves the name off the natural
            // alignment with the parent and wastes horizontal room without
            // clarifying anything. Reserve those 3 chars from `name_width`
            // so child cost / tokens / calls columns still share the parent
            // row's right-hand column edges.
            const TREE_PREFIX_CHARS: usize = 3;
            let max_child_name = name_width.saturating_sub(TREE_PREFIX_CHARS);
            for (vidx, v) in meaningful_variants.iter().enumerate() {
                let is_last = vidx == meaningful_variants.len() - 1;
                let prefix = if is_last { "└─ " } else { "├─ " };
                // Keep child variant names as the raw API id so the
                // operator sees exactly what was recorded (e.g.
                // `qwen3.7-max-20260520`), matching the parent row's
                // lowercase-kebab convention.
                let v_display = v.name.clone();
                let v_name = if v_display.chars().count() > max_child_name {
                    format!(
                        "{}",
                        v_display
                            .chars()
                            .take(max_child_name.saturating_sub(1))
                            .collect::<String>()
                    )
                } else {
                    v_display
                };
                let v_name = format!("{prefix}{v_name}");
                lines.push(Line::from(vec![
                    // Same total span width as the parent row — leading space
                    // + name padded to name_width — so cost/tokens/calls
                    // start at the same column for both.
                    Span::styled(format!(" {:<width$}", v_name, width = name_width), DIM),
                    Span::raw("  "),
                    Span::styled(
                        format!("{:>width$}", fmt_cost(v.cost), width = cost_width),
                        LABEL,
                    ),
                    Span::raw("  "),
                    Span::styled(
                        format!("{:>width$}", fmt_tokens(v.tokens), width = tok_width),
                        DIM,
                    ),
                    Span::raw("  "),
                    Span::styled(format!("{:>width$} req", v.calls, width = calls_width), DIM),
                ]));
            }
        }
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── Core Tools ───────────────────────────────────────────────────────────────

pub fn render_tools(f: &mut Frame, tools: &[ToolStats], area: Rect, focused: bool) {
    let block = card_block("Core Tools", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if tools.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    let visible = (inner.height as usize).min(tools.len());

    // Compute widths from actual data
    let max_name_len = tools
        .iter()
        .take(visible)
        .map(|t| t.tool_name.len())
        .max()
        .unwrap_or(8);
    let max_count_len = tools
        .iter()
        .take(visible)
        .map(|t| t.call_count.to_string().len())
        .max()
        .unwrap_or(1);
    let max_count = tools.first().map(|t| t.call_count).unwrap_or(1);

    // Reserve space for actual name and count, bar gets the rest
    let data_cols = max_name_len + max_count_len + 3; // 3 spacer chars
    let total_needed = (inner.width as usize).min(data_cols);
    let name_width = total_needed
        .saturating_sub(max_count_len + 2)
        .max(max_name_len.min(4));
    let count_width = total_needed
        .saturating_sub(name_width + 2)
        .max(max_count_len);
    let bar_width = inner
        .width
        .saturating_sub((name_width + count_width + 3) as u16) as usize;

    let mut lines: Vec<Line> = Vec::new();
    for tool in tools.iter().take(visible) {
        let bar_len = if max_count > 0 && bar_width > 0 {
            ((tool.call_count as f64 / max_count as f64) * bar_width as f64).ceil() as usize
        } else {
            0
        };
        let bar_len = bar_len.max(1).min(bar_width);
        let bar: String = "\u{2584}".repeat(bar_len);
        let pad: String = " ".repeat(bar_width.saturating_sub(bar_len));
        let name = if tool.tool_name.len() > name_width {
            format!(
                "{}...",
                tool.tool_name
                    .chars()
                    .take(name_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            tool.tool_name.clone()
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", name, width = name_width), BOLD),
            Span::styled(bar, ACCENT),
            Span::raw(pad),
            Span::styled(
                format!(" {:>width$}", tool.call_count, width = count_width),
                DIM,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── By Activity ──────────────────────────────────────────────────────────────

/// Column widths for the "By Activity" card. Each width is the larger
/// of the widest data value and its header label, so headers like
/// "1-shot" or "Turns" never overflow a column sized only for the
/// values (the 2026-04-25 "1-sho" clip).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ActivityColumnWidths {
    pub cat: usize,
    pub cost: usize,
    pub turns: usize,
    /// Pct width includes one slot for the trailing '%' so values like
    /// "60%" line up at the same right edge as the bare-number widest.
    pub pct: usize,
}

/// Pure column-width calculator for the "By Activity" card. Pulled out
/// so `src/tests/usage_activity_columns_test.rs` can exercise the
/// header-label floor that prevents "1-shot" / "Turns" from being
/// clipped when their data values are narrower than the header.
pub(crate) fn activity_column_widths(activities: &[ActivityStats]) -> ActivityColumnWidths {
    let max_cat_len = activities
        .iter()
        .map(|a| a.category.len())
        .max()
        .unwrap_or(8);
    let max_cost_len = activities
        .iter()
        .map(|a| fmt_cost(a.cost).len())
        .max()
        .unwrap_or(6);
    let max_turns_len = activities
        .iter()
        .map(|a| a.turns.to_string().len())
        .max()
        .unwrap_or(1);
    let max_pct_len = activities
        .iter()
        .map(|a| a.one_shot_pct.to_string().len())
        .max()
        .unwrap_or(1);

    ActivityColumnWidths {
        cat: max_cat_len,
        cost: max_cost_len.max("Cost".len()),
        turns: max_turns_len.max("Turns".len()),
        // +1 for the trailing '%' the values render with.
        pct: (max_pct_len + 1).max("1-shot".len()),
    }
}

pub fn render_activities(f: &mut Frame, activities: &[ActivityStats], area: Rect, focused: bool) {
    let block = card_block("By Activity", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if activities.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    // Compute column widths from actual data
    let visible = (inner.height.saturating_sub(1) as usize).min(activities.len());
    let widths = activity_column_widths(&activities[..visible]);
    let max_cat_len = widths.cat;
    let cost_width = widths.cost;
    let turns_width = widths.turns;
    let pct_width = widths.pct;

    // Data columns get guaranteed space; category+bar fill whatever is left
    let spacing = 1; // single space between data cols
    let fixed_data = cost_width + turns_width + pct_width + spacing * 3;
    let cat_bar_width = (inner.width as usize).saturating_sub(fixed_data + 1); // +1 leading space
    let cat_width = max_cat_len.min(cat_bar_width / 3).max(4);
    let bar_width = cat_bar_width.saturating_sub(cat_width);

    let header_line = Line::from(vec![
        Span::styled(format!(" {:<width$}", "Category", width = cat_width), LABEL),
        Span::raw(" ".repeat(bar_width)),
        Span::styled(format!(" {:>width$}", "Cost", width = cost_width), LABEL),
        Span::styled(format!(" {:>width$}", "Turns", width = turns_width), LABEL),
        Span::styled(format!(" {:>width$}", "1-shot", width = pct_width), LABEL),
    ]);
    let mut lines: Vec<Line> = vec![header_line];

    let max_cost = activities.iter().map(|a| a.cost).fold(0.0_f64, f64::max);

    for act in activities.iter().take(visible) {
        let bar_len = if max_cost > 0.0 && bar_width > 0 {
            ((act.cost / max_cost) * bar_width as f64).ceil() as usize
        } else {
            0
        };
        let bar_len = bar_len
            .max(if act.cost > 0.0 { 1 } else { 0 })
            .min(bar_width);
        let bar: String = "\u{2584}".repeat(bar_len);
        let pad: String = " ".repeat(bar_width.saturating_sub(bar_len));
        let category = if act.category.len() > cat_width {
            format!(
                "{}...",
                act.category
                    .chars()
                    .take(cat_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            act.category.clone()
        };
        let one_shot = format!("{}%", act.one_shot_pct as u32);
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", category, width = cat_width), BOLD),
            Span::styled(bar, ACCENT),
            Span::raw(pad),
            Span::styled(
                format!(" {:>width$}", fmt_cost(act.cost), width = cost_width),
                LABEL,
            ),
            Span::styled(format!(" {:>width$}", act.turns, width = turns_width), DIM),
            Span::styled(format!(" {:>width$}", one_shot, width = pct_width), DIM),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── Cache Efficiency ─────────────────────────────────────────────────────────

pub fn render_cache_efficiency(
    f: &mut Frame,
    cache: &Option<CacheStats>,
    area: Rect,
    focused: bool,
) {
    let block = card_block("Cache Efficiency", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    let cache = match cache {
        Some(c) => c,
        None => {
            let p = Paragraph::new(" No cache data").style(DIM);
            f.render_widget(p, inner);
            return;
        }
    };

    // Color-code by hit rate: green >=60%, yellow 30-60%, red <30%.
    let pct_color = |p: f64| {
        if p >= 60.0 {
            Color::Green
        } else if p >= 30.0 {
            Color::Yellow
        } else {
            Color::Red
        }
    };

    let mut lines: Vec<Line> = Vec::new();

    // Overall efficiency — one compact header line.
    lines.push(Line::from(vec![
        Span::styled(
            format!("{:.0}%", cache.cache_hit_pct),
            Style::new()
                .fg(pct_color(cache.cache_hit_pct))
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(" overall", DIM),
    ]));

    // Per-model breakdown, highest hit-rate first. Each row: model … NN%.
    // No blank spacer — vertical space in this card is scarce, every row is a
    // model we'd otherwise drop.
    if !cache.per_model.is_empty() {
        let width = inner.width as usize;
        let name_w = width.saturating_sub(5).max(6); // leave room for " 100%"
        for ms in &cache.per_model {
            if lines.len() as u16 >= inner.height {
                break;
            }
            let name: String = ms.model.chars().take(name_w).collect();
            lines.push(Line::from(vec![
                Span::raw(format!("{name:<name_w$}")),
                Span::styled(
                    format!("{:>3}%", ms.cache_hit_pct.round() as i64),
                    Style::new().fg(pct_color(ms.cache_hit_pct)),
                ),
            ]));
        }
    } else {
        // No per-model rows (e.g. only one provider) — keep the cached/total line.
        lines.push(Line::from(Span::styled(
            format!(
                "{} / {} cached",
                fmt_tokens(cache.cached_tokens),
                fmt_tokens(cache.total_input_tokens)
            ),
            DIM,
        )));
    }

    f.render_widget(Paragraph::new(lines), inner);
}

// ── Footer ───────────────────────────────────────────────────────────────────

pub fn render_footer(f: &mut Frame, area: Rect) {
    let line = Line::from(vec![
        Span::styled("Tab", ACCENT),
        Span::styled(" navigate  ", DIM),
        Span::styled("Enter", ACCENT),
        Span::styled(" details  ", DIM),
        Span::styled("T", ACCENT),
        Span::styled(" today  ", DIM),
        Span::styled("W", ACCENT),
        Span::styled(" week  ", DIM),
        Span::styled("M", ACCENT),
        Span::styled(" month  ", DIM),
        Span::styled("A", ACCENT),
        Span::styled(" all  ", DIM),
        Span::styled("Esc", ACCENT),
        Span::styled(" close", DIM),
    ]);
    let p = Paragraph::new(vec![line])
        .alignment(Alignment::Center)
        .wrap(Wrap { trim: false });
    f.render_widget(p, area);
}