ratatui-widgettable 0.1.2

A Ratatui table widget whose cells can contain any widget (Paragraph, Block, Gauge, ...)
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
use pretty_assertions::assert_eq;
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Flex, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::widgets::{
    self as upstream, Block, Gauge, HighlightSpacing, Paragraph, StatefulWidget, TableState,
    Widget, Wrap,
};
use ratatui_widgettable::{Cell, Row, Table};

// Note: `scroll_padding` is not compared here as it is not in the published `ratatui-widgets`
// 0.3.2 release yet (it is on ratatui's main branch). It is tested separately below using the
// test cases from ratatui's main branch.

/// (cells as (text, column span), height, top margin, bottom margin)
type RowSpec = (Vec<(&'static str, u16)>, u16, u16, u16);

/// Rows of text described in a way that can be turned into both an upstream table and ours.
struct Spec {
    rows: Vec<RowSpec>,
    header: Option<Vec<&'static str>>,
    footer: Option<Vec<&'static str>>,
    widths: Vec<Constraint>,
    column_spacing: u16,
    flex: Flex,
    highlight_spacing: HighlightSpacing,
    block: bool,
}

impl Spec {
    fn simple(rows: usize, height: u16) -> Self {
        let names: &'static [&'static str] = &[
            "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India",
            "Juliet", "Kilo", "Lima",
        ];
        Self {
            rows: (0..rows)
                .map(|i| {
                    let a = names[i % names.len()];
                    let b = names[(i + 3) % names.len()];
                    let c = names[(i + 7) % names.len()];
                    (vec![(a, 1), (b, 1), (c, 1)], height, 0, 0)
                })
                .collect(),
            header: Some(vec!["Col1", "Col2", "Col3"]),
            footer: None,
            widths: vec![
                Constraint::Length(8),
                Constraint::Fill(1),
                Constraint::Length(8),
            ],
            column_spacing: 1,
            flex: Flex::Start,
            highlight_spacing: HighlightSpacing::WhenSelected,
            block: false,
        }
    }

    fn upstream(&self) -> upstream::Table<'static> {
        let rows = self.rows.iter().map(|(cells, height, top, bottom)| {
            upstream::Row::new(
                cells
                    .iter()
                    .map(|(text, span)| upstream::Cell::new(*text).column_span(*span)),
            )
            .height(*height)
            .top_margin(*top)
            .bottom_margin(*bottom)
            .style(Style::new().italic())
        });
        let mut table = upstream::Table::new(rows, self.widths.clone())
            .column_spacing(self.column_spacing)
            .flex(self.flex)
            .highlight_spacing(self.highlight_spacing.clone())
            .highlight_symbol(">>")
            .row_highlight_style(Style::new().reversed())
            .column_highlight_style(Style::new().red())
            .cell_highlight_style(Style::new().on_blue())
            .style(Style::new().green());
        if let Some(header) = &self.header {
            table = table.header(upstream::Row::new(header.clone()).bold().bottom_margin(1));
        }
        if let Some(footer) = &self.footer {
            table = table.footer(upstream::Row::new(footer.clone()).top_margin(1));
        }
        if self.block {
            table = table.block(Block::bordered().title("T"));
        }
        table
    }

    fn ours(&self) -> Table<'static> {
        let rows = self.rows.iter().map(|(cells, height, top, bottom)| {
            Row::new(
                cells
                    .iter()
                    .map(|(text, span)| Cell::from(*text).column_span(*span)),
            )
            .height(*height)
            .top_margin(*top)
            .bottom_margin(*bottom)
            .style(Style::new().italic())
        });
        let mut table = Table::new(rows, self.widths.clone())
            .column_spacing(self.column_spacing)
            .flex(self.flex)
            .highlight_spacing(self.highlight_spacing.clone())
            .highlight_symbol(">>")
            .row_highlight_style(Style::new().reversed())
            .column_highlight_style(Style::new().red())
            .cell_highlight_style(Style::new().on_blue())
            .style(Style::new().green());
        if let Some(header) = &self.header {
            table = table.header(Row::new(header.clone()).bold().bottom_margin(1));
        }
        if let Some(footer) = &self.footer {
            table = table.footer(Row::new(footer.clone()).top_margin(1));
        }
        if self.block {
            table = table.block(Block::bordered().title("T"));
        }
        table
    }

    /// Renders both tables with a copy of `state` and asserts that the buffers and resulting
    /// states are identical.
    fn assert_parity(&self, area: Rect, state: TableState) {
        let mut expected_state = state;
        let mut expected = Buffer::empty(area);
        StatefulWidget::render(self.upstream(), area, &mut expected, &mut expected_state);

        let mut actual_state = state;
        let mut actual = Buffer::empty(area);
        StatefulWidget::render(self.ours(), area, &mut actual, &mut actual_state);

        assert_eq!(actual, expected, "buffer mismatch for state {state:?}");
        assert_eq!(actual_state, expected_state, "state mismatch for {state:?}");
    }
}

fn states(rows: usize, columns: usize) -> Vec<TableState> {
    let mut states = vec![TableState::default()];
    for offset in [0, 3, rows + 5] {
        for selected in [
            None,
            Some(0),
            Some(rows / 2),
            Some(rows.saturating_sub(1)),
            Some(rows + 2),
        ] {
            for column in [None, Some(0), Some(columns + 1)] {
                states.push(
                    TableState::new()
                        .with_offset(offset)
                        .with_selected(selected)
                        .with_selected_column(column),
                );
            }
        }
    }
    states
}

#[test]
fn parity_basic() {
    let spec = Spec::simple(20, 1);
    for state in states(20, 3) {
        spec.assert_parity(Rect::new(0, 0, 30, 8), state);
    }
}

#[test]
fn parity_tall_rows_block_footer() {
    let mut spec = Spec::simple(15, 2);
    spec.block = true;
    spec.footer = Some(vec!["F1", "F2", "F3"]);
    spec.highlight_spacing = HighlightSpacing::Always;
    for state in states(15, 3) {
        spec.assert_parity(Rect::new(2, 1, 32, 14), state);
    }
}

#[test]
fn parity_margins_spans_flex() {
    let mut spec = Spec::simple(6, 1);
    spec.rows[1].0 = vec![("spans two columns", 2), ("x", 1)];
    spec.rows[2].0 = vec![("all three columns wide", 3)];
    spec.rows[3].2 = 1;
    spec.rows[4].3 = 2;
    spec.column_spacing = 2;
    spec.flex = Flex::SpaceBetween;
    spec.widths = vec![Constraint::Length(5); 3];
    spec.highlight_spacing = HighlightSpacing::Never;
    for state in states(6, 3) {
        spec.assert_parity(Rect::new(0, 0, 40, 9), state);
    }
}

#[test]
fn parity_empty_and_default_widths() {
    let mut spec = Spec::simple(0, 1);
    for state in states(0, 3) {
        spec.assert_parity(Rect::new(0, 0, 20, 4), state);
    }
    spec = Spec::simple(4, 1);
    spec.widths.clear();
    for state in states(4, 3) {
        spec.assert_parity(Rect::new(0, 0, 21, 4), state);
    }
}

#[test]
fn render_wrapped_paragraph_in_cell() {
    let rows = [
        Row::new(vec![
            Cell::from("id-1"),
            Cell::new(
                Paragraph::new("the quick brown fox jumps over the lazy dog")
                    .wrap(Wrap { trim: true }),
            ),
        ])
        .height(3),
        Row::new(vec!["id-2", "short"]),
    ];
    let table = Table::new(rows, [Constraint::Length(4), Constraint::Length(15)]);
    let mut buf = Buffer::empty(Rect::new(0, 0, 20, 4));
    Widget::render(table, buf.area, &mut buf);
    assert_eq!(
        buf,
        Buffer::with_lines([
            "id-1 the quick brown",
            "     fox jumps over ",
            "     the lazy dog   ",
            "id-2 short          ",
        ])
    );
}

#[test]
fn render_block_and_gauge_in_cells() {
    let rows = [Row::new(vec![
        Cell::new(Paragraph::new("hi").block(Block::bordered())),
        Cell::new(Gauge::default().percent(50).label("50%")),
    ])
    .height(3)];
    let table = Table::new(rows, [Constraint::Length(6), Constraint::Length(6)]);
    let mut buf = Buffer::empty(Rect::new(0, 0, 13, 3));
    Widget::render(table, buf.area, &mut buf);
    // only compare the symbols, the gauge applies styles to its filled half
    let symbols: Vec<String> = (0..3)
        .map(|y| (0..13).map(|x| buf[(x, y)].symbol()).collect())
        .collect();
    assert_eq!(
        symbols,
        ["┌────┐ ███   ", "│hi  │ █50%  ", "└────┘ ███   ",]
    );
}

#[test]
fn render_nested_table() {
    let inner = Table::new(
        [Row::new(vec!["a", "b"]), Row::new(vec!["c", "d"])],
        [Constraint::Length(1), Constraint::Length(1)],
    );
    let rows = [Row::new(vec![Cell::from("outer"), Cell::new(inner)]).height(2)];
    let table = Table::new(rows, [Constraint::Length(5), Constraint::Length(3)]);
    let mut buf = Buffer::empty(Rect::new(0, 0, 9, 2));
    Widget::render(table, buf.area, &mut buf);
    assert_eq!(buf, Buffer::with_lines(["outer a b", "      c d"]));
}

#[test]
fn render_from_fn_cell_gets_cell_area() {
    let rows = [
        Row::new(vec!["x"]),
        Row::new(vec![
            Cell::from("y"),
            Cell::from_fn(|area, buf| {
                let text = format!("{},{} {}x{}", area.x, area.y, area.width, area.height);
                buf.set_string(area.x, area.y, text, Style::new());
            }),
        ])
        .height(2),
    ];
    let table =
        Table::new(rows, [Constraint::Length(1), Constraint::Length(10)]).block(Block::bordered());
    let mut buf = Buffer::empty(Rect::new(0, 0, 14, 5));
    Widget::render(table, buf.area, &mut buf);
    assert_eq!(
        buf,
        Buffer::with_lines([
            "┌────────────┐",
            "│x           │",
            "│y 3,2 10x2  │",
            "│            │",
            "└────────────┘",
        ])
    );
}

#[test]
fn widget_cells_are_clipped_to_visible_area() {
    // A row taller than the remaining space is rendered as a partial row, and the widget inside
    // is given the clipped area.
    let rows = [
        Row::new(vec!["1"]),
        Row::new(vec![Cell::new(Paragraph::new("a\nb\nc"))]).height(3),
    ];
    let table = Table::new(rows, [Constraint::Length(3)]);
    let mut buf = Buffer::empty(Rect::new(0, 0, 3, 3));
    Widget::render(table, buf.area, &mut buf);
    assert_eq!(buf, Buffer::with_lines(["1  ", "a  ", "b  "]));
}

#[test]
fn highlight_applies_over_widget_cells() {
    let rows = [
        Row::new(vec![Cell::new(Paragraph::new("one"))]),
        Row::new(vec![Cell::new(Paragraph::new("two"))]),
    ];
    let table = Table::new(rows, [Constraint::Length(3)])
        .highlight_symbol(">")
        .row_highlight_style(Style::new().reversed());
    let mut state = TableState::new().with_selected(Some(1));
    let mut buf = Buffer::empty(Rect::new(0, 0, 4, 2));
    StatefulWidget::render(table, buf.area, &mut buf, &mut state);
    let mut expected = Buffer::with_lines([" one", ">two"]);
    expected.set_style(Rect::new(0, 1, 4, 1), Style::new().reversed());
    assert_eq!(buf, expected);
}

/// Test cases ported from ratatui's main branch (`ratatui-widgets/src/table.rs`).
#[test]
fn scroll_padding() {
    // (name, render height, offset, padding, selected, expected offset, expected selected)
    let cases = [
        ("padding_scroll_down", 4, 0, 1, Some(3), 1, Some(3)),
        ("padding_scroll_up", 4, 4, 1, Some(2), 1, Some(2)),
        ("no_padding_offset_behavior", 5, 2, 0, Some(3), 2, Some(3)),
        ("padding_two_before", 5, 2, 2, Some(3), 1, Some(3)),
        (
            "padding_keep_selected_visible",
            4,
            0,
            4,
            Some(1),
            0,
            Some(1),
        ),
        ("no_selection_no_padding", 4, 2, 1, None, 2, None),
        (
            "maximum_padding_when_all_rows_fit",
            6,
            0,
            usize::MAX,
            Some(3),
            0,
            Some(3),
        ),
        (
            "maximum_padding_when_rows_do_not_fit",
            4,
            0,
            usize::MAX,
            Some(3),
            1,
            Some(3),
        ),
    ];
    for (name, height, offset, padding, selected, expected_offset, expected_selected) in cases {
        let rows = (0..6).map(|i| Row::new(vec![format!("Row {i}")]));
        let table = Table::new(rows, [Constraint::Length(10)]).scroll_padding(padding);
        let mut buf = Buffer::empty(Rect::new(0, 0, 10, height));
        let mut state = TableState::new()
            .with_offset(offset)
            .with_selected(selected);
        StatefulWidget::render(table, buf.area, &mut buf, &mut state);
        assert_eq!(state.offset(), expected_offset, "{name}: offset mismatch");
        assert_eq!(
            state.selected(),
            expected_selected,
            "{name}: selected mismatch"
        );
    }
}

#[test]
fn scroll_padding_is_stable_across_renders() {
    let rows = (0..8).map(|i| Row::new(vec![format!("Row {i}")]));
    let table = Table::new(rows, [Constraint::Length(10)]).scroll_padding(3);
    let mut buf = Buffer::empty(Rect::new(0, 0, 10, 5));
    let mut state = TableState::new().with_offset(2).with_selected(Some(4));
    StatefulWidget::render(&table, buf.area, &mut buf, &mut state);
    let offset_after_render = state.offset();
    StatefulWidget::render(&table, buf.area, &mut buf, &mut state);
    assert_eq!(offset_after_render, state.offset());
}

// Regression tests for bugs found by fuzzing (see fuzz/README.md). These also exist in ratatui's
// table.

#[test]
fn tall_rows_do_not_overflow() {
    let table = Table::new([Row::new(["a"]).height(u16::MAX)], [Constraint::Length(1)]);
    let mut buf = Buffer::empty(Rect::new(0, 1, 5, 5));
    Widget::render(table, buf.area, &mut buf);

    let rows = [Row::new(["a"]).height(3), Row::new(["b"]).height(u16::MAX)];
    let table = Table::new(rows, [Constraint::Length(1)]);
    let mut buf = Buffer::empty(Rect::new(0, 0, 5, 5));
    Widget::render(table, buf.area, &mut buf);
}

#[test]
fn column_span_with_large_spacing_does_not_overflow() {
    let rows = [Row::new([Cell::from("a").column_span(3)])];
    let table = Table::new(rows, [Constraint::Length(1); 3]).column_spacing(u16::MAX);
    let mut buf = Buffer::empty(Rect::new(0, 0, 5, 1));
    Widget::render(table, buf.area, &mut buf);
}

/// Renders into the left 4 columns of a 10x3 buffer, to check nothing is drawn outside the table.
fn render_narrow(table: Table, state: &mut TableState) -> Buffer {
    let mut buf = Buffer::filled(Rect::new(0, 0, 10, 3), ratatui::buffer::Cell::new("."));
    StatefulWidget::render(table, Rect::new(0, 0, 4, 1), &mut buf, state);
    buf
}

#[test]
fn wide_highlight_symbol_is_clipped_to_table() {
    let table = Table::new([Row::new(["a"])], [Constraint::Length(1)]).highlight_symbol(">>>>>>");
    let buf = render_narrow(table, &mut TableState::new().with_selected(Some(0)));
    assert_eq!(
        buf,
        Buffer::with_lines([">>>>......", "..........", ".........."])
    );
}

#[test]
fn spanning_cell_is_clipped_to_table() {
    let rows = [Row::new([Cell::from("abcdefghij").column_span(3)])];
    let table = Table::new(rows, [Constraint::Length(3); 3]).column_spacing(3);
    let buf = render_narrow(table, &mut TableState::new());
    assert_eq!(
        buf,
        Buffer::with_lines(["abcd......", "..........", ".........."])
    );
}

#[test]
fn row_pushed_out_by_margin_is_not_rendered() {
    // The row's top margin pushes it below the table, so its cell gets an empty area outside of
    // the table. The cell must not be rendered there.
    let rows = [Row::new([Cell::from_fn(|area, buf| {
        buf.set_string(area.x, area.y, "x", Style::new());
    })])
    .top_margin(2)];
    let table = Table::new(rows, [Constraint::Length(4)]);
    let buf = render_narrow(table, &mut TableState::new());
    assert_eq!(buf, Buffer::with_lines([".........."; 3]));
}