tuitab 0.7.0

Terminal tabular data explorer — CSV/JSON/YAML/TOML/Parquet/Excel/SQLite viewer with filtering, sorting, pivot tables, and charts
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
use crate::app::App;
use crate::data::dataframe::DataFrame;
use crate::theme::EverforestTheme as T;
use ratatui::layout::{Constraint, Margin, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
    Block, BorderType, Cell, HighlightSpacing, Row, Scrollbar, ScrollbarOrientation,
    ScrollbarState, Table,
};
use ratatui::Frame;
use unicode_width::UnicodeWidthStr;

/// Render the active sheet's data table with header, rows, selection marks, and scrollbar.
pub fn render(frame: &mut Frame, app: &mut App, area: Rect) {
    let stack_depth = app.stack.depth();
    let sheet = app.stack.active_mut();

    let (visible_cols, widths_override) = build_column_plan(
        &sheet.dataframe,
        sheet.cursor_col,
        &mut sheet.left_col,
        area,
    );
    let aggregates = sheet.dataframe.compute_aggregates();
    let df = &sheet.dataframe;
    let max_aggs = aggregates.iter().map(|a| a.len()).max().unwrap_or(0) as u16;
    let footer_height = max_aggs;
    let non_row_height = 3 + footer_height;

    let cursor_col = sheet.cursor_col;
    let sort_keys = sheet.sort_keys.as_slice();
    let active_display_row = sheet.table_state.selected().unwrap_or(0);
    let table_height = area.height.saturating_sub(non_row_height) as usize;

    let mut top_row = sheet.top_row;
    if active_display_row < top_row {
        top_row = active_display_row;
    } else if active_display_row >= top_row + table_height && table_height > 0 {
        top_row = active_display_row.saturating_sub(table_height) + 1;
    }
    let max_top = df.visible_row_count().saturating_sub(table_height);
    top_row = top_row.min(max_top);
    let end_row = (top_row + table_height).min(df.visible_row_count());

    let header = make_header_row(&visible_cols, &widths_override, df, cursor_col, sort_keys);
    let data_rows = make_data_rows(
        &visible_cols,
        &widths_override,
        df,
        cursor_col,
        top_row,
        end_row,
        active_display_row,
    );

    let widths: Vec<Constraint> = widths_override
        .iter()
        .map(|&w| Constraint::Length(w))
        .collect();

    let title = format!(
        " {}{}{} ",
        sheet.title,
        if df.modified { " [*]" } else { "" },
        if stack_depth > 1 {
            format!(" [{}/{}]", stack_depth, stack_depth)
        } else {
            String::new()
        }
    );

    let make_block = |title: String| {
        Block::bordered()
            .title(title)
            .border_type(BorderType::Rounded)
            .border_style(T::separator_style())
            .style(Style::default().bg(T::BG0))
    };

    let table = if footer_height > 0 {
        let footer = make_footer_row(&visible_cols, &widths_override, &aggregates, footer_height);
        Table::new(data_rows, &widths)
            .header(header)
            .footer(footer)
            .highlight_spacing(HighlightSpacing::Always)
            .highlight_symbol("")
            .column_spacing(0)
            .block(make_block(title))
    } else {
        Table::new(data_rows, &widths)
            .header(header)
            .highlight_spacing(HighlightSpacing::Always)
            .highlight_symbol("")
            .column_spacing(0)
            .block(make_block(title))
    };

    sheet.top_row = top_row;

    let relative_col = visible_cols
        .iter()
        .position(|&c| c == cursor_col)
        .unwrap_or(0);
    let mut relative_state = ratatui::widgets::TableState::default()
        .with_selected(Some(active_display_row.saturating_sub(top_row)))
        .with_selected_column(Some(relative_col));

    frame.render_stateful_widget(table, area, &mut relative_state);

    frame.render_stateful_widget(
        Scrollbar::default()
            .orientation(ScrollbarOrientation::VerticalRight)
            .style(T::scrollbar_style()),
        area.inner(Margin {
            vertical: 1,
            horizontal: 0,
        }),
        &mut sheet.scroll_state,
    );

    let mut horizontal_scroll =
        ScrollbarState::new(df.col_count().saturating_sub(1)).position(cursor_col);

    frame.render_stateful_widget(
        Scrollbar::default()
            .orientation(ScrollbarOrientation::HorizontalBottom)
            .style(T::scrollbar_style()),
        area.inner(Margin {
            vertical: 0,
            horizontal: 1,
        }),
        &mut horizontal_scroll,
    );

    // Compute viewport-clip indicator for the cursor column. `widths_override[pos]`
    // is the chars allocated to the column on this frame; when it is less than
    // the column's stored `width`, content wider than `shown` is hidden on the
    // right by the viewport (column at edge / not enough room).
    let cursor_overflow: Option<(u16, u16)> = visible_cols
        .iter()
        .position(|&c| c == cursor_col)
        .and_then(|pos| {
            let shown = *widths_override.get(pos)?;
            let stored = df.columns.get(cursor_col)?.width;
            (shown < stored).then_some((shown, stored))
        });
    app.cursor_cell_overflow = cursor_overflow;
}

/// Compute which columns are visible and their pixel widths given the available area.
///
/// Returns `(visible_cols, widths_override)` where `visible_cols` is a list of column
/// indices (with `usize::MAX` as a separator marker between pinned and unpinned blocks)
/// and `widths_override` is the matching pixel width for each slot.
fn build_column_plan(
    df: &DataFrame,
    cursor_col: usize,
    left_col_state: &mut usize,
    area: Rect,
) -> (Vec<usize>, Vec<u16>) {
    // Subtract 4: 2 for the table block border + 2 for the always-reserved
    // highlight symbol "▶ " before the first column. Leaving these out causes
    // the last visible column to be silently clipped by ratatui below the
    // width we hand it.
    let max_width = area.width.saturating_sub(4);

    let pinned_cols: Vec<usize> = df
        .columns
        .iter()
        .enumerate()
        .filter(|(_, c)| c.pinned)
        .map(|(i, _)| i)
        .collect();
    let unpinned_cols: Vec<usize> = df
        .columns
        .iter()
        .enumerate()
        .filter(|(_, c)| !c.pinned)
        .map(|(i, _)| i)
        .collect();

    let mut pinned_width: u16 = 0;
    let mut visible_pinned: Vec<usize> = Vec::new();
    for &i in &pinned_cols {
        let w = df.columns[i].width + 1;
        if pinned_width + w > max_width {
            break;
        }
        pinned_width += w;
        visible_pinned.push(i);
    }

    let remaining_width = max_width.saturating_sub(pinned_width);

    let mut left_col = *left_col_state;
    if !unpinned_cols.contains(&left_col) {
        left_col = unpinned_cols.first().copied().unwrap_or(0);
    }

    if unpinned_cols.contains(&cursor_col) {
        if let Some(pos) = unpinned_cols.iter().position(|&x| x == cursor_col) {
            let left_pos = unpinned_cols
                .iter()
                .position(|&x| x == left_col)
                .unwrap_or(0);

            if pos < left_pos {
                left_col = cursor_col;
            } else {
                loop {
                    let mut w = 0;
                    let current_left_pos = unpinned_cols
                        .iter()
                        .position(|&x| x == left_col)
                        .unwrap_or(0);
                    for &col_idx in unpinned_cols.iter().take(pos + 1).skip(current_left_pos) {
                        w += df.columns[col_idx].width + 1;
                    }
                    if w <= remaining_width || left_col == cursor_col {
                        break;
                    }
                    if let Some(next_pos) = unpinned_cols
                        .iter()
                        .position(|&x| x == left_col)
                        .map(|p| p + 1)
                    {
                        if next_pos < unpinned_cols.len() {
                            left_col = unpinned_cols[next_pos];
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }
            }
        }
    }

    *left_col_state = left_col;

    let mut visible_unpinned: Vec<usize> = Vec::new();
    let mut widths_override: Vec<u16> = Vec::new();

    for &i in &visible_pinned {
        widths_override.push(df.columns[i].width + 1);
    }

    let insert_border = !visible_pinned.is_empty() && !unpinned_cols.is_empty();
    let mut border_added = false;
    let mut current_w = 0;

    if insert_border {
        let border_w = 1;
        if remaining_width > border_w {
            current_w += border_w;
            border_added = true;
        }
    }

    if let Some(start_idx) = unpinned_cols.iter().position(|&x| x == left_col) {
        let mut idx = start_idx;
        while idx < unpinned_cols.len() {
            let col_idx = unpinned_cols[idx];
            let col_w = df.columns[col_idx].width + 1;
            if current_w + col_w > remaining_width {
                let diff = remaining_width.saturating_sub(current_w);
                if diff > 0 {
                    if border_added && visible_unpinned.is_empty() {
                        widths_override.push(1);
                    }
                    widths_override.push(diff);
                    visible_unpinned.push(col_idx);
                } else if idx == start_idx {
                    if border_added && visible_unpinned.is_empty() {
                        widths_override.push(1);
                    }
                    widths_override.push(remaining_width);
                    visible_unpinned.push(col_idx);
                }
                break;
            }
            if border_added && visible_unpinned.is_empty() {
                widths_override.push(1);
            }
            widths_override.push(col_w);
            visible_unpinned.push(col_idx);
            current_w += col_w;
            idx += 1;
        }
    }

    let mut visible_cols = visible_pinned;
    if border_added && !visible_unpinned.is_empty() {
        visible_cols.push(usize::MAX);
    }
    visible_cols.extend(visible_unpinned);

    (visible_cols, widths_override)
}

/// Cut `text` down to `width` display columns, never splitting a wide character.
fn clip(text: &str, width: usize) -> String {
    let mut out = String::new();
    let mut used = 0usize;
    for c in text.chars() {
        let w = UnicodeWidthStr::width(c.to_string().as_str());
        if used + w > width {
            break;
        }
        used += w;
        out.push(c);
    }
    out
}

/// Compose the header text for one column, dropping the least informative part
/// first when it does not fit.
///
/// Column widths come from [`crate::data::DataFrame::calc_column_width`], which
/// measures the name alone — `!`, `*` and the sort arrow have no room reserved.
/// Truncating the whole string from the right, which is what used to happen,
/// therefore cut the arrow off every column narrow enough to be sized by its own
/// name: the arrow is last. A name has spare information in it, the marks do
/// not, so the name gives way instead.
///
/// The two columns held back are the leading space and the trailing type icon,
/// which the caller draws either side of this text.
fn fit_header(name: &str, pin: &str, sel: &str, sort: &str, cell_w: usize) -> String {
    let budget = cell_w.saturating_sub(2);
    let prefix = format!("{}{}", pin, sel);
    let prefix_w = UnicodeWidthStr::width(prefix.as_str());
    if prefix_w >= budget {
        return clip(&prefix, budget);
    }

    // Keep the arrow only while a character of the name survives beside it: a
    // cell holding nothing but `▲` names no column.
    let sort = if prefix_w + UnicodeWidthStr::width(sort) < budget {
        sort
    } else {
        ""
    };
    let for_name = budget - prefix_w - UnicodeWidthStr::width(sort);
    format!("{}{}{}", prefix, clip(name, for_name), sort)
}

/// Build the header row: column names with sort arrows, type icons, pin/select markers.
fn make_header_row(
    visible_cols: &[usize],
    widths_override: &[u16],
    df: &DataFrame,
    cursor_col: usize,
    sort_keys: &[(String, bool)],
) -> Row<'static> {
    let header_cells: Vec<Cell> = visible_cols
        .iter()
        .enumerate()
        .map(|(i, &actual_col_idx)| {
            if actual_col_idx == usize::MAX {
                return Cell::from(Span::styled("", T::separator_style()));
            }

            let col = &df.columns[actual_col_idx];
            let icon_ch = col.col_type.icon();
            let icon_str = icon_ch.to_string();

            // An arrow for the sort key, plus its rank when more than one is
            // active — otherwise a compound sort looks like several independent
            // ones.
            let sort_mark = match sort_keys.iter().position(|(n, _)| *n == col.name) {
                Some(rank) => {
                    let arrow = if sort_keys[rank].1 { "" } else { "" };
                    if sort_keys.len() > 1 {
                        format!(" {}{}", arrow, rank + 1)
                    } else {
                        format!(" {}", arrow)
                    }
                }
                None => String::new(),
            };
            let pin_mark = if col.pinned { "!" } else { "" };
            let sel_mark = if col.selected { "*" } else { "" };
            let cell_w = widths_override[i] as usize;

            let name_display = fit_header(&col.name, pin_mark, sel_mark, &sort_mark, cell_w);
            let padding = cell_w.saturating_sub(UnicodeWidthStr::width(name_display.as_str()) + 2);

            let (name_style, icon_style) = if actual_col_idx == cursor_col {
                (
                    T::selected_col_header_style(),
                    Style::default()
                        .fg(Color::Rgb(0x23, 0x2A, 0x2E))
                        .bg(T::AQUA)
                        .add_modifier(Modifier::BOLD | Modifier::ITALIC),
                )
            } else {
                (
                    T::header_style(),
                    Style::default()
                        .fg(T::BG3)
                        .bg(T::GREEN)
                        .add_modifier(Modifier::BOLD | Modifier::ITALIC),
                )
            };

            let spaces = " ".repeat(padding);
            let line = Line::from(vec![
                Span::styled(" ", name_style),
                Span::styled(name_display, name_style),
                Span::styled(spaces, name_style),
                Span::styled(icon_str, icon_style),
            ]);
            Cell::from(line)
        })
        .collect();

    Row::new(header_cells).style(T::header_style()).height(1)
}

/// Build the visible data rows for the current viewport.
fn make_data_rows(
    visible_cols: &[usize],
    widths_override: &[u16],
    df: &DataFrame,
    cursor_col: usize,
    top_row: usize,
    end_row: usize,
    active_display_row: usize,
) -> Vec<Row<'static>> {
    (top_row..end_row)
        .map(|display_row| {
            let physical = df.row_order[display_row];
            let is_selected = df.selected_rows.contains(&physical);
            let is_active = display_row == active_display_row;

            let cells: Vec<Cell> = visible_cols
                .iter()
                .enumerate()
                .map(|(i, &col)| {
                    if col == usize::MAX {
                        return Cell::from(Span::styled("", T::separator_style()));
                    }

                    let mut text = DataFrame::anyvalue_to_string_fmt(&df.get_val(display_row, col));
                    let col_meta = &df.columns[col];
                    let mut is_negative_currency = false;
                    if !text.is_empty() {
                        let p = col_meta.precision as usize;
                        if col_meta.col_type == crate::types::ColumnType::Percentage {
                            if let Ok(f) = text.parse::<f64>() {
                                text = format!("{:.*}%", p, f * 100.0);
                            }
                        } else if col_meta.col_type == crate::types::ColumnType::Currency {
                            if let Ok(f) = text.parse::<f64>() {
                                let sym = col_meta.currency.map(|k| k.symbol()).unwrap_or("$");
                                let prefix =
                                    col_meta.currency.map(|k| k.is_prefix()).unwrap_or(true);
                                if f < 0.0 {
                                    is_negative_currency = true;
                                    let abs_f = f.abs();
                                    if prefix {
                                        text = format!("({}{:.*})", sym, p, abs_f);
                                    } else {
                                        text = format!("({:.*}{})", p, abs_f, sym);
                                    }
                                } else if prefix {
                                    text = format!("{}{:.*}", sym, p, f);
                                } else {
                                    text = format!("{:.*}{}", p, f, sym);
                                }
                            }
                        } else if col_meta.col_type == crate::types::ColumnType::Float {
                            if let Ok(f) = text.parse::<f64>() {
                                text = format!("{:.*}", p, f);
                            }
                        } else if col_meta.col_type == crate::types::ColumnType::FileSize {
                            if let Ok(n) = text.parse::<i64>() {
                                text = if n < 0 {
                                    "-".to_string()
                                } else {
                                    crate::data::io::format_file_size_pub(n as u64)
                                };
                            }
                        }
                    }
                    let is_active_col = col == cursor_col;
                    let display_chars = widths_override[i] as usize;

                    let truncated_text: String = text
                        .chars()
                        .scan(0usize, |acc, c: char| {
                            if c == '\n' {
                                return None;
                            }
                            let w = UnicodeWidthStr::width(c.to_string().as_str());
                            if *acc + w <= display_chars {
                                *acc += w;
                                Some(c)
                            } else {
                                None
                            }
                        })
                        .collect();

                    let mut style = match (is_active, is_selected, is_active_col) {
                        (true, true, true) => T::selected_active_col_style(),
                        (true, true, false) => T::selected_active_row_style(),
                        (true, false, true) => T::active_row_col_style(),
                        (true, false, false) => T::active_row_style(),
                        (false, true, _) => T::selected_mark_style(display_row),
                        (false, false, _) => T::normal_row_style(display_row),
                    };

                    if is_negative_currency && !is_selected && !is_active {
                        style = style.fg(T::RED);
                    }

                    if !is_selected
                        && !is_active
                        && df.columns.len() == 5
                        && df.columns[0].name == "Name"
                        && df.columns[1].name == "Is Directory"
                        && df.columns[4].name == "Supported"
                        && col == 0
                    {
                        let is_dir = DataFrame::anyvalue_to_string_fmt(&df.get_val(display_row, 1));
                        let is_supported =
                            DataFrame::anyvalue_to_string_fmt(&df.get_val(display_row, 4));
                        if is_dir == "true" {
                            style = style.fg(T::BLUE);
                        } else if is_supported == "true" {
                            style = style.fg(T::GREEN);
                        } else {
                            style = style.fg(T::RED);
                        }
                    }

                    Cell::from(truncated_text).style(style)
                })
                .collect();

            let row_style = if is_active && is_selected {
                T::selected_active_row_style()
            } else if is_active {
                T::active_row_style()
            } else if is_selected {
                T::selected_mark_style(display_row)
            } else {
                T::normal_row_style(display_row)
            };
            Row::new(cells).style(row_style)
        })
        .collect()
}

/// Build the aggregates footer row.
fn make_footer_row(
    visible_cols: &[usize],
    widths_override: &[u16],
    aggregates: &[Vec<(crate::data::aggregator::AggregatorKind, String)>],
    footer_height: u16,
) -> Row<'static> {
    let footer_cells: Vec<Cell> = visible_cols
        .iter()
        .enumerate()
        .map(|(i, &col_idx)| {
            if col_idx == usize::MAX {
                return Cell::from(Span::styled("", T::separator_style()));
            }
            let col_aggs = &aggregates[col_idx];
            if col_aggs.is_empty() {
                Cell::from("")
            } else {
                let display_chars = widths_override[i] as usize;
                let text: Vec<String> = col_aggs
                    .iter()
                    .map(|(agg, val)| {
                        let full = format!("{}={}", agg.name(), val);
                        full.chars()
                            .scan(0usize, |acc, c: char| {
                                let w = UnicodeWidthStr::width(c.to_string().as_str());
                                if *acc + w <= display_chars {
                                    *acc += w;
                                    Some(c)
                                } else {
                                    None
                                }
                            })
                            .collect()
                    })
                    .collect();
                Cell::from(text.join("\n")).style(T::footer_style())
            }
        })
        .collect();

    Row::new(footer_cells)
        .style(T::footer_style())
        .height(footer_height)
}

#[cfg(test)]
mod header_fit {
    use super::fit_header;
    use unicode_width::UnicodeWidthStr;

    /// The width of a header cell's text, given what the caller draws around it.
    fn budget(cell_w: usize) -> usize {
        cell_w.saturating_sub(2)
    }

    /// The regression the user hit: a column whose width came from its own name
    /// has exactly zero slack, so appending the arrow overflowed and the arrow —
    /// being last — was what got cut. `age` is 3 wide, so its cell is 5.
    #[test]
    fn a_column_sized_by_its_name_still_shows_the_sort_arrow() {
        let fitted = fit_header("age", "", "", "", 5);
        assert!(
            fitted.contains(''),
            "the arrow is the point of the header: {:?}",
            fitted
        );
    }

    /// Rank digits make a compound sort three columns wide, not two. The
    /// compound sort is new, so nothing had ever asked for that third column.
    #[test]
    fn a_compound_sort_keeps_its_rank_digit() {
        let fitted = fit_header("age", "", "", " ▲2", 8);
        assert_eq!(fitted, "age ▲2");
    }

    /// With room to spare nothing is touched.
    #[test]
    fn a_wide_column_is_left_alone() {
        assert_eq!(fit_header("amount", "!", "*", "", 20), "!*amount ▼");
    }

    /// The name gives way, the marks do not — but only while a character of the
    /// name survives. A cell holding nothing but an arrow names no column.
    #[test]
    fn the_arrow_goes_when_no_name_would_be_left() {
        // Budget 4, marks 2: keeping the arrow too would leave nothing of the
        // name, so the arrow is what goes.
        assert_eq!(fit_header("age", "!", "*", "", 6), "!*ag");
        // One column more and both fit.
        assert_eq!(fit_header("age", "!", "*", "", 7), "!*a ▲");
    }

    /// Whatever it drops, the cell must come out exactly the width it was given:
    /// the caller draws a leading space and a trailing type icon around this
    /// text and pads the gap, so a byte too many pushes every column to the
    /// right out of place, and a byte too few leaves a hole.
    #[test]
    fn the_cell_comes_out_exactly_the_width_it_was_given() {
        for cell_w in 2..24usize {
            for sort in ["", "", " ▼2"] {
                for (pin, sel) in [("", ""), ("!", ""), ("!", "*")] {
                    let fitted = fit_header("department", pin, sel, sort, cell_w);
                    let text_w = UnicodeWidthStr::width(fitted.as_str());
                    assert!(
                        text_w <= budget(cell_w),
                        "width {} overflows cell {}: {:?}",
                        text_w,
                        cell_w,
                        fitted
                    );
                    // What `make_header_row` lays down: space + text + padding + icon.
                    let padding = cell_w.saturating_sub(text_w + 2);
                    assert_eq!(
                        1 + text_w + padding + 1,
                        cell_w,
                        "cell {} drawn at the wrong width: {:?}",
                        cell_w,
                        fitted
                    );
                }
            }
        }
    }

    /// And across every width where an arrow can fit at all, it does.
    #[test]
    fn the_arrow_survives_at_every_width_that_can_hold_it() {
        for cell_w in 5..24usize {
            let fitted = fit_header("department", "", "", "", cell_w);
            assert!(
                fitted.contains(''),
                "width {} dropped the arrow: {:?}",
                cell_w,
                fitted
            );
        }
    }
}