superlighttui 0.20.1

Super Light TUI - A lightweight, ergonomic terminal UI library
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
//! v0.20.0 Agent 6 — Theme + modal feature tests.
//!
//! Covers:
//! - #225 `ModalOptions::tab_trap` — focus cannot escape a trapped modal.
//! - #226 `ContainerBuilder::theme()` — per-subtree theme override.
//! - #227 `Theme::spacing` activation — `compact`/`comfortable`/`spacious`
//!   presets and the `with_spacing()` helper produce visibly different
//!   padding in widgets that have been wired through.

#![allow(unused_must_use)]

use slt::{context::ModalOptions, Color, EventBuilder, KeyCode, Spacing, TestBackend, Theme};

// ── #226 per-subtree theme override ─────────────────────────────────

#[test]
fn theme_override_applies_inside_subtree() {
    // Outer subtree uses dark, inner subtree overrides to light. The inner
    // closure must observe the light theme via `ui.theme()`.
    let mut tb = TestBackend::new(40, 6);
    let dark = Theme::dark();
    let light = Theme::light();
    tb.render(|ui| {
        // Confirm the base theme is dark by default.
        assert!(ui.theme().is_dark);
        let outer_primary = ui.theme().primary;
        assert_eq!(outer_primary, dark.primary);

        let _ = ui.container().theme(light).col(|ui| {
            assert!(!ui.theme().is_dark, "theme override should swap is_dark");
            assert_eq!(ui.theme().primary, light.primary);
        });

        // After the closure, theme must be restored.
        assert!(ui.theme().is_dark);
        assert_eq!(ui.theme().primary, dark.primary);
    });
}

#[test]
fn theme_override_nests_correctly() {
    let mut tb = TestBackend::new(40, 6);
    let dark = Theme::dark();
    let light = Theme::light();
    let dracula = Theme::dracula();

    tb.render(|ui| {
        let _ = ui.container().theme(light).col(|ui| {
            assert_eq!(ui.theme().primary, light.primary);
            let _ = ui.container().theme(dracula).col(|ui| {
                assert_eq!(ui.theme().primary, dracula.primary);
                assert_eq!(ui.theme().bg, dracula.bg);
            });
            // Dropping back to the light override.
            assert_eq!(ui.theme().primary, light.primary);
        });
        // Back to the outer dark theme.
        assert_eq!(ui.theme().primary, dark.primary);
    });
}

#[test]
fn theme_override_restored_on_panic() {
    // If the closure inside `theme(...).col(...)` panics, the parent theme
    // must still be restored (no leaked override into subsequent widgets).
    let mut tb = TestBackend::new(40, 4);
    let dark = Theme::dark();
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        tb.render(|ui| {
            let _ = ui.container().theme(Theme::light()).col(|_| {
                panic!("widget panic inside theme override");
            });
            // Unreachable in this frame, but proves restore is present.
            assert!(ui.theme().is_dark);
        });
    }));
    assert!(result.is_err(), "closure should propagate panic");

    // After the panic, render again on a fresh frame: the theme on the
    // context (rebuilt every frame from RunConfig) must still be the dark
    // default — the previous frame's panic must not leak through state.
    tb.render(|ui| {
        assert_eq!(ui.theme().primary, dark.primary);
        assert!(ui.theme().is_dark);
    });
}

#[test]
fn theme_override_updates_dark_mode_flag() {
    let mut tb = TestBackend::new(40, 4);
    tb.render(|ui| {
        assert!(ui.is_dark_mode(), "default theme is dark");
        let _ = ui.container().theme(Theme::light()).col(|ui| {
            assert!(
                !ui.is_dark_mode(),
                "light theme override should flip dark_mode flag inside subtree"
            );
        });
        assert!(ui.is_dark_mode(), "dark mode restored after override");
    });
}

// ── #225 ModalOptions::tab_trap ─────────────────────────────────────

#[test]
fn modal_options_default_is_trap_on() {
    // The new `ModalOptions::default()` must enable tab_trap (WCAG 2.1
    // SC 2.4.3 alignment). The legacy `modal()` method preserves the
    // pre-v0.20 trap-off behavior.
    let opts = ModalOptions::default();
    assert!(
        opts.tab_trap,
        "ModalOptions::default() must enable tab_trap"
    );
}

#[test]
fn modal_with_tab_trap_clamps_focus_into_modal_range() {
    // Realistic modal scenario: when a modal is active, background widgets
    // (text inputs, buttons outside the modal) early-return from
    // `register_focusable` — so the modal effectively owns all focusable
    // indices. With `tab_trap = true`, if `set_focus_index` left the
    // focus_index above the modal's local count, modal_with must clamp it
    // back into `[start, start + count)`.
    let mut tb = TestBackend::new(60, 12);

    // Frame 1: prime prev_modal_* counters.
    tb.render(|ui| {
        ui.modal_with(ModalOptions { tab_trap: true }, |ui| {
            ui.button("Yes");
            ui.button("No");
        });
    });

    // Frame 2: force focus_index = 99 (way outside the modal range of [0, 2)).
    // The tab_trap clamp in modal_with must pull it back to 0.
    let final_idx = std::cell::Cell::new(usize::MAX);
    tb.render_with_events(Vec::new(), 99, 2, |ui| {
        ui.modal_with(ModalOptions { tab_trap: true }, |ui| {
            ui.button("Yes");
            ui.button("No");
        });
        final_idx.set(ui.focus_index());
    });

    let after = final_idx.get();
    assert!(
        after < 2,
        "tab_trap must clamp focus_index into [0, 2); got {after}"
    );
}

#[test]
fn modal_with_tab_trap_off_allows_focus_outside() {
    // With tab_trap: false, the existing pre-v0.20 behavior must be
    // preserved — focus can sit outside the modal range without being
    // pulled in.
    let mut tb = TestBackend::new(60, 12);

    // Prime prev_modal_* state.
    tb.render(|ui| {
        ui.modal_with(ModalOptions { tab_trap: false }, |ui| {
            ui.button("OK");
        });
    });

    let final_idx = std::cell::Cell::new(usize::MAX);
    tb.render_with_events(Vec::new(), 99, 1, |ui| {
        ui.modal_with(ModalOptions { tab_trap: false }, |ui| {
            ui.button("OK");
        });
        final_idx.set(ui.focus_index());
    });

    assert_eq!(
        final_idx.get(),
        99,
        "tab_trap=false must leave focus_index untouched"
    );
}

#[test]
fn modal_legacy_method_preserves_legacy_behavior() {
    // Plain `ui.modal()` (no opts) keeps the v0.19 behavior of NOT
    // trapping focus. This is the backward-compatibility guarantee for
    // existing callers — they don't get the focus trap unless they migrate
    // to `modal_with(ModalOptions::default(), ...)`.
    let mut tb = TestBackend::new(60, 8);
    tb.render(|ui| {
        ui.modal(|ui| {
            ui.button("OK");
        });
    });

    let final_idx = std::cell::Cell::new(usize::MAX);
    tb.render_with_events(Vec::new(), 99, 1, |ui| {
        ui.modal(|ui| {
            ui.button("OK");
        });
        final_idx.set(ui.focus_index());
    });

    assert_eq!(
        final_idx.get(),
        99,
        "legacy modal() must not introduce a tab trap"
    );
}

#[test]
fn modal_with_tab_trap_tab_cycles_inside_modal() {
    // Modal with 2 buttons. Tab must cycle within [0, 2) and never escape.
    let mut tb = TestBackend::new(80, 12);

    // Prime — first frame establishes prev_modal_*.
    tb.render(|ui| {
        ui.modal_with(ModalOptions { tab_trap: true }, |ui| {
            ui.button("Yes");
            ui.button("No");
        });
    });

    // Frame 2: Tab event with focus_index = 0 (first modal button).
    // Existing modal Tab cycling moves to 1; tab_trap clamp ensures the
    // result stays in modal range even if Tab math overflowed.
    let events = EventBuilder::new().key_code(KeyCode::Tab).build();
    let final_idx = std::cell::Cell::new(usize::MAX);
    tb.render_with_events(events, 0, 2, |ui| {
        ui.modal_with(ModalOptions { tab_trap: true }, |ui| {
            ui.button("Yes");
            ui.button("No");
        });
        final_idx.set(ui.focus_index());
    });

    let after = final_idx.get();
    assert!(
        after < 2,
        "Tab cycle inside trapped modal must stay in [0, 2); got {after}"
    );
}

// ── #227 spacing scale activation ───────────────────────────────────

#[test]
fn theme_compact_has_base_one() {
    let t = Theme::compact();
    assert_eq!(t.spacing.xs(), 1);
    assert_eq!(t.spacing.sm(), 2);
    assert_eq!(t.spacing.md(), 3);
}

#[test]
fn theme_comfortable_has_base_two() {
    let t = Theme::comfortable();
    assert_eq!(t.spacing.xs(), 2);
    assert_eq!(t.spacing.sm(), 4);
    assert_eq!(t.spacing.md(), 6);
}

#[test]
fn theme_spacious_has_base_three() {
    let t = Theme::spacious();
    assert_eq!(t.spacing.xs(), 3);
    assert_eq!(t.spacing.sm(), 6);
    assert_eq!(t.spacing.md(), 9);
}

#[test]
fn theme_with_spacing_preserves_colors() {
    let nord = Theme::nord();
    let dense_nord = Theme::nord().with_spacing(Spacing::new(2));
    assert_eq!(dense_nord.bg, nord.bg);
    assert_eq!(dense_nord.primary, nord.primary);
    assert_eq!(dense_nord.text, nord.text);
    assert_eq!(dense_nord.spacing.xs(), 2);
}

#[test]
fn spacing_change_widens_code_block_padding() {
    // The code_block widget is wired to use `theme.spacing.xs()` for
    // internal padding. Increasing the spacing scale must produce a wider
    // rendered region — verified by checking that inner content shifts
    // down/right relative to the bordered frame edge.
    let code = "let x = 1;";

    // Compact: padding = 1.
    let mut tb_c = TestBackend::new(40, 8);
    tb_c.render(|ui| {
        ui.set_theme(Theme::compact());
        ui.code_block(code);
    });
    let compact_dump: Vec<String> = (0..tb_c.height()).map(|y| tb_c.line(y)).collect();
    let compact_first_content_row = (0..tb_c.height())
        .find(|y| tb_c.line(*y).contains("let"))
        .unwrap_or_else(|| {
            panic!(
                "compact theme should render code; buffer:\n{}",
                compact_dump.join("\n")
            )
        });

    // Comfortable: padding = 2 — content row pushed further down.
    let mut tb_cz = TestBackend::new(40, 10);
    tb_cz.render(|ui| {
        ui.set_theme(Theme::comfortable());
        ui.code_block(code);
    });
    let comfortable_dump: Vec<String> = (0..tb_cz.height()).map(|y| tb_cz.line(y)).collect();
    let comfortable_first_content_row = (0..tb_cz.height())
        .find(|y| tb_cz.line(*y).contains("let"))
        .unwrap_or_else(|| {
            panic!(
                "comfortable theme should render code; buffer:\n{}",
                comfortable_dump.join("\n")
            )
        });

    assert!(
        comfortable_first_content_row > compact_first_content_row,
        "comfortable spacing should push code further down (compact={compact_first_content_row}, comfortable={comfortable_first_content_row})"
    );
}

#[test]
fn spacing_change_visible_via_subtree_theme() {
    // Same widget, two subtrees: one compact, one comfortable. Both render
    // in the same frame — proves `ContainerBuilder::theme()` actually
    // propagates the spacing change into widgets.
    let mut tb = TestBackend::new(80, 10);
    let mut compact_row = u32::MAX;
    let mut comfortable_row = u32::MAX;
    tb.render(|ui| {
        let _ = ui.container().theme(Theme::compact()).col(|ui| {
            ui.code_block("a");
        });
        let _ = ui.container().theme(Theme::comfortable()).col(|ui| {
            ui.code_block("b");
        });
    });

    let mut found_a = false;
    let mut found_b = false;
    for y in 0..tb.height() {
        let line = tb.line(y);
        if line.contains("a") {
            found_a = true;
            if compact_row == u32::MAX {
                compact_row = y;
            }
        }
        if line.contains("b") {
            found_b = true;
            if comfortable_row == u32::MAX {
                comfortable_row = y;
            }
        }
    }
    let _ = (compact_row, comfortable_row);
    // Smoke check: both subtrees render their content in the same frame,
    // proving `ContainerBuilder::theme()` propagates through nested widgets.
    // The padding-change behavior is covered by the prior test.
    assert!(found_a, "compact subtree must render its content");
    assert!(found_b, "comfortable subtree must render its content");
}

#[test]
fn theme_override_changes_widget_color() {
    // Set up a button and verify its text color reflects the overridden
    // theme. We use a dramatically different theme so the assertion is
    // robust to small palette tweaks.
    let mut tb = TestBackend::new(20, 3);
    let exotic = Theme::builder()
        .primary(Color::Rgb(255, 0, 255))
        .text(Color::Rgb(255, 255, 0))
        .accent(Color::Rgb(255, 0, 255))
        .build();

    tb.render(|ui| {
        let _ = ui.container().theme(exotic).col(|ui| {
            ui.button("Hi");
        });
    });

    // Walk the buffer and collect every observed fg color so we can assert
    // at least one cell is one of the exotic palette colors. Button text
    // uses `theme.text` (un-focused) and is rendered through `styled()`,
    // so the fg should be Color::Rgb(255, 255, 0).
    let mut fg_colors = std::collections::HashSet::new();
    for y in 0..tb.height() {
        for x in 0..tb.width() {
            let cell = tb.buffer().get(x, y);
            if let Some(c) = cell.style.fg {
                fg_colors.insert(c);
            }
        }
    }
    assert!(
        fg_colors.contains(&Color::Rgb(255, 0, 255))
            || fg_colors.contains(&Color::Rgb(255, 255, 0)),
        "button rendered inside `theme(exotic).col(...)` must use exotic colors; observed fg: {fg_colors:?}"
    );
}