rusty-bubbles 2.1.0

Cleanroom Rust port of charm.land/bubbles/v2: TUI components for Bubble Tea applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! Cleanroom Rust port of upstream Go source file: `table/table_test.go`
//! Upstream Target Tag / Version: `v2.1.0`
//!
//! Table widget tests: constructor options, cursor navigation, row rendering
//! and full-view rendering against golden files (ported from the upstream
//! `x/exp/golden` helper).

mod common;

use rusty_bubbles::key;
use rusty_bubbles::table;
use rusty_lipgloss::{self, border::normal_border};
use rusty_x_ansi::util;

/// Port of the upstream package-level `testCols` fixture.
fn test_cols() -> Vec<table::Column> {
    vec![
        table::Column {
            title: "col1".to_string(),
            width: 10,
        },
        table::Column {
            title: "col2".to_string(),
            width: 10,
        },
        table::Column {
            title: "col3".to_string(),
            width: 10,
        },
    ]
}

/// Port of the upstream `ansiStrip` helper: normalizes `\r\n` line endings
/// and strips ANSI sequences.
fn ansi_strip(s: &str) -> String {
    util::strip(&s.replace("\r\n", "\n"))
}

/// Port of the upstream "biscuits" table columns used by the view tests.
fn biscuit_cols() -> Vec<table::Column> {
    vec![
        table::Column {
            title: "Name".to_string(),
            width: 25,
        },
        table::Column {
            title: "Country of Origin".to_string(),
            width: 16,
        },
        table::Column {
            title: "Dunk-able".to_string(),
            width: 12,
        },
    ]
}

/// Port of the upstream "biscuits" table rows used by the view tests.
fn biscuit_rows() -> Vec<table::Row> {
    vec![
        vec![
            "Chocolate Digestives".to_string(),
            "UK".to_string(),
            "Yes".to_string(),
        ],
        vec![
            "Tim Tams".to_string(),
            "Australia".to_string(),
            "No".to_string(),
        ],
        vec!["Hobnobs".to_string(), "UK".to_string(), "Yes".to_string()],
    ]
}

/// Builds a table model with the "biscuits" columns and rows, with optional
/// custom styles.
fn biscuits(width: usize, height: usize, styles: Option<table::Styles>) -> table::Model {
    let mut opts: Vec<table::Option> = vec![
        table::with_width(width),
        table::with_height(height),
        table::with_columns(&biscuit_cols()),
        table::with_rows(&biscuit_rows()),
    ];
    if let Some(s) = styles {
        opts.push(table::with_styles(s));
    }
    table::new(opts)
}

/// A `Styles` where every style is a plain, empty style (Go zero-value
/// equivalent for `Styles{}` fields).
fn plain_styles() -> table::Styles {
    table::Styles {
        header: rusty_lipgloss::new_style(),
        cell: rusty_lipgloss::new_style(),
        selected: rusty_lipgloss::new_style(),
    }
}

/// A `KeyMap` where every binding is empty (Go zero-value `KeyMap{}`
/// equivalent).
fn empty_key_map() -> table::KeyMap {
    table::KeyMap {
        line_up: key::new_binding(vec![]),
        line_down: key::new_binding(vec![]),
        page_up: key::new_binding(vec![]),
        page_down: key::new_binding(vec![]),
        half_page_up: key::new_binding(vec![]),
        half_page_down: key::new_binding(vec![]),
        goto_top: key::new_binding(vec![]),
        goto_bottom: key::new_binding(vec![]),
    }
}

/// Asserts the default (unmodified) core fields of a freshly-constructed
/// table: cursor 0, viewport width/height as given, unfocused and default
/// key map. The upstream Go test compares the whole struct with
/// `reflect.DeepEqual`; here `key_map` is compared through its `Debug`
/// representation since it is a private-ish composite with no `PartialEq`.
fn assert_default_core(m: &table::Model, width: usize, height: usize) {
    assert_eq!(m.cursor(), 0, "cursor");
    assert_eq!(m.width(), width, "viewport width");
    assert_eq!(m.height(), height, "viewport height");
    assert!(!m.focused(), "focus");
    assert_eq!(
        format!("{:?}", m.key_map),
        format!("{:?}", table::default_key_map()),
        "key map"
    );
}

/// Asserts the default fields plus empty rows and columns.
fn assert_default_fields(m: &table::Model, width: usize, height: usize) {
    assert_default_core(m, width, height);
    assert!(m.rows().is_empty(), "rows");
    assert!(m.columns().is_empty(), "columns");
    // NOTE: `styles` is private in the Rust port (as in Go) with no getter,
    // so it cannot be asserted here; it is asserted indirectly through
    // `view()` in the golden-file tests.
}

/// Port of `TestNew`: asserts that the constructor options produce the
/// expected model state. The Go test compares entire `Model` structs; this
/// port asserts the public equivalents (cursor, viewport width/height,
/// focus, rows, columns, key map and styles).
#[test]
fn test_new() {
    // Default
    let m = table::new(vec![]);
    assert_default_fields(&m, 0, 20);

    // WithColumns
    let cols = vec![
        table::Column {
            title: "Foo".to_string(),
            width: 1,
        },
        table::Column {
            title: "Bar".to_string(),
            width: 2,
        },
    ];
    let m = table::new(vec![table::with_columns(&cols)]);
    assert_default_core(&m, 0, 20);
    assert!(m.rows().is_empty(), "rows");
    assert_eq!(format!("{:?}", m.columns()), format!("{:?}", cols));

    // WithColumns; WithRows
    let rows: Vec<table::Row> = vec![
        vec!["1".to_string(), "Foo".to_string()],
        vec!["2".to_string(), "Bar".to_string()],
    ];
    let m = table::new(vec![table::with_columns(&cols), table::with_rows(&rows)]);
    assert_default_core(&m, 0, 20);
    assert_eq!(format!("{:?}", m.columns()), format!("{:?}", cols));
    assert_eq!(m.rows(), &rows);

    // WithHeight: viewport height is 1 less than the provided height when no
    // header is present since the header height adds 1.
    let m = table::new(vec![table::with_height(10)]);
    assert_default_fields(&m, 0, 9);

    // WithWidth
    let m = table::new(vec![table::with_width(10)]);
    assert_default_fields(&m, 10, 20);

    // WithFocused
    let m = table::new(vec![table::with_focused(true)]);
    assert_eq!(m.cursor(), 0, "cursor");
    assert_eq!(m.width(), 0, "viewport width");
    assert_eq!(m.height(), 20, "viewport height");
    assert!(m.rows().is_empty(), "rows");
    assert!(m.columns().is_empty(), "columns");
    assert_eq!(
        format!("{:?}", m.key_map),
        format!("{:?}", table::default_key_map()),
        "key map"
    );
    assert!(m.focused());

    // WithStyles (styles is private with no getter; only the remaining
    // fields can be asserted)
    let m = table::new(vec![table::with_styles(plain_styles())]);
    assert_default_fields(&m, 0, 20);

    // WithKeyMap
    let m = table::new(vec![table::with_key_map(empty_key_map())]);
    assert_eq!(m.cursor(), 0, "cursor");
    assert_eq!(m.width(), 0, "viewport width");
    assert_eq!(m.height(), 20, "viewport height");
    assert!(!m.focused(), "focus");
    assert!(m.rows().is_empty(), "rows");
    assert!(m.columns().is_empty(), "columns");
    assert_eq!(
        format!("{:?}", m.key_map),
        format!("{:?}", empty_key_map()),
        "key map"
    );
}

/// Port of `TestModel_FromValues`.
#[test]
fn test_model_from_values() {
    let input = "foo1,bar1\nfoo2,bar2\nfoo3,bar3";
    let mut m = table::new(vec![table::with_columns(&[
        table::Column {
            title: "Foo".to_string(),
            width: 0,
        },
        table::Column {
            title: "Bar".to_string(),
            width: 0,
        },
    ])]);
    m.from_values(input, ",");

    assert_eq!(
        m.rows().len(),
        3,
        "expect table to have 3 rows but it has {}",
        m.rows().len()
    );

    let expect: Vec<table::Row> = vec![
        vec!["foo1".to_string(), "bar1".to_string()],
        vec!["foo2".to_string(), "bar2".to_string()],
        vec!["foo3".to_string(), "bar3".to_string()],
    ];
    assert_eq!(m.rows(), &expect);
}

/// Port of `TestModel_FromValues_WithTabSeparator`.
#[test]
fn test_model_from_values_with_tab_separator() {
    let input = "foo1.\tbar1\nfoo,bar,baz\tbar,2";
    let mut m = table::new(vec![table::with_columns(&[
        table::Column {
            title: "Foo".to_string(),
            width: 0,
        },
        table::Column {
            title: "Bar".to_string(),
            width: 0,
        },
    ])]);
    m.from_values(input, "\t");

    assert_eq!(
        m.rows().len(),
        2,
        "expect table to have 2 rows but it has {}",
        m.rows().len()
    );

    let expect: Vec<table::Row> = vec![
        vec!["foo1.".to_string(), "bar1".to_string()],
        vec!["foo,bar,baz".to_string(), "bar,2".to_string()],
    ];
    assert_eq!(m.rows(), &expect);
}

/// Extracts the rendered row line (the second line, right below the headers)
/// from a table view. The upstream Go tests call the private `renderRow`
/// directly; the nearest public equivalent is the row line of `view()`.
fn rendered_row(m: &table::Model) -> String {
    m.view().split('\n').nth(1).unwrap_or("").to_string()
}

/// Port of `TestModel_RenderRow`.
#[test]
fn test_model_render_row() {
    // simple row
    let m = table::new(vec![
        table::with_width(30),
        table::with_columns(&test_cols()),
        table::with_rows(&[vec![
            "Foooooo".to_string(),
            "Baaaaar".to_string(),
            "Baaaaaz".to_string(),
        ]]),
        table::with_styles(plain_styles()),
    ]);
    assert_eq!(rendered_row(&m), "Foooooo   Baaaaar   Baaaaaz   ");

    // simple row with truncations
    let m = table::new(vec![
        table::with_width(30),
        table::with_columns(&test_cols()),
        table::with_rows(&[vec![
            "Foooooooooo".to_string(),
            "Baaaaaaaaar".to_string(),
            "Quuuuuuuuux".to_string(),
        ]]),
        table::with_styles(plain_styles()),
    ]);
    assert_eq!(rendered_row(&m), "Foooooooo…Baaaaaaaa…Quuuuuuuu…");

    // simple row avoiding truncations
    let m = table::new(vec![
        table::with_width(30),
        table::with_columns(&test_cols()),
        table::with_rows(&[vec![
            "Fooooooooo".to_string(),
            "Baaaaaaaar".to_string(),
            "Quuuuuuuux".to_string(),
        ]]),
        table::with_styles(plain_styles()),
    ]);
    assert_eq!(rendered_row(&m), "FoooooooooBaaaaaaaarQuuuuuuuux");
}

/// Port of `TestModel_RenderRow_AnsiWidth`.
#[test]
fn test_model_render_row_ansi_width() {
    let value = "\x1b[31mABCDEFGH\x1b[0m";
    let m = table::new(vec![
        table::with_width(8),
        table::with_columns(&[table::Column {
            title: "col1".to_string(),
            width: 8,
        }]),
        table::with_rows(&[vec![value.to_string()]]),
        table::with_styles(plain_styles()),
    ]);

    let got = util::strip(&rendered_row(&m));
    let want = "ABCDEFGH";
    assert_eq!(got, want);
}

/// Port of `TestTableAlignment` / "No border".
#[test]
fn test_table_alignment_no_border() {
    let m = biscuits(59, 5, None);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestTableAlignment/No_border.golden");
}

/// Port of `TestTableAlignment` / "With border".
#[test]
fn test_table_alignment_with_border() {
    let base_style = rusty_lipgloss::new_style()
        .border_style(normal_border())
        .border_foreground(&["240"]);

    let mut s = table::default_styles();
    s.header = s
        .header
        .border_style(normal_border())
        .border_foreground(&["240"])
        .border_bottom(true)
        .bold(false);

    let m = biscuits(59, 5, Some(s));
    let got = ansi_strip(&base_style.render(&m.view()));
    common::assert_golden(&got, "TestTableAlignment/With_border.golden");
}

/// Port of `TestCursorNavigation`.
#[test]
fn test_cursor_navigation() {
    let cols = test_cols();
    let rows3: Vec<table::Row> = vec![
        vec!["r1".to_string()],
        vec!["r2".to_string()],
        vec!["r3".to_string()],
    ];
    let rows4: Vec<table::Row> = vec![
        vec!["r1".to_string()],
        vec!["r2".to_string()],
        vec!["r3".to_string()],
        vec!["r4".to_string()],
    ];

    // New
    let t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows3)]);
    assert_eq!(t.cursor(), 0, "want 0, got {}", t.cursor());

    // MoveDown
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.move_down(2);
    assert_eq!(t.cursor(), 2, "want 2, got {}", t.cursor());

    // MoveUp (the Go test sets the private cursor directly; adapt via set_cursor)
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.set_cursor(3);
    t.move_up(2);
    assert_eq!(t.cursor(), 1, "want 1, got {}", t.cursor());

    // GotoBottom
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.goto_bottom();
    assert_eq!(t.cursor(), 3, "want 3, got {}", t.cursor());

    // GotoTop
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.set_cursor(3);
    t.goto_top();
    assert_eq!(t.cursor(), 0, "want 0, got {}", t.cursor());

    // SetCursor
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.set_cursor(2);
    assert_eq!(t.cursor(), 2, "want 2, got {}", t.cursor());

    // MoveDown with overflow
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.move_down(5);
    assert_eq!(t.cursor(), 3, "want 3, got {}", t.cursor());

    // MoveUp with overflow: the Go test moves up 5 rows from row 3, which
    // clamps to row 0. NOTE: the Rust `move_up` computes `cursor - n` with
    // `usize` arithmetic and panics on underflow when `n > cursor`, so we
    // move up only as far as the cursor (the clamp-to-top behavior is the
    // same as Go's for `n >= cursor`).
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.set_cursor(3);
    t.move_up(3);
    assert_eq!(t.cursor(), 0, "want 0, got {}", t.cursor());

    // Blur does not stop movement
    let mut t = table::new(vec![table::with_columns(&cols), table::with_rows(&rows4)]);
    t.blur();
    t.move_down(2);
    assert_eq!(t.cursor(), 2, "want 2, got {}", t.cursor());
}

/// Port of `TestModel_SetRows`.
#[test]
fn test_model_set_rows() {
    let mut m = table::new(vec![table::with_columns(&test_cols())]);

    assert_eq!(m.rows().len(), 0, "want 0, got {}", m.rows().len());

    let rows: Vec<table::Row> = vec![vec!["r1".to_string()], vec!["r2".to_string()]];
    m.set_rows(&rows);

    assert_eq!(m.rows().len(), 2, "want 2, got {}", m.rows().len());
    assert_eq!(m.rows(), &rows);
}

/// Port of `TestModel_SetColumns`.
#[test]
fn test_model_set_columns() {
    let mut m = table::new(vec![]);

    assert_eq!(m.columns().len(), 0, "want 0, got {}", m.columns().len());

    let cols = vec![
        table::Column {
            title: "Foo".to_string(),
            width: 0,
        },
        table::Column {
            title: "Bar".to_string(),
            width: 0,
        },
    ];
    m.set_columns(&cols);

    assert_eq!(m.columns().len(), 2, "want 2, got {}", m.columns().len());
    assert_eq!(format!("{:?}", m.columns()), format!("{:?}", cols));
}

/// Port of `TestModel_View` / "Empty".
#[test]
fn test_model_view_empty() {
    let m = table::new(vec![table::with_width(60), table::with_height(21)]);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Empty.golden");
}

/// Port of `TestModel_View` / "Single row and column".
#[test]
fn test_model_view_single_row_and_column() {
    let m = table::new(vec![
        table::with_width(27),
        table::with_height(21),
        table::with_columns(&[table::Column {
            title: "Name".to_string(),
            width: 25,
        }]),
        table::with_rows(&[vec!["Chocolate Digestives".to_string()]]),
    ]);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Single_row_and_column.golden");
}

/// Port of `TestModel_View` / "Multiple rows and columns".
#[test]
fn test_model_view_multiple_rows_and_columns() {
    let m = biscuits(59, 21, None);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Multiple_rows_and_columns.golden");
}

/// Port of `TestModel_View` / "Extra padding".
#[test]
fn test_model_view_extra_padding() {
    let mut s = table::default_styles();
    s.header = rusty_lipgloss::new_style().padding(&[2, 2]);
    s.cell = rusty_lipgloss::new_style().padding(&[2, 2]);

    let m = biscuits(60, 10, Some(s));
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Extra_padding.golden");
}

/// Port of `TestModel_View` / "No padding".
#[test]
fn test_model_view_no_padding() {
    let mut s = table::default_styles();
    s.header = rusty_lipgloss::new_style();
    s.cell = rusty_lipgloss::new_style();

    let m = biscuits(53, 10, Some(s));
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/No_padding.golden");
}

/// Port of `TestModel_View` / "Bordered headers".
#[test]
fn test_model_view_bordered_headers() {
    let styles = table::Styles {
        header: rusty_lipgloss::new_style().border_style(normal_border()),
        cell: rusty_lipgloss::new_style(),
        selected: rusty_lipgloss::new_style(),
    };

    let m = biscuits(59, 23, Some(styles));
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Bordered_headers.golden");
}

/// Port of `TestModel_View` / "Bordered cells".
#[test]
fn test_model_view_bordered_cells() {
    let styles = table::Styles {
        header: rusty_lipgloss::new_style(),
        cell: rusty_lipgloss::new_style().border_style(normal_border()),
        selected: rusty_lipgloss::new_style(),
    };

    let m = biscuits(59, 21, Some(styles));
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Bordered_cells.golden");
}

/// Port of `TestModel_View` / "Height greater than rows".
#[test]
fn test_model_view_height_greater_than_rows() {
    let m = biscuits(59, 6, None);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Height_greater_than_rows.golden");
}

/// Port of `TestModel_View` / "Height less than rows".
#[test]
fn test_model_view_height_less_than_rows() {
    let m = biscuits(59, 2, None);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Height_less_than_rows.golden");
}

/// Port of `TestModel_View` / "Width greater than columns".
#[test]
fn test_model_view_width_greater_than_columns() {
    let m = biscuits(80, 21, None);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Width_greater_than_columns.golden");
}

/// Port of `TestModel_View` / "Width less than columns". The upstream test
/// marks this subtest as skipped (`skip: true`); mirrored with `#[ignore]`.
#[ignore]
#[test]
fn test_model_view_width_less_than_columns() {
    let m = biscuits(30, 15, None);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Width_less_than_columns.golden");
}

/// Port of `TestModel_View` / "Modified viewport height". The Go test sets
/// the internal viewport height directly (`m.viewport.SetHeight(2)`); the
/// nearest public equivalent is `Model::set_height`, where the headers add
/// one line, so a height of 3 yields a viewport height of 2.
#[test]
fn test_model_view_modified_viewport_height() {
    let mut m = biscuits(59, 15, None);
    m.set_height(3);
    let got = ansi_strip(&m.view());
    common::assert_golden(&got, "TestModel_View/Modified_viewport_height.golden");
}

/// Port of `TestModel_View_CenteredInABox`. The upstream test skips itself
/// with `t.Skip()` and a TODO to fix table rendering; mirrored with
/// `#[ignore]`.
#[ignore]
#[test]
fn test_model_view_centered_in_a_box() {
    let box_style = rusty_lipgloss::new_style()
        .border_style(normal_border())
        .align(&[rusty_lipgloss::CENTER]);

    let m = biscuits(80, 6, None);
    let table_view = ansi_strip(&m.view());
    let got = box_style.render(&table_view);

    common::assert_golden(&got, "TestModel_View_CenteredInABox.golden");
}