eye_declare 0.7.1

Inline terminal UIs for Rust: timeline-first, Elm-shaped, built on Ratatui
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
//! A complete app driven headlessly: synthetic key events in, VTE-verified
//! terminal contents out. This is the snapshot-testing story downstream
//! users get — the framework tests itself the same way.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use eye_declare::{
    App, Ctx, Element, ElementExt, Fluent, Focus, FocusHandle, InputEvent, Keymap, Runtime, col,
    key, keymap, text,
};
use eye_declare_engine::test_terminal::TestTerminal;

#[derive(Clone)]
enum Msg {
    Submit,
    Typed(char),
    Backspace,
    Quit,
}

#[derive(Default)]
enum Outcome {
    #[default]
    Cancelled,
    Finished(usize),
}

/// Minimal echo app: type a line, Enter commits it as a block, Ctrl+C
/// exits with the number of submitted lines.
struct Echo {
    input_focus: FocusHandle,
    typed: String,
    submitted: usize,
}

impl Echo {
    fn new(focus: &Focus) -> Self {
        let input_focus = focus.handle();
        input_focus.focus();
        Self {
            input_focus,
            typed: String::new(),
            submitted: 0,
        }
    }
}

impl App for Echo {
    type Msg = Msg;
    type Output = Outcome;

    fn update(&mut self, msg: Msg, ctx: &mut Ctx<'_, Self>) {
        match msg {
            Msg::Typed(c) => self.typed.push(c),
            Msg::Backspace => {
                self.typed.pop();
            }
            Msg::Submit => {
                if !self.typed.is_empty() {
                    let line = std::mem::take(&mut self.typed);
                    ctx.push(text(format!("* {line}")));
                    self.submitted += 1;
                }
            }
            Msg::Quit => ctx.exit(Outcome::Finished(self.submitted)),
        }
    }

    fn tail(&self) -> impl Element + '_ {
        col()
            .child(text(format!("> {}", self.typed)))
            .when(self.submitted > 0, |c| {
                c.child(text(format!("({} sent)", self.submitted)))
            })
    }

    fn keymap(&self) -> Keymap<Msg> {
        keymap()
            .on_override(key(KeyCode::Char('c')).ctrl(), Msg::Quit)
            .in_scope(&self.input_focus, key(KeyCode::Enter), Msg::Submit)
            .fallthrough(&self.input_focus, |ev| match ev {
                InputEvent::Key(k) => match k.code {
                    KeyCode::Char(c) => Msg::Typed(c),
                    KeyCode::Backspace => Msg::Backspace,
                    _ => Msg::Typed('?'),
                },
                InputEvent::Paste(_) => Msg::Typed('P'),
                _ => Msg::Typed('?'),
            })
    }
}

fn press(code: KeyCode) -> InputEvent {
    InputEvent::Key(KeyEvent::new(code, KeyModifiers::NONE))
}

fn type_str(rt: &mut Runtime<Echo>, term: &mut TestTerminal, s: &str) {
    for c in s.chars() {
        let (bytes, exit) = rt.handle(press(KeyCode::Char(c)));
        assert!(exit.is_none());
        term.feed(&bytes);
    }
}

#[test]
fn echo_session_end_to_end() {
    let focus = Focus::new();
    let mut rt = Runtime::new(Echo::new(&focus), 20, 24);
    let mut term = TestTerminal::new(20, 24);

    term.feed(&rt.present());
    assert_eq!(term.viewport_lines()[0], ">");

    // Type and watch the tail echo.
    type_str(&mut rt, &mut term, "hello");
    assert_eq!(term.viewport_lines()[0], "> hello");

    // Backspace edits.
    let (bytes, _) = rt.handle(press(KeyCode::Backspace));
    term.feed(&bytes);
    assert_eq!(term.viewport_lines()[0], "> hell");

    // Enter commits the line as a block; tail resets below it.
    let (bytes, _) = rt.handle(press(KeyCode::Enter));
    term.feed(&bytes);
    assert_eq!(term.viewport_lines()[0], "* hell");
    assert_eq!(term.viewport_lines()[1], ">");
    assert_eq!(term.viewport_lines()[2], "(1 sent)");

    // Another line stacks below the first block.
    type_str(&mut rt, &mut term, "again");
    let (bytes, _) = rt.handle(press(KeyCode::Enter));
    term.feed(&bytes);
    assert_eq!(term.viewport_lines()[0], "* hell");
    assert_eq!(term.viewport_lines()[1], "* again");
    assert_eq!(term.viewport_lines()[2], ">");
    assert_eq!(term.viewport_lines()[3], "(2 sent)");

    // Ctrl+C exits with the outcome; committed history survives.
    let (bytes, exit) = rt.handle(InputEvent::Key(KeyEvent::new(
        KeyCode::Char('c'),
        KeyModifiers::CONTROL,
    )));
    term.feed(&bytes);
    match exit {
        Some(Outcome::Finished(n)) => assert_eq!(n, 2),
        _ => panic!("expected Finished exit"),
    }
    assert_eq!(term.viewport_lines()[0], "* hell");
    assert_eq!(term.viewport_lines()[1], "* again");

    // Exit parks the cursor on a fresh line below the still-visible tail,
    // so whatever main() prints after run() returns doesn't land on top
    // of it.
    assert_eq!(term.viewport_lines()[2], ">");
    assert_eq!(term.viewport_lines()[3], "(2 sent)");
    assert_eq!(term.cursor(), (4, 0));
    term.feed(b"echoed 2 line(s)\r\n");
    assert_eq!(term.viewport_lines()[3], "(2 sent)");
    assert_eq!(term.viewport_lines()[4], "echoed 2 line(s)");
}

#[test]
fn init_exit_parks_cursor_below_content() {
    // An exit requested from App::init bypasses the message loop, so the
    // shell handoff must come from startup() itself.
    struct PrintAndQuit;

    impl App for PrintAndQuit {
        type Msg = ();
        type Output = ();

        fn init(&mut self, ctx: &mut Ctx<'_, Self>) {
            ctx.push(text("one-shot output"));
            ctx.exit(());
        }

        fn update(&mut self, _msg: (), _ctx: &mut Ctx<'_, Self>) {}

        fn tail(&self) -> impl Element + '_ {
            text("tail line")
        }

        fn keymap(&self) -> Keymap<()> {
            keymap()
        }
    }

    let mut rt = Runtime::new(PrintAndQuit, 20, 24);
    let mut term = TestTerminal::new(20, 24);

    let (bytes, exit) = rt.startup();
    term.feed(&bytes);
    assert!(exit.is_some());

    assert_eq!(term.viewport_lines()[0], "one-shot output");
    assert_eq!(term.viewport_lines()[1], "tail line");
    assert_eq!(term.cursor(), (2, 0));
    term.feed(b"$ prompt");
    assert_eq!(term.viewport_lines()[2], "$ prompt");
}

#[test]
fn tail_hidden_before_deferred_exit_leaves_no_gap() {
    // The Atuin AI exit shape: one update hides the tail (the input box
    // disappears in its own frame), a later message actually exits. The
    // vacated rows must be reclaimed with no blank line left between the
    // committed content and the shell prompt.
    struct HideThenQuit {
        exiting: bool,
    }

    #[derive(Clone)]
    enum HMsg {
        Commit,
        Hide,
        Quit,
    }

    impl App for HideThenQuit {
        type Msg = HMsg;
        type Output = ();

        fn update(&mut self, msg: HMsg, ctx: &mut Ctx<'_, Self>) {
            match msg {
                HMsg::Commit => ctx.push(text("committed line")),
                HMsg::Hide => self.exiting = true,
                HMsg::Quit => ctx.exit(()),
            }
        }

        fn tail(&self) -> impl Element + '_ {
            if self.exiting {
                col()
            } else {
                col().child(text("| input box |")).child(text("footer"))
            }
        }

        fn keymap(&self) -> Keymap<HMsg> {
            keymap()
        }
    }

    let mut rt = Runtime::new(HideThenQuit { exiting: false }, 20, 24);
    let mut term = TestTerminal::new(20, 24);

    term.feed(&rt.present());
    let (bytes, _) = rt.process(HMsg::Commit);
    term.feed(&bytes);
    assert_eq!(term.viewport_lines()[1], "| input box |");
    assert_eq!(term.viewport_lines()[2], "footer");

    let (bytes, exit) = rt.process(HMsg::Hide);
    term.feed(&bytes);
    assert!(exit.is_none());
    let (bytes, exit) = rt.process(HMsg::Quit);
    term.feed(&bytes);
    assert!(exit.is_some());

    assert_eq!(term.viewport_lines()[0], "committed line");
    assert_eq!(term.viewport_lines()[1], "");
    assert_eq!(term.cursor(), (1, 0));
    term.feed(b"$ prompt");
    assert_eq!(term.viewport_lines()[1], "$ prompt");
}

#[test]
fn unclaimed_events_produce_no_output() {
    let focus = Focus::new();
    let mut rt = Runtime::new(Echo::new(&focus), 20, 24);
    let mut term = TestTerminal::new(20, 24);
    term.feed(&rt.present());

    // F5 matches nothing: no bindings, fallthrough maps it to Typed('?')…
    // actually fallthrough claims everything for the focused input. Blur
    // first so nothing is focused.
    focus.blur_all();
    let (bytes, exit) = rt.handle(press(KeyCode::F(5)));
    assert!(bytes.is_empty());
    assert!(exit.is_none());
}

#[test]
fn conditional_bindings_rebuild_per_update() {
    // The "(n sent)" line only appears after a submit — and so could a
    // binding. Verify keymap() is consulted fresh each event by toggling
    // focus between events.
    let focus = Focus::new();
    let app = Echo::new(&focus);
    let input_focus = app.input_focus.clone();
    let mut rt = Runtime::new(app, 20, 24);
    let mut term = TestTerminal::new(20, 24);
    term.feed(&rt.present());

    type_str(&mut rt, &mut term, "a");
    assert_eq!(term.viewport_lines()[0], "> a");

    input_focus.blur();
    let (bytes, _) = rt.handle(press(KeyCode::Char('b')));
    assert!(bytes.is_empty(), "typing while blurred does nothing");

    input_focus.focus();
    type_str(&mut rt, &mut term, "c");
    assert_eq!(term.viewport_lines()[0], "> ac");
}

#[test]
fn borrowed_blocks_push_cleanly() {
    // Ctx::push renders immediately, so blocks may borrow from update
    // locals — this is the API shape the driver port needed.
    struct OneShot {
        events: Vec<String>,
        done: bool,
    }

    impl App for OneShot {
        type Msg = ();
        type Output = ();

        fn update(&mut self, _msg: (), ctx: &mut Ctx<'_, Self>) {
            let events = std::mem::take(&mut self.events);
            ctx.push(col().children(events.iter().map(|e| text(e.as_str()).pad_left(2))));
            self.done = true;
            ctx.exit(());
        }

        fn tail(&self) -> impl Element + '_ {
            text(if self.done { "" } else { "working" })
        }
    }

    let mut rt = Runtime::new(
        OneShot {
            events: vec!["one".into(), "two".into()],
            done: false,
        },
        20,
        24,
    );
    let mut term = TestTerminal::new(20, 24);
    term.feed(&rt.present());

    let (bytes, exit) = rt.process(());
    term.feed(&bytes);
    assert!(exit.is_some());
    assert_eq!(term.viewport_lines()[0], "  one");
    assert_eq!(term.viewport_lines()[1], "  two");
}

#[test]
fn init_pushes_precede_first_frame_and_later_pushes() {
    struct Banner {
        submitted: bool,
    }

    impl App for Banner {
        type Msg = ();
        type Output = ();

        fn init(&mut self, ctx: &mut Ctx<'_, Self>) {
            ctx.push(text("welcome banner"));
        }

        fn update(&mut self, _msg: (), ctx: &mut Ctx<'_, Self>) {
            self.submitted = true;
            ctx.push(text("first message"));
        }

        fn tail(&self) -> impl Element + '_ {
            text(if self.submitted { "done" } else { "prompt" })
        }
    }

    // The driver path: startup() delivers init's block before the tail.
    let mut rt = Runtime::new(Banner { submitted: false }, 40, 10);
    let mut term = TestTerminal::new(40, 10);
    let (bytes, exit) = rt.startup();
    assert!(exit.is_none());
    term.feed(&bytes);
    let screen = term.viewport_lines().join("\n");
    let banner_row = screen.find("welcome banner").expect("banner missing");
    let prompt_row = screen.find("prompt").expect("tail missing");
    assert!(banner_row < prompt_row, "init block must precede the tail");

    // A later push lands below the banner.
    let (bytes, _) = rt.process(());
    term.feed(&bytes);
    let all = [term.scrollback_lines(), term.viewport_lines()]
        .concat()
        .join("\n");
    let banner = all.find("welcome banner").unwrap();
    let first = all.find("first message").unwrap();
    assert!(
        banner < first,
        "init block stays above later pushes:\n{all}"
    );
}

#[test]
fn init_bytes_survive_process_without_present() {
    struct P;
    impl App for P {
        type Msg = ();
        type Output = ();
        fn init(&mut self, ctx: &mut Ctx<'_, Self>) {
            ctx.push(text("from init"));
        }
        fn update(&mut self, _msg: (), ctx: &mut Ctx<'_, Self>) {
            ctx.push(text("from update"));
        }
        fn tail(&self) -> impl Element + '_ {
            text("tail")
        }
    }

    // Skipping startup() and going straight to process(): init's block
    // must still come out first.
    let mut rt = Runtime::new(P, 40, 10);
    let mut term = TestTerminal::new(40, 10);
    let (bytes, _) = rt.process(());
    term.feed(&bytes);
    let all = [term.scrollback_lines(), term.viewport_lines()]
        .concat()
        .join("\n");
    assert!(
        all.find("from init").unwrap() < all.find("from update").unwrap(),
        "wrong order:\n{all}"
    );
}