clauth 0.5.3

Simple Claude Code account switcher and usage monitor
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
691
692
693
694
695
//! Status tab — incident list on the left, the selected incident's timeline on
//! the right. Master-detail (counts as 2 of the 3-panel budget; no third panel).
//!
//! The left panel is the focusable selector: each incident takes two rows, a
//! title row and a `[ phase ]` pill row with a right-aligned relative age. A
//! spinner sits in the title inset while a manual refresh is in flight; feed
//! health lives in the header's `● status.claude.ai` dot. The right panel is
//! a read-only timeline the list descends into with enter; up/down scrolls it.

use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::symbols::border;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};

use super::super::app::{App, StatusFocus};
use super::super::theme;
use super::format::{clock_label, relative_age, spinner_frame};
use super::panes::{draw_scrollbar, empty_state, section_box};
use crate::status::{Impact, Incident, IncidentUpdate, UpdatePhase, shorten_component_status};

/// Detail-pane key column width (matches the usage tab's `KEY_W`).
const KEY_W: usize = 11;

pub(super) fn draw(frame: &mut Frame<'_>, area: Rect, app: &App) {
    // Selector width: 2/5 of the body, clamped 24–40 (spec).
    let sel_w = (area.width.saturating_mul(2) / 5).clamp(24, 40);
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Length(sel_w), Constraint::Min(20)])
        .split(area);

    draw_incident_list(frame, cols[0], app);
    draw_incident_detail(frame, cols[1], app);
}

// ── Left panel: incident list ─────────────────────────────────────────────────

fn draw_incident_list(frame: &mut Frame<'_>, area: Rect, app: &App) {
    let focused = app.status.focus == StatusFocus::List;
    let block = list_block(app, focused);
    let inner = block.inner(area);
    frame.render_widget(block, area);

    if app.status.incidents.is_empty() {
        // The widget already renders the `r to retry` action line, so the hint
        // line only states the condition.
        let widget = if app.status.error.is_some() {
            empty_state("fetch failed", "r", "to retry")
        } else {
            empty_state("no status data yet", "r", "to fetch")
        };
        frame.render_widget(widget, inner);
        return;
    }

    // 2 lines per incident; window so the selected item stays visible.
    const ITEM_H: usize = 2;
    let viewport_lines = inner.height as usize;
    // Items that fill the viewport (ceil — the last may be partially clipped).
    let viewport_items = viewport_lines
        .div_ceil(ITEM_H)
        .max(1)
        .min(app.status.incidents.len());
    let first_item = first_visible_item(
        app.status.cursor,
        viewport_items,
        app.status.incidents.len(),
    );

    let shown = (app.status.incidents.len() - first_item).min(viewport_items);
    let mut lines: Vec<Line<'static>> = Vec::with_capacity(shown * ITEM_H);
    let content_w = inner.width as usize;
    for (i, incident) in app
        .status
        .incidents
        .iter()
        .enumerate()
        .skip(first_item)
        .take(shown)
    {
        let selected = i == app.status.cursor;
        lines.extend(incident_rows(incident, selected, focused, content_w));
    }

    frame.render_widget(Paragraph::new(lines).style(theme::base()), inner);

    // Scrollbar: total = items × 2 lines; viewport = actual pixel height.
    draw_scrollbar(
        frame,
        inner,
        app.status.incidents.len() * ITEM_H,
        first_item * ITEM_H,
        viewport_lines,
    );
}

/// First item index to render so `cursor` is near the center of the viewport,
/// shifting the window on both ↑ and ↓ (not only when the cursor hits the bottom).
fn first_visible_item(cursor: usize, viewport_items: usize, total: usize) -> usize {
    if total <= viewport_items {
        return 0;
    }
    let half = viewport_items / 2;
    if cursor < half {
        0
    } else {
        cursor.saturating_sub(half).min(total - viewport_items)
    }
}

/// Two rendered lines for one incident: title row + phase-pill / age row. The
/// `BG_HOVER` tint spans both lines' full content width when selected.
fn incident_rows(
    incident: &Incident,
    selected: bool,
    pane_focused: bool,
    content_w: usize,
) -> Vec<Line<'static>> {
    let tint = if selected {
        Some(theme::bg_hover())
    } else {
        None
    };
    let with_bg = |style: Style| match tint {
        Some(c) => style.bg(c),
        None => style,
    };

    // Line 1: caret gutter + truncated title.
    let caret = if selected && pane_focused {
        Span::styled(
            "",
            with_bg(
                Style::default()
                    .fg(theme::accent_color())
                    .add_modifier(Modifier::BOLD),
            ),
        )
    } else {
        Span::styled("  ", with_bg(Style::default()))
    };
    // Unselected titles read as primary content (TEXT); the selected one keeps
    // the focused TEXT + bold promotion.
    let title_style = if selected && pane_focused {
        with_bg(
            Style::default()
                .fg(theme::text_color())
                .add_modifier(Modifier::BOLD),
        )
    } else {
        with_bg(theme::body())
    };
    let title = truncate(&incident.title, content_w.saturating_sub(2));
    let mut line1 = vec![caret, Span::styled(title, title_style)];
    pad_to(&mut line1, content_w, tint);

    // Line 2: 2-space gutter + `[ impact ]` pill (severity only, no status
    // word) + right-aligned age. Ongoing incidents swap the age for a semantic
    // dot. Fit priority: impact > age/dot.
    let mut line2: Vec<Span<'static>> = vec![Span::styled("  ", with_bg(Style::default()))];
    let mut used = 2usize;

    // Impact pill (omitted when none).
    if !matches!(incident.impact, Impact::None) {
        let (iword, icolor) = impact_pill(&incident.impact);
        line2.extend([
            Span::styled("[ ", with_bg(theme::dim())),
            Span::styled(
                iword.clone(),
                with_bg(Style::default().fg(icolor).add_modifier(Modifier::BOLD)),
            ),
            Span::styled(" ]", with_bg(theme::dim())),
        ]);
        used += 2 + iword.chars().count() + 2; // "[ word ]" — no gap before it
    }

    if incident.is_active() {
        // Ongoing → semantic dot in place of the age, 1-char right padding.
        let dot_color = phase_text_color(&incident.phase);
        let dot = "";
        let dot_w = dot.chars().count();
        // 1-char right pad so pad_to leaves a trailing space when possible.
        if used + 2 + dot_w <= content_w {
            let gap = content_w - used - dot_w - 1;
            line2.push(Span::styled(" ".repeat(gap), with_bg(Style::default())));
            line2.push(Span::styled(
                dot,
                with_bg(Style::default().fg(dot_color).add_modifier(Modifier::BOLD)),
            ));
        }
    } else {
        let age = relative_age(incident.started_ms);
        let age_w = age.chars().count();
        if used + 2 + age_w <= content_w {
            let gap = content_w - used - age_w - 1;
            line2.push(Span::styled(" ".repeat(gap), with_bg(Style::default())));
            line2.push(Span::styled(age, with_bg(theme::faint())));
        }
    }
    pad_to(&mut line2, content_w, tint);

    vec![Line::from(line1), Line::from(line2)]
}

/// Pad a span list with tinted filler so the `BG_HOVER` tint spans the full
/// content width (the ratatui filler-tint gotcha).
fn pad_to(spans: &mut Vec<Span<'static>>, content_w: usize, tint: Option<ratatui::style::Color>) {
    let used: usize = spans.iter().map(|s| s.content.chars().count()).sum();
    let pad = content_w.saturating_sub(used);
    if pad > 0 {
        let style = match tint {
            Some(c) => Style::default().bg(c),
            None => Style::default(),
        };
        spans.push(Span::styled(" ".repeat(pad), style));
    }
}

/// The list panel block. The Block draws every border dash (chrome owns the
/// dashes); the title is a bare token with single-space insets. A
/// manual-refresh spinner lives inside the title token's trailing inset
/// (` INCIDENTS ⠇ `), never appended after it. Feed health lives in the
/// header's `● status.claude.ai` dot, not here.
fn list_block(app: &App, focused: bool) -> Block<'static> {
    let border_color = if focused {
        theme::line_strong_color()
    } else {
        theme::line_color()
    };
    let border_style = Style::default().fg(border_color);

    // First panel on the screen → ACCENT_2 title, italic, bold when focused.
    let mut title_mods = Modifier::ITALIC;
    if focused {
        title_mods |= Modifier::BOLD;
    }
    let title_style = Style::default()
        .fg(theme::accent_2_color())
        .add_modifier(title_mods);

    // Title token: ` INCIDENTS ` with the spinner inside the trailing inset.
    let mut title_spans = vec![Span::styled(" INCIDENTS ", title_style)];
    if app.status.fetching {
        title_spans.push(Span::styled(
            format!("{} ", spinner_frame(app.tick_count)),
            theme::accent(),
        ));
    }

    Block::default()
        .borders(Borders::ALL)
        .border_set(border::ROUNDED)
        .border_style(border_style)
        .title(Line::from(title_spans))
        .padding(ratatui::widgets::Padding::horizontal(1))
}

/// `(word, color)` for an incident's status pill — delegates to
/// [`phase_text_color`] so the pill, detail header, and timeline all agree.
fn phase_pill(incident: &Incident) -> (String, ratatui::style::Color) {
    (incident.phase.word(), phase_text_color(&incident.phase))
}

// ── Right panel: incident timeline ─────────────────────────────────────────────

fn draw_incident_detail(frame: &mut Frame<'_>, area: Rect, app: &App) {
    let incident = app.status.selected();
    let title = incident.map(|i| i.title.as_str()).unwrap_or("status");
    // Read-only detail pane (focus descends but it's the second panel): blurred
    // when the list owns focus, strong when the detail is focused.
    let focused = app.status.focus == StatusFocus::Detail;
    let block = section_box(title, focused, false);
    let inner = block.inner(area);
    frame.render_widget(block, area);

    let Some(incident) = incident else {
        let hint = Paragraph::new(Line::from(Span::styled(
            "no incident selected",
            theme::dim(),
        )))
        .style(theme::base());
        frame.render_widget(hint, inner);
        return;
    };

    let lines = detail_lines(incident, inner.width as usize);
    let total = lines.len();
    let viewport = inner.height as usize;

    // Clamp the scroll so the last line stays reachable but the body never
    // scrolls past its end. Record the bound so the key handler can clamp state
    // too (otherwise a held ↓ inflates `detail_scroll` past the end).
    let max_scroll = total.saturating_sub(viewport).min(u16::MAX as usize) as u16;
    app.status.detail_max_scroll.set(max_scroll);
    let scroll = app.status.detail_scroll.min(max_scroll);

    frame.render_widget(
        Paragraph::new(lines)
            .style(theme::base())
            .scroll((scroll, 0)),
        inner,
    );
    draw_scrollbar(frame, inner, total, scroll as usize, viewport);
}

/// Build the full detail view as styled lines (header, started / components
/// rows, link, eyebrow, per-update timeline). The caller scrolls / clamps; this
/// is pure layout.
fn detail_lines(incident: &Incident, inner_w: usize) -> Vec<Line<'static>> {
    let mut lines: Vec<Line<'static>> = Vec::new();

    // Header: status pill + impact pill + age left; duration right-aligned.
    let (pill_word, pill_color) = phase_pill(incident);
    let mut header = vec![
        Span::styled("[ ", theme::dim()),
        Span::styled(
            pill_word,
            Style::default().fg(pill_color).add_modifier(Modifier::BOLD),
        ),
        Span::styled(" ]", theme::dim()),
    ];
    // Impact pill — omitted entirely when impact is none.
    if !matches!(incident.impact, Impact::None) {
        let (iword, icolor) = impact_pill(&incident.impact);
        header.extend([
            Span::styled("  [ ", theme::dim()),
            Span::styled(
                iword,
                Style::default().fg(icolor).add_modifier(Modifier::BOLD),
            ),
            Span::styled(" ]", theme::dim()),
        ]);
    }
    let age_str = relative_age(incident.started_ms);
    header.push(Span::styled(format!("  {age_str}"), theme::faint()));

    // Duration text: `· lasted <dur>` when resolved, `· ongoing` otherwise.
    let dur_str = match incident.resolved_ms {
        Some(resolved) if resolved >= incident.started_ms => {
            let dur = duration_label((resolved - incident.started_ms) / 1000);
            format!("lasted {dur}")
        }
        _ => "ongoing".to_string(),
    };
    let left_w: usize = header.iter().map(|s| s.content.chars().count()).sum();
    if left_w + dur_str.chars().count() < inner_w {
        let gap = inner_w - left_w - dur_str.chars().count();
        header.push(Span::styled(" ".repeat(gap), Style::default()));
    }
    header.push(Span::styled(dur_str, theme::faint()));
    lines.push(Line::from(header));

    // started row (the one place the `utc` suffix appears).
    lines.push(Line::from(vec![
        key_span("started"),
        Span::styled(clock_label(incident.started_ms, true), theme::body()),
    ]));

    // components row — status-dot-prefixed entries, 2-space gaps; omitted when
    // empty. Narrow panes fit whole entries only and append `+N` for the rest.
    if !incident.components.is_empty() {
        lines.push(components_line(&incident.components, inner_w));
    }

    // Link line, middle-truncated to width (kept as-is per user).
    if !incident.link.is_empty() {
        lines.push(Line::from(Span::styled(
            middle_truncate(&incident.link, inner_w),
            theme::faint(),
        )));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled("TIMELINE", theme::dim())));

    // Per update (newest first). Columns: time | phase word | wrapped body.
    let time_col = incident
        .updates
        .iter()
        .map(|u| clock_label(u.at_ms, false).chars().count())
        .max()
        .unwrap_or(0)
        .max("jun 6, 10:14".chars().count())
        .min(18);
    let phase_col = 13; // widest phase word ("investigating") + gap
    let indent = time_col + 1 + phase_col + 1;
    let body_w = inner_w.saturating_sub(indent).max(8);

    for update in &incident.updates {
        lines.extend(update_lines(update, time_col, phase_col, body_w, indent));
    }
    lines
}

/// Render one update: a `time | phase | wrapped body` row (body wraps with a
/// hanging indent under the body column), then one `TEXT_FAINT` continuation
/// line per changed component, indented to the body column.
fn update_lines(
    update: &IncidentUpdate,
    time_col: usize,
    phase_col: usize,
    body_w: usize,
    indent: usize,
) -> Vec<Line<'static>> {
    let time = pad_right(&clock_label(update.at_ms, false), time_col);
    let phase = pad_right(&update.phase.word(), phase_col);
    let phase_color = phase_text_color(&update.phase);

    let wrapped = wrap_text(&update.text, body_w);
    let mut out = Vec::with_capacity(wrapped.len() + update.transitions.len());
    let first_body = wrapped.first().cloned().unwrap_or_default();
    out.push(Line::from(vec![
        Span::styled(format!("{time} "), theme::dim()),
        Span::styled(format!("{phase} "), Style::default().fg(phase_color)),
        Span::styled(first_body, theme::dim()),
    ]));
    for cont in wrapped.iter().skip(1) {
        out.push(Line::from(vec![
            Span::raw(" ".repeat(indent)),
            Span::styled(cont.clone(), theme::dim()),
        ]));
    }
    out.extend(transition_lines(&update.transitions, indent, body_w));
    out
}

/// Aligned, colored transition block, one line per changed component, indented
/// to the body column: `name  → new`. The name column is left-padded to the
/// block's widest name + a 2-space gap; the arrow renders `TEXT_FAINT`; the new
/// status carries its semantic color (the previous state is dropped — the prior
/// timeline entry already shows it). Width-safe: an over-long line truncates the
/// NAME column first, then trailing-truncates the whole line as a last resort.
fn transition_lines(
    transitions: &[(String, String, String)],
    indent: usize,
    body_w: usize,
) -> Vec<Line<'static>> {
    if transitions.is_empty() {
        return Vec::new();
    }
    // Pre-shorten the new status; the name column is sized to the widest name.
    let rows: Vec<(String, String)> = transitions
        .iter()
        .map(|(name, _, new)| (name.to_lowercase(), shorten_component_status(new)))
        .collect();
    let widest_name = rows
        .iter()
        .map(|(n, _)| n.chars().count())
        .max()
        .unwrap_or(0);
    // Reserve room for ` → new` so the name column can shrink under pressure.
    let max_status = rows
        .iter()
        .map(|(_, n)| 2 + n.chars().count())
        .max()
        .unwrap_or(0);
    // Name column width: widest name, but shrunk so `name  → new` fits body_w.
    let name_col = widest_name
        .min(body_w.saturating_sub(2 + max_status).max(3))
        .max(1);

    rows.into_iter()
        .map(|(name, new)| {
            let name = pad_right(&truncate(&name, name_col), name_col);
            let new_color = component_status_color(&new);
            let spans = vec![
                Span::raw(" ".repeat(indent)),
                Span::styled(format!("{name}  "), theme::faint()),
                Span::styled("", theme::faint()),
                Span::styled(new, Style::default().fg(new_color)),
            ];
            // Last-resort guard: if the assembled content still overflows body_w,
            // trailing-truncate the whole line so ratatui never clips silently.
            let content_w: usize = spans
                .iter()
                .map(|s| s.content.chars().count())
                .sum::<usize>()
                - indent;
            if content_w > body_w {
                let mut flat = String::new();
                for s in spans.iter().skip(1) {
                    flat.push_str(&s.content);
                }
                Line::from(vec![
                    Span::raw(" ".repeat(indent)),
                    Span::styled(truncate(&flat, body_w), theme::faint()),
                ])
            } else {
                Line::from(spans)
            }
        })
        .collect()
}

/// Semantic timeline color per phase: resolved/completed SUCCESS, monitoring &
/// in-progress/verifying INFO, identified/investigating WARNING, scheduled &
/// update/other TEXT_DIM.
fn phase_text_color(phase: &UpdatePhase) -> ratatui::style::Color {
    match phase {
        UpdatePhase::Resolved | UpdatePhase::Completed => theme::success_color(),
        UpdatePhase::Monitoring | UpdatePhase::InProgress | UpdatePhase::Verifying => {
            theme::info_color()
        }
        UpdatePhase::Identified | UpdatePhase::Investigating => theme::warning_color(),
        UpdatePhase::Update | UpdatePhase::Scheduled | UpdatePhase::Other(_) => {
            theme::text_dim_color()
        }
    }
}

/// `(word, color)` for an impact pill. Minor → WARNING; major/critical → DANGER;
/// none/maintenance/other → neutral TEXT_DIM.
fn impact_pill(impact: &Impact) -> (String, ratatui::style::Color) {
    let color = match impact {
        Impact::Minor => theme::warning_color(),
        Impact::Major | Impact::Critical => theme::danger_color(),
        Impact::None | Impact::Maintenance | Impact::Other(_) => theme::text_dim_color(),
    };
    (impact.word(), color)
}

/// Semantic color for a component status (raw or shortened word): operational →
/// SUCCESS, degraded → WARNING, partial/major outage → DANGER, maintenance →
/// TEXT_DIM, unknown → TEXT_FAINT.
fn component_status_color(status: &str) -> ratatui::style::Color {
    match status.trim().to_ascii_lowercase().as_str() {
        "operational" => theme::success_color(),
        "degraded" | "degraded_performance" => theme::warning_color(),
        "partial outage" | "partial_outage" | "major outage" | "major_outage" => {
            theme::danger_color()
        }
        "maintenance" | "under_maintenance" => theme::text_dim_color(),
        _ => theme::text_faint_color(),
    }
}

/// Build the detail `components` row: `● name  ● name …`. The dot alone carries
/// the component's first-reported status (user decision — no status word; the
/// name is `theme::body()`, lowercased — value-row consistency, a house
/// deviation from the dim-label rule). Fits whole entries only; dropped entries
/// append `+N`. When the column has zero room, shows just `…`.
fn components_line(components: &[(String, String)], inner_w: usize) -> Line<'static> {
    let avail = inner_w.saturating_sub(KEY_W);
    let mut spans: Vec<Span<'static>> = vec![key_span("components")];

    // Zero-width guard: no room for even one entry → a faint ellipsis.
    if avail == 0 {
        spans.push(Span::styled("", theme::faint()));
        return Line::from(spans);
    }

    let mut used = 0usize;
    let mut shown = 0usize;
    for (i, (name, status)) in components.iter().enumerate() {
        let label = name.to_lowercase();
        // Each entry: "● name", preceded by a 2-space gap except the first.
        let gap = if i == 0 { 0 } else { 2 };
        let entry_w = gap + 2 + label.chars().count();
        // Reserve room for a possible trailing `  +N` when more remain.
        let remaining_after = components.len() - i - 1;
        let reserve = if remaining_after > 0 {
            2 + 1 + count_digits(remaining_after)
        } else {
            0
        };
        if used + entry_w + reserve > avail && shown > 0 {
            break;
        }
        if gap > 0 {
            spans.push(Span::raw("  "));
        }
        spans.push(Span::styled(
            "",
            Style::default().fg(component_status_color(status)),
        ));
        spans.push(Span::styled(format!(" {label}"), theme::body()));
        used += entry_w;
        shown += 1;
    }
    let dropped = components.len() - shown;
    if dropped > 0 {
        spans.push(Span::styled(format!("  +{dropped}"), theme::faint()));
    }
    Line::from(spans)
}

/// Decimal digit count of `n` (for reserving `+N` width).
fn count_digits(n: usize) -> usize {
    if n == 0 { 1 } else { (n.ilog10() + 1) as usize }
}

/// Single-largest-unit duration label for a span in seconds: `47m`, `2h`, `3d`.
fn duration_label(secs: u64) -> String {
    let mins = secs / 60;
    let hours = mins / 60;
    let days = hours / 24;
    if days >= 1 {
        format!("{days}d")
    } else if hours >= 1 {
        format!("{hours}h")
    } else {
        format!("{}m", mins.max(1))
    }
}

/// Detail key column: `key` padded to [`KEY_W`] in the eyebrow label style.
fn key_span(key: &str) -> Span<'static> {
    let pad = KEY_W.saturating_sub(key.chars().count()).max(1);
    Span::styled(format!("{key}{}", " ".repeat(pad)), theme::label())
}

// ── Small text helpers ──────────────────────────────────────────────────────

/// Trailing-ellipsis truncation to `max` chars.
fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        return s.to_string();
    }
    if max == 0 {
        return String::new();
    }
    let mut out: String = s.chars().take(max.saturating_sub(1)).collect();
    out.push('');
    out
}

/// Middle-ellipsis truncation (for URLs / IDs — both ends carry meaning).
fn middle_truncate(s: &str, max: usize) -> String {
    let chars: Vec<char> = s.chars().collect();
    if chars.len() <= max || max < 3 {
        return truncate(s, max);
    }
    let keep = max - 1;
    let head = keep.div_ceil(2);
    let tail = keep - head;
    let front: String = chars[..head].iter().collect();
    let back: String = chars[chars.len() - tail..].iter().collect();
    format!("{front}{back}")
}

/// Pad `s` on the right to `width` chars (truncating with `…` if longer).
fn pad_right(s: &str, width: usize) -> String {
    let count = s.chars().count();
    if count > width {
        return truncate(s, width);
    }
    format!("{s}{}", " ".repeat(width - count))
}

/// Greedy word-wrap to `width` chars; long words are hard-split.
fn wrap_text(text: &str, width: usize) -> Vec<String> {
    if width == 0 {
        return vec![text.to_string()];
    }
    let mut lines = Vec::new();
    let mut line = String::new();
    for word in text.split_whitespace() {
        if word.chars().count() > width {
            // Flush, then hard-split the oversized word.
            if !line.is_empty() {
                lines.push(std::mem::take(&mut line));
            }
            let mut chunk = String::new();
            for ch in word.chars() {
                if chunk.chars().count() == width {
                    lines.push(std::mem::take(&mut chunk));
                }
                chunk.push(ch);
            }
            if !chunk.is_empty() {
                line = chunk;
            }
            continue;
        }
        let extra = if line.is_empty() { 0 } else { 1 };
        if line.chars().count() + extra + word.chars().count() > width {
            lines.push(std::mem::take(&mut line));
            line.push_str(word);
        } else {
            if !line.is_empty() {
                line.push(' ');
            }
            line.push_str(word);
        }
    }
    if !line.is_empty() {
        lines.push(line);
    }
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}