lv-tui 0.4.0

A reactive TUI framework for Rust
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
/// Integration tests for all built-in widgets using Pilot.
///
/// Tests interactive behavior: event handling, state changes, focus management.
use lv_tui::prelude::*;
use lv_tui::widgets::*;

// ── Helpers ─────────────────────────────────────────────────────

fn has_text(buf: &Buffer, text: &str) -> bool {
    let cells: String = buf.cells.iter().map(|c| c.symbol.as_str()).collect();
    cells.contains(text)
}

// ===================================================================
// Checkbox
// ===================================================================

#[test]
fn checkbox_space_toggles() {
    let mut pilot = Pilot::new(Checkbox::new("Option"), 20, 3);
    pilot.focus_first();

    // Initial: unchecked
    assert!(!has_text(pilot.frame(), ""), "should start unchecked");

    // Toggle on
    pilot.press(Key::Char(' ')).unwrap();
    assert!(has_text(pilot.frame(), ""), "should be checked after Space");

    // Toggle off
    pilot.press(Key::Char(' ')).unwrap();
    assert!(!has_text(pilot.frame(), ""), "should be unchecked after second Space");
}

#[test]
fn checkbox_enter_toggles() {
    let mut pilot = Pilot::new(Checkbox::new("Opt"), 20, 3);
    pilot.focus_first();
    pilot.press(Key::Enter).unwrap();
    assert!(has_text(pilot.frame(), ""), "Enter should toggle checkbox");
}

#[test]
fn checkbox_starts_checked() {
    let mut pilot = Pilot::new(Checkbox::new("Opt").checked(), 20, 3);
    pilot.focus_first();
    assert!(has_text(pilot.frame(), ""), "checked() should start checked");
}

// ===================================================================
// RadioGroup
// ===================================================================

#[test]
fn radiogroup_down_selects_next() {
    let mut pilot = Pilot::new(RadioGroup::new(vec!["A", "B", "C"]), 20, 5);
    pilot.focus_first();

    // Initial: first selected — "•" marker on first line
    let frame0: String = pilot.frame().cells.iter().map(|c| c.symbol.as_str()).collect();
    assert!(frame0.contains(""), "should have selection marker");

    // Down to second
    pilot.press(Key::Down).unwrap();
    // Check that 'B' appears in the rendered output
    assert!(has_text(pilot.frame(), "B"), "should show option B");
}

#[test]
fn radiogroup_up_wraps() {
    let mut pilot = Pilot::new(RadioGroup::new(vec!["X", "Y"]), 20, 3);
    pilot.focus_first();
    // First press Up: wraps to last
    pilot.press(Key::Up).unwrap();
    assert!(has_text(pilot.frame(), "Y"), "should wrap to last option");
}

#[test]
fn radiogroup_ignores_event_when_not_target_phase() {
    // Events during capture/bubble should not change selection
    let mut pilot = Pilot::new(RadioGroup::new(vec!["A", "B"]), 20, 3);
    // No focus — keyboard events should be dispatched to root
    // which means they go through capture/bubble but not target
    pilot.press(Key::Down).unwrap();
    // Selection should not have changed — A is still first
    assert!(has_text(pilot.frame(), "A"), "A should still be selected without focus");
}

// ===================================================================
// Select
// ===================================================================

#[test]
fn select_expands_on_space() {
    let mut pilot = Pilot::new(Select::new().options(vec!["Alpha", "Beta", "Gamma"]), 20, 10);
    pilot.focus_first();

    // Collapsed: shows "Alpha ▼"
    assert!(has_text(pilot.frame(), "Alpha"), "should show selected option");
    assert!(has_text(pilot.frame(), ""), "should show collapsed indicator");

    // Expand
    pilot.press(Key::Char(' ')).unwrap();
    assert!(has_text(pilot.frame(), ""), "should show expanded indicator");
    // Should now show all options
    assert!(has_text(pilot.frame(), "Beta"), "expanded should show all options");
}

#[test]
fn select_collapses_on_esc() {
    let mut pilot = Pilot::new(Select::new().options(vec!["A", "B"]), 20, 8);
    pilot.focus_first();
    pilot.press(Key::Char(' ')).unwrap(); // expand
    assert!(has_text(pilot.frame(), ""));
    pilot.press(Key::Esc).unwrap(); // collapse
    assert!(!has_text(pilot.frame(), ""));
}

#[test]
fn select_enter_toggles() {
    let mut pilot = Pilot::new(Select::new().options(vec!["One", "Two"]), 20, 8);
    pilot.focus_first();
    pilot.press(Key::Enter).unwrap(); // expand
    assert!(has_text(pilot.frame(), ""));
    pilot.press(Key::Enter).unwrap(); // collapse (selects current)
    assert!(!has_text(pilot.frame(), ""));
}

#[test]
fn select_navigation_in_expanded_mode() {
    let mut pilot = Pilot::new(Select::new().options(vec!["First", "Second", "Third"]), 20, 10);
    pilot.focus_first();
    pilot.press(Key::Char(' ')).unwrap(); // expand

    // Navigate down
    pilot.press(Key::Down).unwrap();
    // After navigation, "Second" should still be visible
    // (the selection marker ❯ moves, not the text itself)
    assert!(has_text(pilot.frame(), "Second"), "navigated list should show Second");
}

// ===================================================================
// Input
// ===================================================================

#[test]
fn input_types_characters() {
    let mut pilot = Pilot::new(Input::new(), 30, 3);
    pilot.focus_first();

    pilot.press(Key::Char('h')).unwrap();
    pilot.press(Key::Char('i')).unwrap();
    assert!(has_text(pilot.frame(), "hi"), "should show typed characters");
}

#[test]
fn input_backspace_removes_char() {
    let mut pilot = Pilot::new(Input::new(), 30, 3);
    pilot.focus_first();

    pilot.press(Key::Char('x')).unwrap();
    pilot.press(Key::Backspace).unwrap();
    assert!(!has_text(pilot.frame(), "x"), "backspace should remove character");
}

#[test]
fn input_shows_placeholder() {
    let pilot = Pilot::new(Input::new().placeholder("Enter name"), 30, 3);
    // No focus, empty text → shows placeholder
    assert!(has_text(pilot.frame(), "Enter name"), "should show placeholder when empty and unfocused");
}

#[test]
fn input_enter_triggers_submit() {
    use std::sync::mpsc;
    let (tx, rx) = mpsc::channel();

    let mut pilot = Pilot::new(
        Input::new().placeholder("test").on_submit(move |text| {
            let _ = tx.send(text.to_string());
        }),
        30, 3,
    );
    pilot.focus_first();
    pilot.press(Key::Char('y')).unwrap();
    pilot.press(Key::Enter).unwrap();

    // Check that submit was called
    let submitted = rx.try_recv().ok();
    assert_eq!(submitted, Some("y".to_string()), "submit callback should receive text");
}

#[test]
fn input_ignores_ctrl_key_combos() {
    let mut pilot = Pilot::new(Input::new(), 30, 3);
    pilot.focus_first();
    // Ctrl+C is handled specially (quits), but other ctrl combos are ignored
    // Just verify typing works after ctrl press
    pilot.press(Key::Char('a')).unwrap();
    assert!(has_text(pilot.frame(), "a"), "regular chars should still work");
}

// ===================================================================
// Dialog
// ===================================================================

#[test]
fn dialog_enter_confirms() {
    let mut pilot = Pilot::new(
        Dialog::new(Label::new("content")).border(Border::Rounded),
        30, 10,
    );
    pilot.focus_first();
    pilot.press(Key::Enter).unwrap();
    // Dialog renders a border — verify it's still there after confirmation
    assert!(has_text(pilot.frame(), "Enter"), "footer should be visible");
}

#[test]
fn dialog_esc_cancels() {
    let mut pilot = Pilot::new(
        Dialog::new(Label::new("test")).border(Border::Rounded),
        30, 10,
    );
    pilot.focus_first();
    pilot.press(Key::Esc).unwrap();
    // Esc pressed — dialog still renders (cancel state is internal)
    assert!(has_text(pilot.frame(), "test"), "dialog content should still render");
}

// ===================================================================
// Spinner
// ===================================================================

#[test]
fn spinner_advances_on_tick() {
    let mut pilot = Pilot::new(Spinner::new("loading"), 20, 3);
    let frame0: String = pilot.frame().cells.iter().map(|c| c.symbol.as_str()).collect();

    // Send tick and check frame changed
    pilot.send_event(Event::Tick).unwrap();
    let frame1: String = pilot.frame().cells.iter().map(|c| c.symbol.as_str()).collect();

    // The spinner character should change
    assert_ne!(frame0, frame1, "spinner frame should change on Tick");
}

// ===================================================================
// ProgressBar
// ===================================================================

#[test]
fn progressbar_shows_ratio() {
    let pilot = Pilot::new(ProgressBar::new().ratio(0.5).width(20).label(true), 22, 3);
    assert!(has_text(pilot.frame(), "50%"), "should show percentage when label enabled");
}

#[test]
fn progressbar_without_label() {
    let pilot = Pilot::new(ProgressBar::new().ratio(0.75).width(10), 15, 3);
    // Without label, bar renders blocks only
    assert!(has_text(pilot.frame(), ""), "should render filled blocks");
}

// ===================================================================
// VirtualList
// ===================================================================

#[test]
fn virtuallist_renders_items() {
    let items: Vec<String> = (0..5).map(|i| format!("item-{}", i)).collect();
    let pilot = Pilot::new(VirtualList::new(items), 20, 5);
    assert!(has_text(pilot.frame(), "item-0"), "should render first item");
    assert!(has_text(pilot.frame(), "item-4"), "should render last item within viewport");
}

#[test]
fn virtuallist_down_selects_next() {
    let items: Vec<String> = (0..10).map(|i| format!("row-{}", i)).collect();
    let mut pilot = Pilot::new(
        VirtualList::new(items).highlight_symbol("> "),
        20, 10,
    );

    // Initially nothing selected
    assert!(!has_text(pilot.frame(), "> "), "no highlight when nothing selected");

    // Select first item
    pilot.press(Key::Down).unwrap();
    assert!(has_text(pilot.frame(), "> "), "highlight should appear after Down");
}

// Note: VirtualList page-level scrolling via PageDown key is a known issue.
// scroll_y updates in the event handler but the render does not reflect the
// change. See virtuallist_pagedown_scrolls investigation in Pilot debug output.

// ===================================================================
// Scroll
// ===================================================================

#[test]
fn scroll_content_is_clipped() {
    // Create a tall label inside scroll with small viewport
    let label = Label::new("line1\nline2\nline3\nline4\nline5\nline6");
    let mut pilot = Pilot::new(Scroll::new(label), 20, 3);
    pilot.press(Key::Down).unwrap();
    pilot.press(Key::Down).unwrap();
    // After scrolling down, line1 should be hidden
    assert!(!has_text(pilot.frame(), "line1"), "scrolled content should be clipped");
}

#[test]
fn scroll_pagedown_moves_by_viewport() {
    let lines: Vec<String> = (0..20).map(|i| format!("L{:02}", i)).collect();
    let text = lines.join("\n");
    let mut pilot = Pilot::new(Scroll::new(Label::new(text)), 10, 5);

    pilot.press(Key::PageDown).unwrap();
    // After PageDown (4 lines), L00 should be gone (scrolled off)
    assert!(!has_text(pilot.frame(), "L00"), "L00 scrolled off after PageDown");
}

// ===================================================================
// Table
// ===================================================================

#[test]
fn table_renders_headers() {
    let t = Table::new()
        .columns(vec![
            TableColumn { title: Text::from("Name"), width: ColumnWidth::Flex(1), align: TextAlign::Left },
            TableColumn { title: Text::from("Size"), width: ColumnWidth::Fixed(6), align: TextAlign::Right },
        ])
        .rows_simple(vec![
            vec!["file.rs", "1.2KB"],
            vec!["main.rs", "3.4KB"],
        ]);
    let pilot = Pilot::new(t, 30, 8);
    assert!(has_text(pilot.frame(), "Name"), "should show header");
    assert!(has_text(pilot.frame(), "file.rs"), "should show row data");
}

#[test]
fn table_down_selects_row() {
    let t = Table::new()
        .columns(vec![
            TableColumn { title: Text::from("Col"), width: ColumnWidth::Flex(1), align: TextAlign::Left },
        ])
        .rows_simple(vec![vec!["A"], vec!["B"], vec!["C"]]);
    let mut pilot = Pilot::new(t, 20, 8);

    pilot.press(Key::Down).unwrap();
    // After Down, selected row index changes (internal state)
    // Verify table still renders
    assert!(has_text(pilot.frame(), "Col"), "table should still render after navigation");
}

// ===================================================================
// DiffView
// ===================================================================

#[test]
fn diffview_parses_and_renders() {
    let diff = "diff --git a/file b/file\n--- a/file\n+++ b/file\n@@ -1,3 +1,3 @@\n-old\n+new\n ctx";
    let pilot = Pilot::new(DiffView::new(diff), 40, 10);
    // Check that lines are parsed and rendered
    assert!(has_text(pilot.frame(), "old"), "should show removed lines");
    assert!(has_text(pilot.frame(), "new"), "should show added lines");
}

#[test]
fn diffview_scrolls_down() {
    let lines: Vec<String> = (0..20).map(|i| format!(" line_{}", i)).collect();
    let diff = format!("--- a\n+++ b\n@@ -1,20 +1,20 @@\n{}", lines.join("\n"));
    let mut pilot = Pilot::new(DiffView::new(&diff), 40, 8);

    // Scroll down several times
    for _ in 0..5 {
        pilot.press(Key::Down).unwrap();
    }
    // After scrolling, line_0 should be off screen, later lines visible
    assert!(!has_text(pilot.frame(), "line_0"), "first line scrolled off");
}

// ===================================================================
// SplitPane
// ===================================================================

#[test]
fn splitpane_renders_both_children() {
    let mut pilot = Pilot::new(
        SplitPane::new()
            .first(Label::new("LEFT"))
            .second(Label::new("RIGHT"))
            .ratio(50),
        40, 10,
    );
    pilot.focus_first();
    assert!(has_text(pilot.frame(), "LEFT"), "should show first child");
    assert!(has_text(pilot.frame(), "RIGHT"), "should show second child");
}

#[test]
fn splitpane_ratio_change() {
    let mut pilot = Pilot::new(
        SplitPane::new()
            .first(Label::new("AAA"))
            .second(Label::new("BBB"))
            .direction(SplitDirection::Horizontal)
            .ratio(50),
        50, 10,
    );
    // Ctrl+Left decreases ratio (first panel shrinks)
    pilot.press_with_modifiers(Key::Left, Modifiers { ctrl: true, alt: false, shift: false }).unwrap();
    // Verify both children still render (ratio changed but still valid)
    assert!(has_text(pilot.frame(), "AAA"), "first child still visible after resize");
    assert!(has_text(pilot.frame(), "BBB"), "second child still visible after resize");
}

// ===================================================================
// Block
// ===================================================================

#[test]
fn block_renders_border_and_title() {
    let pilot = Pilot::new(
        Block::new(Label::new("inner"))
            .border(Border::Rounded)
            .title("My Title"),
        30, 8,
    );
    assert!(has_text(pilot.frame(), "My Title"), "should render title");
    assert!(has_text(pilot.frame(), "inner"), "should render child");
}

// ===================================================================
// Overlay
// ===================================================================

#[test]
fn overlay_shows_foreground_when_active() {
    let mut pilot = Pilot::new(
        Overlay::new(
            Label::new("background"),
            Label::new("POPUP"),
        ),
        30, 10,
    );
    pilot.focus_first();
    // Overlay starts inactive — shows background
    assert!(has_text(pilot.frame(), "background"), "should show background initially");
}

// ===================================================================
// Label
// ===================================================================

#[test]
fn label_renders_multiline() {
    let pilot = Pilot::new(Label::new("line1\nline2\nline3"), 20, 5);
    assert!(has_text(pilot.frame(), "line1"), "should render first line");
    assert!(has_text(pilot.frame(), "line3"), "should render third line");
}

#[test]
fn label_renders_styled_text() {
    let text = Text::from(Line::from("hello".red().bold()));
    let pilot = Pilot::new(Label::new(text), 20, 3);
    assert!(has_text(pilot.frame(), "hello"), "should render styled label text");
}

// ===================================================================
// Column / Row layout
// ===================================================================

#[test]
fn column_stacks_children() {
    let pilot = Pilot::new(
        Column::new()
            .child(Label::new("TOP"))
            .child(Label::new("BOTTOM"))
            .gap(0),
        20, 5,
    );
    let cells: String = pilot.frame().cells.iter().map(|c| c.symbol.as_str()).collect();
    let top_pos = cells.find("TOP").unwrap();
    let bottom_pos = cells.find("BOTTOM").unwrap();
    assert!(top_pos < bottom_pos, "TOP should be above BOTTOM");
}

#[test]
fn row_arranges_horizontally() {
    let pilot = Pilot::new(
        Row::new()
            .child(Label::new("LEFT"))
            .child(Label::new("RIGHT"))
            .gap(1),
        30, 3,
    );
    let cells: String = pilot.frame().cells.iter().map(|c| c.symbol.as_str()).collect();
    let left_pos = cells.find("LEFT").unwrap();
    let right_pos = cells.find("RIGHT").unwrap();
    assert!(left_pos < right_pos, "LEFT should be before RIGHT");
}