abstracttui 0.2.13

A reactive, compositor-grade terminal UI engine: fine-grained signals, layered rendering with damage tracking, images (kitty/iTerm2/sixel/mosaic), software-rasterized 3D (GLB), themes and animation.
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
//! Field-fix wave for the first app's ChoicePrompt filings (backlog
//! first-app 0286/0287/0288): the shifted-letter wire-spelling split
//! and the body slot. Wire BYTES in through `CaptureTerm`, modeled VT
//! screen out, through the REAL `Driver` (the same harness posture as
//! wave_choice_review.rs — helper duplication across integration
//! files is the house style).
//!
//! The 0288/0286 defect in one line: a shifted letter has TWO wire
//! spellings — legacy Shift+A arrives as byte `A` (`Char('A')`, no
//! mods; shift baked into the char) while the kitty keyboard protocol
//! sends `CSI 97;2u` (`Char('a')` + SHIFT — the key identity is
//! deliberately the base key) — and every matcher compared exactly
//! one spelling, so the OTHER wire's users pressed a dead key.

use std::cell::RefCell;
use std::rc::Rc;

use abstracttui::app::{App, Driver, RunConfig};
use abstracttui::base::Size;
use abstracttui::prelude::*;
use abstracttui::term::Capabilities;
use abstracttui::testing::CaptureTerm;
use abstracttui::ui::text;

const W: i32 = 56;
const H: i32 = 18;

/// Kitty CSI u encoding of Shift+A: code 97 ('a', base identity),
/// mods 2 = 1 + SHIFT(1). This is the exact byte sequence of the
/// first app's live P0 (its regression test pushes the same bytes).
const KITTY_SHIFT_A: &[u8] = b"\x1b[97;2u";

fn config() -> RunConfig {
    RunConfig {
        caps: Some(Capabilities::with(|c| {
            c.truecolor = true;
            c.colors_256 = true;
        })),
        enter: None,
        probe: false,
    }
}

fn settle(driver: &mut Driver, app: &mut App, term: &mut CaptureTerm) {
    for _ in 0..128 {
        let turn = driver.turn(app, term).expect("turn");
        if turn.idle {
            return;
        }
    }
    panic!("loop failed to settle within 128 turns");
}

fn screen(term: &CaptureTerm) -> String {
    term.screen().to_text()
}

/// Column (in CELLS, not bytes) of `needle`'s first char in `line` —
/// glyph rows carry multi-byte chars, so `str::find` byte offsets are
/// not columns.
fn col_of(line: &str, needle: &str) -> Option<i32> {
    line.find(needle)
        .map(|byte| line[..byte].chars().count() as i32)
}

/// Locate `needle` on screen as (col, row) in cells.
fn locate(term: &CaptureTerm, needle: &str) -> Option<(i32, i32)> {
    for (y, line) in screen(term).lines().enumerate() {
        if let Some(x) = col_of(line, needle) {
            return Some((x, y as i32));
        }
    }
    None
}

type OutcomeLog = Rc<RefCell<Vec<ChoiceOutcome>>>;

fn recorder(log: &OutcomeLog) -> impl FnOnce(ChoiceOutcome) + 'static {
    let log = log.clone();
    move |o| log.borrow_mut().push(o)
}

fn answered(ids: &[&str], other: Option<&str>) -> ChoiceOutcome {
    ChoiceOutcome::Answered(ChoiceAnswer {
        selected: ids.iter().map(|s| s.to_string()).collect(),
        other: other.map(str::to_string),
    })
}

/// Host app: a focusable root. Returns (app, mount scope).
fn host(size: Size) -> (App, Scope) {
    let mut app = App::new(size);
    let scope_slot: Rc<RefCell<Option<Scope>>> = Rc::default();
    let ss = scope_slot.clone();
    app.mount(move |cx| {
        *ss.borrow_mut() = Some(cx);
        Element::new()
            .style(LayoutStyle::column())
            .focusable()
            .autofocus()
            .child(text("host app row"))
            .build()
    })
    .expect("mount");
    let scope = scope_slot.borrow().expect("scope");
    (app, scope)
}

fn boot(app: &mut App, term: &mut CaptureTerm) -> Driver {
    let mut driver = Driver::new(app, term, config()).expect("driver");
    settle(&mut driver, app, term);
    driver
}

/// The 0.2.8 announcement's approval vocabulary: `a`/`A`/`d` — the
/// exact gate shape 0288 reports dead on kitty wires.
fn approval(p: ChoicePrompt) -> ChoicePrompt {
    p.option_key("approve", "Approve", 'a')
        .option_key("all", "Approve all", 'A')
        .option_key("deny", "Deny", 'd')
}

// ===========================================================================
// 0288 — option_key letters must fire on BOTH wire spellings
// ===========================================================================

/// THE repro (0288): `option_key(…, 'A')` on the kitty wire. Shift+A
/// arrives as `CSI 97;2u` = `Char('a')` + SHIFT; the gate must read it
/// as the uppercase key and commit "all" — exactly once.
#[test]
fn kitty_shift_a_fires_the_uppercase_option_key_and_commits() {
    let (mut app, scope) = host(Size::new(W, H));
    let mut term = CaptureTerm::new(Size::new(W, H));
    let mut driver = boot(&mut app, &mut term);

    let log: OutcomeLog = Rc::default();
    approval(ChoicePrompt::new("Run 3 tool calls?"))
        .on_resolve(recorder(&log))
        .open(scope);
    settle(&mut driver, &mut app, &mut term);
    assert!(screen(&term).contains("Run 3 tool calls?"));

    term.push_input(KITTY_SHIFT_A);
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(
        log.borrow().as_slice(),
        [answered(&["all"], None)],
        "kitty-wire Shift+A must commit the 'A' option (0288)"
    );
    assert!(
        !screen(&term).contains("Run 3 tool calls?"),
        "the gate closed on the letter commit"
    );
}

/// The legacy spelling of the same key: byte `A` = `Char('A')`, no
/// mods. Pinned so the fold never trades one wire for the other.
#[test]
fn legacy_shift_a_fires_the_uppercase_option_key_and_commits() {
    let (mut app, scope) = host(Size::new(W, H));
    let mut term = CaptureTerm::new(Size::new(W, H));
    let mut driver = boot(&mut app, &mut term);

    let log: OutcomeLog = Rc::default();
    approval(ChoicePrompt::new("Run 3 tool calls?"))
        .on_resolve(recorder(&log))
        .open(scope);
    settle(&mut driver, &mut app, &mut term);

    term.push_input(b"A");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(log.borrow().as_slice(), [answered(&["all"], None)]);
}

/// Multiple mode: the kitty spelling jump-toggles the uppercase key's
/// mark (never resolves), and Enter then commits the set.
#[test]
fn kitty_shift_a_jump_toggles_in_multiple_mode() {
    let (mut app, scope) = host(Size::new(W, H));
    let mut term = CaptureTerm::new(Size::new(W, H));
    let mut driver = boot(&mut app, &mut term);

    let log: OutcomeLog = Rc::default();
    approval(ChoicePrompt::new("Which batches?"))
        .allow_multiple(true)
        .on_resolve(recorder(&log))
        .open(scope);
    settle(&mut driver, &mut app, &mut term);

    term.push_input(KITTY_SHIFT_A);
    settle(&mut driver, &mut app, &mut term);
    assert!(
        screen(&term).contains("☑ Approve all"),
        "kitty Shift+A toggles the 'A' mark: {}",
        screen(&term)
    );
    assert!(log.borrow().is_empty(), "toggles never resolve");

    term.push_input(b"\r");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(log.borrow().as_slice(), [answered(&["all"], None)]);
}

/// The other half of the guarantee: a LOWERCASE-declared key must NOT
/// fire on Shift+letter — on either wire, in either mode. Shift+K
/// means 'K', and 'K' is not declared.
#[test]
fn lowercase_declared_key_refuses_shift_letter_on_both_wires() {
    let (mut app, scope) = host(Size::new(W, H));
    let mut term = CaptureTerm::new(Size::new(W, H));
    let mut driver = boot(&mut app, &mut term);

    let log: OutcomeLog = Rc::default();
    ChoicePrompt::new("Keep the copies?")
        .option_key("keep", "Keep my copies", 'k')
        .option("drop", "Drop them")
        .on_resolve(recorder(&log))
        .open(scope);
    settle(&mut driver, &mut app, &mut term);

    term.push_input(b"\x1b[107;2u"); // kitty Shift+K ('k' = 107, mods 2)
    settle(&mut driver, &mut app, &mut term);
    assert!(
        log.borrow().is_empty(),
        "kitty Shift+K means 'K' — the lowercase 'k' key must not fire"
    );

    term.push_input(b"K"); // legacy Shift+K
    settle(&mut driver, &mut app, &mut term);
    assert!(
        log.borrow().is_empty(),
        "legacy Shift+K means 'K' — the lowercase 'k' key must not fire"
    );

    term.push_input(b"k"); // the declared key itself still commits
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(log.borrow().as_slice(), [answered(&["keep"], None)]);
}

/// The decide example's gate-1 vocabulary (`o`/`k`/`d`, lowercase) on
/// both wires: legacy bytes and the kitty PLAIN spelling (`CSI code u`,
/// no mods) — every declared key commits its own option.
#[test]
fn decide_example_gate1_keys_fire_on_both_wires() {
    let gate = |status: &OutcomeLog, scope: Scope| {
        ChoicePrompt::new("Overwrite 3 locally modified files?")
            .option_with(
                ChoiceOption::new("overwrite", "Overwrite them")
                    .detail("the local edits are lost")
                    .key('o')
                    .danger(true),
            )
            .option_with(
                ChoiceOption::new("keep", "Keep my copies")
                    .detail("the sync is skipped")
                    .key('k'),
            )
            .option_key("diff", "Show me the diff first", 'd')
            .allow_other("Something else…")
            .initial("keep")
            .dismissable(false)
            .on_resolve(recorder(status))
            .open(scope);
    };
    let cases: [(&[u8], &str); 6] = [
        (b"o", "overwrite"),         // legacy
        (b"\x1b[111u", "overwrite"), // kitty plain 'o'
        (b"k", "keep"),              // legacy
        (b"\x1b[107u", "keep"),      // kitty plain 'k'
        (b"d", "diff"),              // legacy
        (b"\x1b[100u", "diff"),      // kitty plain 'd'
    ];
    for (bytes, expect) in cases {
        let (mut app, scope) = host(Size::new(W, H));
        let mut term = CaptureTerm::new(Size::new(W, H));
        let mut driver = boot(&mut app, &mut term);
        let log: OutcomeLog = Rc::default();
        gate(&log, scope);
        settle(&mut driver, &mut app, &mut term);
        term.push_input(bytes);
        settle(&mut driver, &mut app, &mut term);
        assert_eq!(
            log.borrow().as_slice(),
            [answered(&[expect], None)],
            "key bytes {bytes:?} must commit {expect:?}"
        );
    }
}

// ===========================================================================
// 0286 — KeyChord shortcut matching folds the same two spellings
// ===========================================================================

/// Tree shortcuts, both directions of the fold: the natural
/// registration `plain(Char('A'))` fires on the kitty spelling, and
/// the kitty-shaped registration `SHIFT + Char('a')` fires on the
/// legacy spelling. One registration, both wires.
#[test]
fn tree_shortcut_shifted_letter_matches_both_wire_spellings() {
    for (chord, bytes) in [
        (KeyChord::plain(Key::Char('A')), KITTY_SHIFT_A),
        (KeyChord::new(Mods::SHIFT, Key::Char('a')), b"A".as_slice()),
    ] {
        let fired: Rc<RefCell<u32>> = Rc::default();
        let f = fired.clone();
        let mut app = App::new(Size::new(W, H));
        app.mount(move |_cx| {
            let f2 = f.clone();
            Element::new()
                .style(LayoutStyle::column())
                .focusable()
                .autofocus()
                .shortcut(chord, move |_| *f2.borrow_mut() += 1)
                .child(text("host"))
                .build()
        })
        .expect("mount");
        let mut term = CaptureTerm::new(Size::new(W, H));
        let mut driver = boot(&mut app, &mut term);
        term.push_input(bytes);
        settle(&mut driver, &mut app, &mut term);
        assert_eq!(
            *fired.borrow(),
            1,
            "chord {chord:?} must fire on wire bytes {bytes:?} (0286)"
        );
    }
}

/// Global actions ride the same fold: `plain(Char('A'))` registered in
/// the app keymap fires when the kitty spelling reaches the driver's
/// action dispatch (nothing in the UI consumed it).
#[test]
fn action_chord_shifted_letter_matches_both_wire_spellings() {
    for (chord, bytes) in [
        (KeyChord::plain(Key::Char('A')), KITTY_SHIFT_A),
        (KeyChord::new(Mods::SHIFT, Key::Char('a')), b"A".as_slice()),
    ] {
        let (mut app, _scope) = host(Size::new(W, H));
        let fired: Rc<RefCell<u32>> = Rc::default();
        let f = fired.clone();
        assert!(app
            .actions()
            .register("approve.all", Some(chord), move || *f.borrow_mut() += 1));
        let mut term = CaptureTerm::new(Size::new(W, H));
        let mut driver = boot(&mut app, &mut term);
        term.push_input(bytes);
        settle(&mut driver, &mut app, &mut term);
        assert_eq!(
            *fired.borrow(),
            1,
            "action {chord:?} must fire on wire bytes {bytes:?} (0286)"
        );
    }
}

// ===========================================================================
// 0287 — the body slot: structure between the prompt and the options
// ===========================================================================

/// The reactive gap 0287 names (the tier-honesty line): a `dyn_view`
/// inside the body re-renders while the gate is up when a
/// caller-owned signal changes — no cancel + reopen.
#[test]
fn body_dyn_view_updates_reactively_while_the_gate_is_up() {
    let (mut app, scope) = host(Size::new(W, H));
    let mut term = CaptureTerm::new(Size::new(W, H));
    let mut driver = boot(&mut app, &mut term);

    let tier = scope.signal(String::from("tier: safe commands only"));
    let log: OutcomeLog = Rc::default();
    ChoicePrompt::new("Approve this batch?")
        .option_key("ok", "Approve", 'a')
        .option("deny", "Deny")
        .body(move |_| dyn_view(LayoutStyle::line(1), move || text(tier.get())))
        .on_resolve(recorder(&log))
        .open(scope);
    settle(&mut driver, &mut app, &mut term);
    assert!(
        screen(&term).contains("tier: safe commands only"),
        "the body renders between prompt and options: {}",
        screen(&term)
    );

    tier.set(String::from("tier: ALL commands"));
    settle(&mut driver, &mut app, &mut term);
    assert!(
        screen(&term).contains("tier: ALL commands"),
        "the body re-renders live inside the open gate (0287): {}",
        screen(&term)
    );
    assert!(log.borrow().is_empty(), "a body update never resolves");

    term.push_input(b"a");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(log.borrow().as_slice(), [answered(&["ok"], None)]);
}

/// The scroll-routing contract: a `Scroll`-wrapped body scrolls under
/// the WHEEL while the pointer is over it (the Scroll consumes the
/// event before the gate's highlight handler); the wheel elsewhere and
/// the arrows/letters stay the OPTIONS' vocabulary.
#[test]
fn scrollable_body_composes_with_twenty_options() {
    let size = Size::new(W, 24);
    let (mut app, scope) = host(size);
    let mut term = CaptureTerm::new(size);
    let mut driver = boot(&mut app, &mut term);

    let log: OutcomeLog = Rc::default();
    let mut gate = ChoicePrompt::new("Run this batch?").body(|mcx| {
        let mut col = Element::new().style(LayoutStyle::column().width(Dimension::Percent(1.0)));
        for i in 1..=30 {
            col = col.child(text(format!("call {i:02} write_file")));
        }
        Scroll::new(col.build()).content_size(40, 30).view(mcx)
    });
    gate = gate.body_rows(6).option_key("go", "Opt 01", 'g');
    for i in 2..=20 {
        gate = gate.option(format!("opt{i}"), format!("Opt {i:02}"));
    }
    gate.on_resolve(recorder(&log)).open(scope);
    settle(&mut driver, &mut app, &mut term);

    let s = screen(&term);
    assert!(s.contains("call 01"), "body top visible: {s}");
    assert!(!s.contains("call 09"), "body clipped to its budget: {s}");
    assert!(s.contains("● Opt 01"), "options render below the body: {s}");

    // Wheel over the BODY: the Scroll consumes it (3-line notch), the
    // highlight does not move.
    let (bx, by) = locate(&term, "call 01").expect("body row on screen");
    term.push_input(format!("\x1b[<65;{};{}M", bx + 1, by + 1).as_bytes());
    settle(&mut driver, &mut app, &mut term);
    let s = screen(&term);
    assert!(
        s.contains("call 04") && !s.contains("call 01"),
        "wheel over the body scrolls the body: {s}"
    );
    assert!(
        s.contains("● Opt 01"),
        "wheel over the body must not move the highlight: {s}"
    );

    // Arrows stay with the options (keys route focused-first — the
    // body's Scroll never sees them) and the body holds its offset.
    term.push_input(b"\x1b[B");
    settle(&mut driver, &mut app, &mut term);
    let s = screen(&term);
    assert!(s.contains("● Opt 02"), "arrow moves the highlight: {s}");
    assert!(s.contains("call 04"), "arrow does not scroll the body: {s}");

    // Wheel over the OPTIONS still moves the highlight (existing gate
    // vocabulary, untouched by the body).
    let (ox, oy) = locate(&term, "Opt 03").expect("option row on screen");
    term.push_input(format!("\x1b[<65;{};{}M", ox + 1, oy + 1).as_bytes());
    settle(&mut driver, &mut app, &mut term);
    assert!(
        screen(&term).contains("● Opt 03"),
        "wheel over the options moves the highlight: {}",
        screen(&term)
    );

    // Declared letters still commit through the body.
    term.push_input(b"g");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(log.borrow().as_slice(), [answered(&["go"], None)]);
}

/// Height honesty at tiny viewports (the 0240 law): the options are
/// allocated FIRST and stay visible and operable; the body degrades to
/// its 1-row floor instead of crushing them.
#[test]
fn body_never_crushes_the_options_at_tiny_heights() {
    let size = Size::new(44, 9);
    let (mut app, scope) = host(size);
    let mut term = CaptureTerm::new(size);
    let mut driver = boot(&mut app, &mut term);

    let log: OutcomeLog = Rc::default();
    ChoicePrompt::new("Proceed?")
        .option("alpha", "Alpha")
        .option("beta", "Beta")
        .option("gamma", "Gamma")
        .body(|_| {
            // Rigid rows (shrink 0), like a real card list: the HOST's
            // clip decides what fits — the first row survives, the
            // second clips below the budget.
            Element::new()
                .style(
                    LayoutStyle::column()
                        .width(Dimension::Percent(1.0))
                        .shrink(0.0),
                )
                .child(text("body line 1"))
                .child(text("body line 2"))
                .build()
        })
        .body_rows(8)
        .on_resolve(recorder(&log))
        .open(scope);
    settle(&mut driver, &mut app, &mut term);

    let s = screen(&term);
    assert!(
        s.contains("● Alpha"),
        "the highlighted option survives a 9-row viewport: {s}"
    );
    assert!(
        s.contains("body line 1"),
        "the body keeps its 1-row floor: {s}"
    );
    assert!(
        !s.contains("body line 2"),
        "the body clips to its solved budget instead of painting over rows: {s}"
    );

    // The gate stays operable: movement + Enter commit.
    term.push_input(b"\x1b[B\r");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(log.borrow().as_slice(), [answered(&["beta"], None)]);
}

/// A lowercase-registered chord must NOT fire on the shifted letter
/// (Shift+A means 'A'): the fold adds the missing spelling, never a
/// case-insensitive blur.
#[test]
fn lowercase_chord_refuses_the_shifted_letter_on_both_wires() {
    let fired: Rc<RefCell<u32>> = Rc::default();
    let f = fired.clone();
    let mut app = App::new(Size::new(W, H));
    app.mount(move |_cx| {
        let f2 = f.clone();
        Element::new()
            .style(LayoutStyle::column())
            .focusable()
            .autofocus()
            .shortcut(KeyChord::plain(Key::Char('a')), move |_| {
                *f2.borrow_mut() += 1
            })
            .child(text("host"))
            .build()
    })
    .expect("mount");
    let mut term = CaptureTerm::new(Size::new(W, H));
    let mut driver = boot(&mut app, &mut term);
    term.push_input(KITTY_SHIFT_A);
    term.push_input(b"A");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(*fired.borrow(), 0, "Shift+A is 'A', never 'a'");
    term.push_input(b"a");
    settle(&mut driver, &mut app, &mut term);
    assert_eq!(*fired.borrow(), 1, "the declared 'a' itself still fires");
}