cargo-port 0.1.0

A TUI for inspecting and managing Rust projects
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
696
697
698
699
700
701
702
703
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::widgets::Paragraph;
use tui_pane::CpuUsage;
use tui_pane::PaneFocusState;
use tui_pane::PaneRule;
use tui_pane::PaneTitleCount;
use tui_pane::Region;
use tui_pane::Size;
use tui_pane::Viewport;
use tui_pane::ViewportOverflow;
use tui_pane::accent_color;
use tui_pane::error_color;
use tui_pane::label_color;
use tui_pane::render_overflow_affordance;
use tui_pane::text_default;
use tui_pane::warning_color;

use super::package::RenderStyles;
use super::pane_impls::CpuPane;
use crate::config::CpuConfig;
use crate::tui::pane::PaneRenderCtx;
use crate::tui::theme_roles;

const CPU_BAR_WIDTH: usize = 10;
/// Shown in the GPU row when the OS exposes no GPU utilization (e.g. the
/// Apple `asahi` driver on Linux). Kept within `CPU_CONTENT_WIDTH` so it
/// never widens the pane.
const GPU_UNAVAILABLE_TEXT: &str = "unavailable";
pub(super) const CPU_CONTENT_WIDTH: u16 = 17;
pub const CPU_PANE_WIDTH: u16 = CPU_CONTENT_WIDTH + 2;
/// Pixel height of every inner row except the scrolling cores band: the
/// aggregate row, the two separator rules, the three breakdown rows, and the
/// one GPU row. The cores band gets `inner.height - CPU_STATIC_INNER_HEIGHT`.
const CPU_STATIC_INNER_HEIGHT: u16 = 7;
/// Pinned head rows above the scrolling cores band: the aggregate line.
const CPU_PINNED_HEAD_ROWS: usize = 1;
/// Breakdown rows pinned below the cores band: System, User, Idle.
const CPU_BREAKDOWN_ROWS: usize = 3;
/// GPU rows pinned below the breakdown. A single aggregate row; multi-GPU is
/// deferred (see `docs/subpane-scrolling.md` → Related, separate work), so
/// growing the pinned tail for more GPUs changes only this count.
const CPU_GPU_ROWS: usize = 1;

/// Pinned tail rows below the scrolling cores band: breakdown rows plus GPU.
const fn cpu_pinned_tail_rows() -> usize { CPU_BREAKDOWN_ROWS + CPU_GPU_ROWS }

const fn total_selectable_rows(core_count: usize) -> usize {
    CPU_PINNED_HEAD_ROWS + core_count + cpu_pinned_tail_rows()
}

pub(super) fn cpu_required_inner_height(core_count: usize) -> u16 {
    let core_rows = u16::try_from(core_count).unwrap_or(u16::MAX);
    CPU_STATIC_INNER_HEIGHT.saturating_add(core_rows)
}

pub fn cpu_required_pane_height(core_count: usize) -> u16 {
    cpu_required_inner_height(core_count).saturating_add(2)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CpuSelectableRow {
    Aggregate,
    Core(usize),
    System,
    User,
    Idle,
    Gpu,
}

impl CpuSelectableRow {
    const fn logical_index(self, core_count: usize) -> usize {
        match self {
            Self::Aggregate => 0,
            Self::Core(index) => index + 1,
            Self::System => core_count + 1,
            Self::User => core_count + 2,
            Self::Idle => core_count + 3,
            Self::Gpu => core_count + 4,
        }
    }
}

/// One scrolling core row: the usage bar with the 1-based core number
/// embedded in the bar's leftmost cells, right-aligned to `number_width` and
/// followed by a one-cell margin before the block glyphs. Digits render in
/// the default text color; the fill behind the number and margin is drawn as
/// background shading, so the bar still spans its full width.
fn cpu_bar_line(
    core_number: usize,
    number_width: usize,
    percent: u8,
    cpu_cfg: &CpuConfig,
) -> Line<'static> {
    let filled = tui_pane::cpu_filled_cells(percent);
    let severity = tui_pane::cpu_severity(
        percent,
        cpu_cfg.low_utilization_max_percent,
        cpu_cfg.medium_utilization_max_percent,
    )
    .color();
    let number_text = format!("{core_number:>number_width$} ");
    let (number_on_filled, number_past_fill) = number_text.split_at(filled.min(number_text.len()));
    let number_filled_span = Span::styled(
        number_on_filled.to_string(),
        Style::default().fg(text_default()).bg(severity),
    );
    let number_empty_span = Span::styled(
        number_past_fill.to_string(),
        Style::default().fg(text_default()),
    );
    let filled_span = Span::styled(
        "".repeat(filled.saturating_sub(number_text.len())),
        Style::default().fg(severity),
    );
    let empty_span = Span::styled(
        " ".repeat(CPU_BAR_WIDTH.saturating_sub(filled.max(number_text.len()))),
        Style::default().fg(tui_pane::cpu_blank_bar_color()),
    );
    let percent_span = Span::raw(format!("{percent:>3}%"));
    Line::from(vec![
        Span::raw(" "),
        number_filled_span,
        number_empty_span,
        filled_span,
        empty_span,
        Span::raw(" "),
        percent_span,
        Span::raw(" "),
    ])
}

/// Single-line GPU row for the breakdown section: a `GPU` label with the
/// utilization percent right-aligned and colored by severity, matching the
/// System / User / Idle rows. Falls back to the unavailable text in warning
/// color when the OS exposes no GPU utilization.
fn gpu_metric_line(percent: Option<u8>, cpu_cfg: &CpuConfig, width: u16) -> Line<'static> {
    let (value_text, value_color) = percent.map_or_else(
        || (GPU_UNAVAILABLE_TEXT.to_string(), warning_color()),
        |percent| {
            let severity = tui_pane::cpu_severity(
                percent,
                cpu_cfg.low_utilization_max_percent,
                cpu_cfg.medium_utilization_max_percent,
            )
            .color();
            (format!("{percent:>3}%"), severity)
        },
    );
    let label_text = "GPU:";
    let space_count = usize::from(width).saturating_sub(
        label_text
            .len()
            .saturating_add(value_text.len())
            .saturating_add(2),
    );
    Line::from(vec![
        Span::raw(" "),
        Span::styled(label_text, Style::default().fg(text_default())),
        Span::raw(" ".repeat(space_count)),
        Span::styled(value_text, Style::default().fg(value_color)),
        Span::raw(" "),
    ])
}

fn metric_line(label: &str, percent: u8, color: Color, width: u16) -> Line<'static> {
    let label_text = format!("{label}:");
    let value_text = format!("{percent:>3}%");
    let space_count = usize::from(width).saturating_sub(
        label_text
            .len()
            .saturating_add(value_text.len())
            .saturating_add(2),
    );
    Line::from(vec![
        Span::raw(" "),
        Span::styled(label_text, Style::default().fg(text_default())),
        Span::raw(" ".repeat(space_count)),
        Span::styled(value_text, Style::default().fg(color)),
        Span::raw(" "),
    ])
}

fn aggregate_line(percent: u8, width: u16) -> Line<'static> {
    let label_text = "Aggregate";
    let value_text = format!("{percent:>3}%");
    let space_count = usize::from(width).saturating_sub(
        label_text
            .len()
            .saturating_add(value_text.len())
            .saturating_add(2),
    );
    Line::from(vec![
        Span::raw(" "),
        Span::styled(
            label_text,
            Style::default().fg(theme_roles::column_header_color()),
        ),
        Span::raw(" ".repeat(space_count)),
        Span::styled(value_text, Style::default().fg(text_default())),
        Span::raw(" "),
    ])
}

/// The CPU pane's box tree: a pinned aggregate row, the scrolling cores band
/// (the one `Fill` box), the System/User/Idle breakdown (a rule above it), and
/// the GPU row (a rule above it). Rebuilt each frame from the live core count.
fn cpu_region(core_count: usize) -> Region {
    Region::stack(vec![
        Region::rows(CPU_PINNED_HEAD_ROWS, Size::Fixed),
        Region::rows(core_count, Size::Fill),
        Region::rows(CPU_BREAKDOWN_ROWS, Size::Fixed).rule(),
        Region::rows(CPU_GPU_ROWS, Size::Fixed).rule(),
    ])
}

/// Resolved rects for one CPU frame. The aggregate row is pinned at the top;
/// the cores band is the `Fill` box and scrolls; the breakdown rows and GPU
/// follow. `band_offset` is the cores band's scroll position, held across
/// frames so the cursor stays visible.
struct CpuPanelLayout {
    core_count:    usize,
    aggregate:     Rect,
    cores:         Rect,
    cores_divider: Rect,
    system:        Rect,
    user:          Rect,
    idle:          Rect,
    gpu_divider:   Rect,
    gpu:           Rect,
    band_offset:   usize,
}

impl CpuPanelLayout {
    fn new(inner: Rect, core_count: usize, cursor_pos: usize, prior_offset: usize) -> Self {
        // The cores band is box 1; only its prior offset is meaningful, the
        // pinned boxes never scroll.
        let placed = cpu_region(core_count).place(inner, cursor_pos, &[0, prior_offset, 0, 0]);
        let breakdown = placed[2].content;
        let breakdown_row = |offset: u16| Rect {
            y: breakdown.y.saturating_add(offset),
            height: 1,
            ..breakdown
        };
        Self {
            core_count,
            aggregate: placed[0].content,
            cores: placed[1].content,
            cores_divider: placed[2].chrome,
            system: breakdown_row(0),
            user: breakdown_row(1),
            idle: breakdown_row(2),
            gpu_divider: placed[3].chrome,
            gpu: placed[3].content,
            band_offset: placed[1].scroll_offset,
        }
    }

    /// Number of core rows visible in the band this frame.
    fn band_visible(&self) -> usize { usize::from(self.cores.height) }
}

#[derive(Clone, Copy)]
struct BreakdownRowSpec<'a> {
    area:        Rect,
    logical_row: usize,
    label:       &'a str,
    percent:     u8,
    color:       Color,
}

fn cpu_panel_title(core_count: usize, cursor: Option<usize>) -> String {
    if let Some(pos) = cursor
        && (1..=core_count).contains(&pos)
    {
        return tui_pane::pane_title(
            "CPU",
            &PaneTitleCount::Single {
                len:    core_count,
                cursor: Some(pos - 1),
            },
        );
    }

    let core_label = if core_count == 1 { "core" } else { "cores" };
    format!(" CPU ({core_count} {core_label}) ")
}

fn cpu_row_overlay_style(viewport: &Viewport, logical_row: usize, focus: PaneFocusState) -> Style {
    tui_pane::selection_state(viewport, logical_row, focus).overlay_style()
}

fn render_selectable_row(
    frame: &mut Frame,
    viewport: &Viewport,
    row_rects: &mut Vec<(Rect, usize)>,
    area: Rect,
    logical_row: usize,
    focus: PaneFocusState,
    paragraph: Paragraph<'static>,
) {
    frame.render_widget(
        paragraph.style(cpu_row_overlay_style(viewport, logical_row, focus)),
        area,
    );
    row_rects.push((area, logical_row));
}

fn render_cpu_dividers(
    frame: &mut Frame,
    area: Rect,
    layout: &CpuPanelLayout,
    border_style: Style,
) {
    tui_pane::render_rules(
        frame,
        &[
            PaneRule::Horizontal {
                area:        Rect {
                    x:      area.x,
                    y:      layout.cores_divider.y,
                    width:  area.width,
                    height: 1,
                },
                connector_x: None,
            },
            PaneRule::Horizontal {
                area:        Rect {
                    x:      area.x,
                    y:      layout.gpu_divider.y,
                    width:  area.width,
                    height: 1,
                },
                connector_x: None,
            },
        ],
        border_style,
    );
}

fn render_aggregate_row(
    frame: &mut Frame,
    viewport: &Viewport,
    row_rects: &mut Vec<(Rect, usize)>,
    usage: &CpuUsage,
    layout: &CpuPanelLayout,
    focus: PaneFocusState,
) {
    let logical_row = CpuSelectableRow::Aggregate.logical_index(layout.core_count);
    render_selectable_row(
        frame,
        viewport,
        row_rects,
        layout.aggregate,
        logical_row,
        focus,
        Paragraph::new(aggregate_line(usage.total_percent, layout.aggregate.width)),
    );
}

fn render_core_rows(
    frame: &mut Frame,
    viewport: &Viewport,
    row_rects: &mut Vec<(Rect, usize)>,
    cpu_cfg: &CpuConfig,
    usage: &CpuUsage,
    layout: &CpuPanelLayout,
    focus: PaneFocusState,
) {
    let end = layout
        .band_offset
        .saturating_add(layout.band_visible())
        .min(usage.cores.len());
    let number_width = layout.core_count.to_string().len();
    for (slot, core_index) in (layout.band_offset..end).enumerate() {
        let core = &usage.cores[core_index];
        let logical_row = CpuSelectableRow::Core(core_index).logical_index(layout.core_count);
        let area = Rect {
            x:      layout.cores.x,
            y:      layout
                .cores
                .y
                .saturating_add(u16::try_from(slot).unwrap_or(u16::MAX)),
            width:  layout.cores.width,
            height: 1,
        };
        render_selectable_row(
            frame,
            viewport,
            row_rects,
            area,
            logical_row,
            focus,
            Paragraph::new(cpu_bar_line(
                core_index + 1,
                number_width,
                core.percent,
                cpu_cfg,
            )),
        );
    }
}

fn render_breakdown_row(
    frame: &mut Frame,
    viewport: &Viewport,
    row_rects: &mut Vec<(Rect, usize)>,
    focus: PaneFocusState,
    row: BreakdownRowSpec<'_>,
) {
    render_selectable_row(
        frame,
        viewport,
        row_rects,
        row.area,
        row.logical_row,
        focus,
        Paragraph::new(metric_line(
            row.label,
            row.percent,
            row.color,
            row.area.width,
        )),
    );
}

fn render_gpu_row(
    frame: &mut Frame,
    viewport: &Viewport,
    row_rects: &mut Vec<(Rect, usize)>,
    cpu_cfg: &CpuConfig,
    usage: &CpuUsage,
    layout: &CpuPanelLayout,
    focus: PaneFocusState,
) {
    let logical_row = CpuSelectableRow::Gpu.logical_index(layout.core_count);
    let area = layout.gpu;
    render_selectable_row(
        frame,
        viewport,
        row_rects,
        area,
        logical_row,
        focus,
        Paragraph::new(gpu_metric_line(usage.gpu_percent, cpu_cfg, area.width)),
    );
}

const fn sync_cpu_pane_state(
    viewport: &mut Viewport,
    inner: Rect,
    core_count: usize,
    band_offset: usize,
) {
    viewport.set_len(total_selectable_rows(core_count));
    viewport.set_content_area(inner);
    viewport.set_scroll_offset(band_offset);
}

/// Body of `CpuPane::render`. Lives here (next to its helpers)
/// rather than inline in `pane_impls.rs` because the helpers
/// belong with the per-pane render code; only the trait method
/// itself sits in `pane_impls.rs` and delegates here.
pub(super) fn render_cpu_pane_body(
    frame: &mut Frame,
    area: Rect,
    pane: &mut CpuPane,
    styles: &RenderStyles,
    ctx: &PaneRenderCtx<'_>,
) {
    let focus = pane.focus.state;
    let cursor = matches!(focus, PaneFocusState::Active).then(|| pane.viewport.pos());
    let title = pane.content().map_or_else(
        || " CPU ".to_string(),
        |usage| cpu_panel_title(usage.cores.len(), cursor),
    );
    let block = styles.chrome.block(title, pane.focus.is_focused);
    let inner = block.inner(area);
    frame.render_widget(block, area);

    if inner.height == 0 {
        pane.viewport.clear_surface();
        pane.clear_row_rects();
        return;
    }

    let usage = pane
        .content()
        .cloned()
        .unwrap_or_else(|| tui_pane::CpuUsage::placeholder(1));
    let cursor_pos = pane.viewport.pos();
    let layout = CpuPanelLayout::new(
        inner,
        usage.cores.len(),
        cursor_pos,
        pane.viewport.scroll_offset(),
    );

    let border_style = if matches!(focus, PaneFocusState::Active) {
        styles.chrome.active_border
    } else {
        styles.chrome.inactive_border
    };
    render_cpu_dividers(frame, area, &layout, border_style);

    let cpu_cfg = &ctx.config.current().cpu;
    let mut row_rects: Vec<(Rect, usize)> = Vec::new();
    let viewport = &pane.viewport;
    render_aggregate_row(frame, viewport, &mut row_rects, &usage, &layout, focus);
    render_core_rows(
        frame,
        viewport,
        &mut row_rects,
        cpu_cfg,
        &usage,
        &layout,
        focus,
    );
    render_breakdown_row(
        frame,
        viewport,
        &mut row_rects,
        focus,
        BreakdownRowSpec {
            area:        layout.system,
            logical_row: CpuSelectableRow::System.logical_index(layout.core_count),
            label:       "System",
            percent:     usage.breakdown.system,
            color:       error_color(),
        },
    );
    render_breakdown_row(
        frame,
        viewport,
        &mut row_rects,
        focus,
        BreakdownRowSpec {
            area:        layout.user,
            logical_row: CpuSelectableRow::User.logical_index(layout.core_count),
            label:       "User",
            percent:     usage.breakdown.user,
            color:       accent_color(),
        },
    );
    render_breakdown_row(
        frame,
        viewport,
        &mut row_rects,
        focus,
        BreakdownRowSpec {
            area:        layout.idle,
            logical_row: CpuSelectableRow::Idle.logical_index(layout.core_count),
            label:       "Idle",
            percent:     usage.breakdown.idle,
            color:       text_default(),
        },
    );
    render_gpu_row(
        frame,
        viewport,
        &mut row_rects,
        cpu_cfg,
        &usage,
        &layout,
        focus,
    );
    render_cores_affordance(frame, &layout, cursor_pos);
    sync_cpu_pane_state(
        &mut pane.viewport,
        inner,
        usage.cores.len(),
        layout.band_offset,
    );
    pane.set_row_rects(row_rects);
}

/// Draw the `▲ n of m ▼` overflow label on the cores band when more cores
/// exist than fit. Skipped when the band has no visible rows.
fn render_cores_affordance(frame: &mut Frame, layout: &CpuPanelLayout, cursor_pos: usize) {
    let band_visible = layout.band_visible();
    if band_visible == 0 {
        return;
    }
    // The cores band owns selectable rows [CPU_PINNED_HEAD_ROWS,
    // CPU_PINNED_HEAD_ROWS + core_count); on one of those the pager anchors to
    // that core, otherwise to the current scroll offset.
    let band_cursor = cursor_pos
        .checked_sub(CPU_PINNED_HEAD_ROWS)
        .filter(|local| *local < layout.core_count)
        .unwrap_or(layout.band_offset);
    render_overflow_affordance(
        frame,
        layout.cores,
        ViewportOverflow::new(
            layout.core_count,
            layout.band_offset,
            band_visible,
            band_cursor,
        ),
        Style::default().fg(label_color()),
    );
}

#[cfg(test)]
mod tests {
    use ratatui::layout::Rect;
    use tui_pane::text_default;

    use super::CPU_CONTENT_WIDTH;
    use super::cpu_bar_line;
    use super::cpu_region;
    use crate::config::CpuConfig;

    // A 15-core CPU. Fixed boxes take 7 rows (1 aggregate + 1+3 breakdown +
    // 1+1 GPU), so a 12-row inner leaves the cores band 5 rows and it must
    // scroll; a 22-row inner fits all 15 cores. The cores band's resolved
    // scroll offset is box index 1's `scroll_offset`.
    fn cores_offset(inner_height: u16, cursor: usize, prior: usize) -> usize {
        let inner = Rect {
            x:      0,
            y:      0,
            width:  20,
            height: inner_height,
        };
        cpu_region(15).place(inner, cursor, &[0, prior, 0, 0])[1].scroll_offset
    }

    #[test]
    fn band_offset_tracks_cursor_inside_the_band() {
        // Cursor on logical row 14 (band-local 13) scrolls a 5-tall band to
        // its last full page.
        assert_eq!(cores_offset(12, 14, 0), 9);
    }

    #[test]
    fn band_offset_holds_prior_on_a_pinned_head_row() {
        // Cursor on the aggregate row (logical 0) is outside the band, so the
        // band stays where it was.
        assert_eq!(cores_offset(12, 0, 7), 7);
    }

    #[test]
    fn band_offset_holds_prior_on_a_pinned_tail_row() {
        // Cursor on the first breakdown row (logical 16) holds the prior
        // offset, clamped to the band's last page (15 cores - 5 visible).
        assert_eq!(cores_offset(12, 16, 20), 10);
    }

    #[test]
    fn band_offset_is_zero_when_every_core_fits() {
        // Cores band taller than the core count: no scroll regardless of
        // cursor.
        assert_eq!(cores_offset(22, 14, 0), 0);
    }

    // cpu_bar_line spans: [space, number-on-filled, number-past-fill,
    // filled bar, empty bar, space, percent, space]. The number text
    // carries a one-cell margin before the bar's block glyphs.

    #[test]
    fn core_number_renders_as_text_on_the_filled_bar() {
        // 35% fills 4 of 10 cells, so a 2-wide number plus its margin sit
        // entirely on the fill (as background shading) and the bar
        // continues with one block glyph.
        let line = cpu_bar_line(12, 2, 35, &CpuConfig::default());
        assert_eq!(line.spans[1].content, "12 ");
        assert_eq!(line.spans[1].style.fg, Some(text_default()));
        assert!(line.spans[1].style.bg.is_some());
        assert_eq!(line.spans[2].content, "");
        assert_eq!(line.spans[3].content, "");
    }

    #[test]
    fn core_number_splits_at_the_fill_boundary() {
        // 10% fills 1 cell: the first digit is shaded by the fill, the
        // second digit and the margin render on the row background.
        let line = cpu_bar_line(12, 2, 10, &CpuConfig::default());
        assert_eq!(line.spans[1].content, "1");
        assert_eq!(line.spans[2].content, "2 ");
        assert_eq!(line.spans[2].style.fg, Some(text_default()));
        assert_eq!(line.spans[2].style.bg, None);
        assert_eq!(line.spans[3].content, "");
    }

    #[test]
    fn single_digit_core_number_is_right_aligned() {
        let line = cpu_bar_line(3, 2, 0, &CpuConfig::default());
        assert_eq!(line.spans[1].content, "");
        assert_eq!(line.spans[2].content, " 3 ");
    }

    #[test]
    fn embedded_core_number_keeps_the_content_width() {
        for percent in [0, 10, 35, 100] {
            let line = cpu_bar_line(12, 2, percent, &CpuConfig::default());
            assert_eq!(line.width(), usize::from(CPU_CONTENT_WIDTH));
        }
    }
}