tuika 0.5.0

A composable terminal UI toolkit — flexbox layout, overlays, focus, and safe ratatui interoperability.
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
//! Application state and input routing.
//!
//! This is the "host" half of the example: everything tuika deliberately leaves
//! to the application — what the transcript contains, which surface owns the
//! keyboard right now, when the view sticks to the bottom, and what a slash
//! command does. tuika supplies the state types (`TextInputState`,
//! `ScrollState`, `SelectState`) and renders from them; the wiring below is the
//! part a real host writes.

use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};

use tuika::prelude::*;

use crate::agent::{Agent, Decision, interrupted_notice};
use crate::history::{Cell, Tone};

/// Whether the event loop should keep running.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Flow {
    Continue,
    Quit,
}

/// The slash commands the composer completes, in the order Codex lists them.
pub const COMMANDS: &[(&str, &str)] = &[
    (
        "/init",
        "create an AGENTS.md file with instructions for Codex",
    ),
    ("/status", "show current session configuration"),
    ("/approvals", "choose what Codex can do without approval"),
    ("/model", "choose what model and reasoning effort to use"),
    ("/new", "start a new chat during a conversation"),
    (
        "/compact",
        "summarize conversation to prevent hitting the context limit",
    ),
    ("/diff", "show git diff (including untracked files)"),
    ("/mention", "mention a file"),
    ("/quit", "exit Codex"),
];

const MODELS: &[(&str, &str)] = &[
    ("gpt-5-codex  medium", "default — balances speed and depth"),
    ("gpt-5-codex  high", "slower, more thorough reasoning"),
    ("gpt-5-codex  low", "fastest, for small mechanical edits"),
    ("gpt-5  medium", "the general model, not the coding tune"),
];

/// Files the `@` picker offers. A real host would search the workspace.
const FILES: &[(&str, &str)] = &[
    ("@src/lib.rs", "crate root"),
    ("@src/markdown.rs", "streaming CommonMark renderer"),
    ("@src/components/scroll.rs", "line viewport"),
    ("@AGENTS.md", "repository guidance"),
    ("@README.md", "public entry point"),
];

const APPROVAL_MODES: &[(&str, &str)] = &[
    ("Read Only", "Codex can read files and answer questions"),
    ("Auto", "reads, edits, and runs commands in the workspace"),
    (
        "Full Access",
        "network access and writes outside the workspace",
    ),
];

/// What the composer's trigger characters mean **here**.
///
/// tuika finds and delimits the tokens (see [`Trigger`]); everything below —
/// which character opens what, what the popup lists, what a completion inserts —
/// is this application's. Swap the table and `/` could summon an emoji picker.
pub fn triggers() -> [Trigger; 2] {
    [
        // Codex only treats `/` as a command when it opens the whole message.
        Trigger::new('/').anchor(TriggerAnchor::BufferStart),
        // A file mention can start any word, and completes to a path.
        Trigger::new('@'),
    ]
}

/// Which picker (if any) is stealing the composer's keys.
pub enum Popup {
    /// Completion for the token under the cursor: `/command` or `@file`,
    /// distinguished by the trigger the host declared, not by parsing the text.
    Token { trigger: char, state: SelectState },
    /// `/model`.
    Model(SelectState),
    /// `/approvals`.
    Approvals(SelectState),
}

/// A command the agent wants to run, waiting on the user.
pub struct Approval {
    pub command: String,
    pub state: SelectState,
}

/// The banner values and the meters the composer footer shows.
pub struct Session {
    pub version: String,
    pub model: String,
    pub cwd: String,
    pub approval: String,
    pub sandbox: String,
    pub tokens: u32,
    pub context_window: u32,
}

impl Session {
    fn new() -> Self {
        Self {
            version: "0.45.0".into(),
            model: "gpt-5-codex   medium reasoning".into(),
            cwd: "~/code/tuika".into(),
            approval: "on-request".into(),
            sandbox: "workspace-write".into(),
            tokens: 0,
            context_window: 272_000,
        }
    }

    /// Percentage of the context window still free, as Codex reports it.
    pub fn context_left(&self) -> u16 {
        let used = (self.tokens as f32 / self.context_window as f32 * 100.0).round() as u16;
        100u16.saturating_sub(used)
    }

    fn rows(&self) -> Vec<(String, String)> {
        vec![
            ("model".into(), self.model.clone()),
            ("directory".into(), self.cwd.clone()),
            ("approval".into(), self.approval.clone()),
            ("sandbox".into(), self.sandbox.clone()),
        ]
    }
}

/// The whole application.
pub struct App {
    pub frame: u64,
    pub cells: Vec<Cell>,
    pub composer: TextInputState,
    pub scroll: ScrollState,
    pub agent: Agent,
    pub popup: Option<Popup>,
    pub approval: Option<Approval>,
    pub session: Session,
    /// Frame the current turn started on, for the `Working (12s …)` timer.
    pub turn_started: Option<u64>,
    /// Previously submitted prompts, newest last (recalled with `Up`).
    history: Vec<String>,
    history_cursor: Option<usize>,
    /// Transcript geometry from the last frame, so paging keys have dimensions
    /// to work against before the next render.
    pub content_h: usize,
    pub viewport_h: usize,
    /// Set by `/quit`, which can be reached from the popup as well as the
    /// composer, so the flag is read once on the way out of `handle`.
    quit_requested: bool,
}

impl App {
    pub fn new() -> Self {
        let session = Session::new();
        let banner = Cell::Banner {
            version: session.version.clone(),
            rows: session.rows(),
            tips: COMMANDS
                .iter()
                .take(4)
                .map(|(c, b)| ((*c).to_string(), (*b).to_string()))
                .collect(),
        };
        Self {
            frame: 0,
            cells: vec![banner],
            composer: TextInputState::new(),
            scroll: ScrollState::new(),
            agent: Agent::new(),
            popup: None,
            approval: None,
            session,
            turn_started: None,
            history: Vec::new(),
            history_cursor: None,
            content_h: 0,
            viewport_h: 0,
            quit_requested: false,
        }
    }

    /// Advance animation and the scripted turn by one frame.
    pub fn tick(&mut self) {
        self.frame = self.frame.wrapping_add(1);
        self.agent.tick(&mut self.cells);
        self.session.tokens = self.agent.tokens;
        if !self.agent.is_running() {
            self.turn_started = None;
        }
        // A turn that hits a command needing approval raises the prompt here,
        // rather than from inside the agent, so the UI owns its own state.
        if let Some(command) = self.agent.pending_approval()
            && self.approval.is_none()
        {
            self.approval = Some(Approval {
                command: command.to_string(),
                state: SelectState::new(),
            });
        }
    }

    /// Seconds the current turn has been running, at the example's frame rate.
    pub fn elapsed_secs(&self) -> u64 {
        let started = self.turn_started.unwrap_or(self.frame);
        (self.frame.saturating_sub(started)) * crate::FRAME_MS / 1000
    }

    /// Where the terminal cursor belongs, given the composer's painted rect.
    pub fn cursor(&self, composer_rect: Rect) -> Option<(u16, u16)> {
        if self.approval.is_some() || composer_rect.width == 0 {
            return None;
        }
        Some(self.composer.cursor_screen(composer_rect))
    }

    /// The rows the current popup offers: `(label, blurb)`.
    ///
    /// The *source* is chosen by the trigger that opened the token, so adding a
    /// third trigger is one arm here and one entry in [`triggers`].
    pub fn popup_items(&self) -> Vec<(String, String)> {
        let (pairs, filter): (&[(&str, &str)], String) = match &self.popup {
            Some(Popup::Token { trigger, .. }) => {
                let token = self.composer.active_token(&triggers());
                let filter = token.map(|t| t.text).unwrap_or_default();
                match trigger {
                    '/' => (COMMANDS, filter),
                    _ => (FILES, filter),
                }
            }
            Some(Popup::Model(_)) => (MODELS, String::new()),
            Some(Popup::Approvals(_)) => (APPROVAL_MODES, String::new()),
            None => return Vec::new(),
        };
        pairs
            .iter()
            .filter(|(label, _)| label.starts_with(filter.trim()))
            .map(|(label, blurb)| ((*label).to_string(), (*blurb).to_string()))
            .collect()
    }

    /// Route one translated event. The order here *is* the focus model: modal
    /// surfaces first, then the picker, then the composer.
    pub fn handle(&mut self, event: &Event) -> Flow {
        let flow = self.route(event);
        if self.quit_requested {
            return Flow::Quit;
        }
        flow
    }

    fn route(&mut self, event: &Event) -> Flow {
        if self.scrolling(event) {
            let _ = self.scroll.handle(event, self.content_h, self.viewport_h);
            return Flow::Continue;
        }
        let Event::Key(key) = event else {
            return Flow::Continue;
        };

        if key.ctrl && key.code == KeyCode::Char('c') {
            return self.interrupt_or_quit();
        }
        if key.ctrl && key.code == KeyCode::Char('d') && self.composer.is_empty() {
            return Flow::Quit;
        }
        if self.approval.is_some() {
            self.handle_approval(event, *key);
            return Flow::Continue;
        }
        if self.popup.is_some() && self.handle_popup(event, *key) {
            return Flow::Continue;
        }
        if key.plain() && key.code == KeyCode::Esc {
            if self.agent.interrupt() {
                self.cells.push(interrupted_notice());
                self.follow();
            }
            return Flow::Continue;
        }
        // `Up` on an empty composer recalls the previous prompt, as in Codex.
        if key.plain() && key.code == KeyCode::Up && self.composer.is_empty() {
            self.recall();
            return Flow::Continue;
        }

        match self.composer.handle(event) {
            Some(TextInputEvent::Submit) => {
                let text = self.composer.text().trim().to_string();
                self.composer.clear();
                self.popup = None;
                self.history_cursor = None;
                if !text.is_empty() {
                    self.submit(&text);
                }
            }
            Some(TextInputEvent::Changed) | None => self.sync_popup(),
        }
        Flow::Continue
    }

    /// Events that belong to the transcript rather than to any focused surface.
    fn scrolling(&self, event: &Event) -> bool {
        match event {
            Event::Mouse(m) => matches!(m.kind, MouseKind::ScrollUp | MouseKind::ScrollDown),
            Event::Key(k) => k.plain() && matches!(k.code, KeyCode::PageUp | KeyCode::PageDown),
            _ => false,
        }
    }

    fn interrupt_or_quit(&mut self) -> Flow {
        if self.approval.take().is_some() {
            self.agent.approve(Decision::Deny, &mut self.cells);
            return Flow::Continue;
        }
        if self.agent.interrupt() {
            self.cells.push(interrupted_notice());
            self.follow();
            return Flow::Continue;
        }
        Flow::Quit
    }

    fn handle_approval(&mut self, event: &Event, key: Key) {
        let Some(approval) = &mut self.approval else {
            return;
        };
        // Codex accepts the digit shortcuts as well as the caret.
        let picked = match key.code {
            KeyCode::Char('1') if key.plain() => Some(0),
            KeyCode::Char('2') if key.plain() => Some(1),
            KeyCode::Char('3') if key.plain() => Some(2),
            _ => match approval.state.handle(event, 3) {
                SelectOutcome::Confirmed(i) => Some(i),
                SelectOutcome::Cancelled => Some(2),
                SelectOutcome::Moved(_) => None,
            },
        };
        let Some(index) = picked else { return };
        let decision = match index {
            0 => Decision::Once,
            1 => Decision::Session,
            _ => Decision::Deny,
        };
        self.approval = None;
        self.agent.approve(decision, &mut self.cells);
        self.follow();
    }

    /// Returns true when the popup consumed the event.
    fn handle_popup(&mut self, event: &Event, key: Key) -> bool {
        let items = self.popup_items();
        let Some(popup) = self.popup.as_mut() else {
            return false;
        };
        let is_token = matches!(popup, Popup::Token { .. });
        let state = match popup {
            Popup::Token { state, .. } | Popup::Model(state) | Popup::Approvals(state) => state,
        };
        // Tab completes the highlighted row in place, without running it.
        if key.plain() && key.code == KeyCode::Tab {
            let completion = items.get(state.selected()).map(|(label, _)| label.clone());
            if is_token && let Some(label) = completion {
                self.complete(&label);
            }
            return true;
        }
        match state.handle(event, items.len()) {
            SelectOutcome::Confirmed(index) => {
                let label = items.get(index).map(|(l, _)| l.clone());
                if let (Some(label), Some(kind)) = (label, self.popup.take()) {
                    self.confirm_popup(kind, &label);
                }
                true
            }
            SelectOutcome::Cancelled => {
                self.popup = None;
                true
            }
            SelectOutcome::Moved(flow) => flow == EventFlow::Consumed,
        }
    }

    fn confirm_popup(&mut self, popup: Popup, label: &str) {
        match popup {
            // A command runs on confirm; a mention completes into the text and
            // leaves the user typing. Same popup machinery, different verbs —
            // which is the point: the trigger decides, not the widget.
            Popup::Token { trigger: '/', .. } => {
                self.composer.clear();
                self.submit(label);
            }
            Popup::Token { .. } => self.complete(&format!("{label} ")),
            Popup::Model(_) => {
                self.session.model = label.to_string();
                self.cells.push(Cell::Notice {
                    tone: Tone::Info,
                    title: format!("Model set to {label}"),
                    body: Vec::new(),
                });
                self.follow();
            }
            Popup::Approvals(_) => {
                self.session.approval = label.to_ascii_lowercase().replace(' ', "-");
                self.cells.push(Cell::Notice {
                    tone: Tone::Info,
                    title: format!("Approval mode set to {label}"),
                    body: Vec::new(),
                });
                self.follow();
            }
        }
    }

    /// Open, refilter, or close the completion popup after the composer changed.
    ///
    /// The whole rule is "is the cursor inside a token?" — tuika answers that
    /// from the declared triggers, so this host never scans the text itself.
    pub fn sync_popup(&mut self) {
        let active = self.composer.active_token(&triggers());
        match (&self.popup, active) {
            (Some(Popup::Model(_)) | Some(Popup::Approvals(_)), _) => {}
            (_, None) => {
                if matches!(self.popup, Some(Popup::Token { .. })) {
                    self.popup = None;
                }
            }
            (Some(Popup::Token { trigger, state }), Some(token)) if *trigger == token.trigger => {
                // The filter shrank the list under the caret; pull it back in.
                let mut state = *state;
                let len = self.popup_items().len();
                state.clamp(len);
                self.popup = Some(Popup::Token {
                    trigger: token.trigger,
                    state,
                });
            }
            (_, Some(token)) => {
                self.popup = Some(Popup::Token {
                    trigger: token.trigger,
                    state: SelectState::new(),
                });
            }
        }
    }

    /// Replace the token under the cursor with `replacement`.
    fn complete(&mut self, replacement: &str) {
        let Some(token) = self.composer.active_token(&triggers()) else {
            return;
        };
        self.composer.replace_token(&token, replacement);
        self.sync_popup();
    }

    /// The composer's tokens as styled ranges — mentions and commands colored
    /// in the input itself, the way Codex marks them.
    pub fn composer_highlights(&self, theme: &Theme) -> Vec<tuika::components::TextSpan> {
        self.composer
            .tokens(&triggers())
            .iter()
            .map(|token: &Token| {
                let style = match token.trigger {
                    '/' => Style::default()
                        .fg(theme.accent_alt)
                        .add_modifier(Modifier::BOLD),
                    _ => Style::default().fg(theme.accent),
                };
                token.span(style)
            })
            .collect()
    }

    fn recall(&mut self) {
        if self.history.is_empty() {
            return;
        }
        let next = match self.history_cursor {
            None => self.history.len() - 1,
            Some(i) => i.saturating_sub(1),
        };
        self.history_cursor = Some(next);
        let text = self.history[next].clone();
        self.composer.set_text(&text);
    }

    /// Accept a submitted prompt: a slash command, or a turn for the agent.
    pub fn submit(&mut self, text: &str) {
        self.history.push(text.to_string());
        if let Some(command) = text.strip_prefix('/') {
            self.slash(command.split_whitespace().next().unwrap_or(""));
            self.follow();
            return;
        }
        self.cells.push(Cell::User(text.to_string()));
        self.agent.start(text);
        self.turn_started = Some(self.frame);
        self.follow();
    }

    fn slash(&mut self, command: &str) {
        match command {
            "init" => {
                self.cells.push(Cell::User("/init".into()));
                self.agent.start("/init");
                self.turn_started = Some(self.frame);
            }
            "status" => self.cells.push(Cell::Config {
                title: "Session".into(),
                rows: {
                    let mut rows = self.session.rows();
                    rows.push((
                        "tokens".into(),
                        format!(
                            "{} used · {}% context left",
                            self.session.tokens,
                            self.session.context_left()
                        ),
                    ));
                    rows
                },
            }),
            "model" => self.popup = Some(Popup::Model(SelectState::new())),
            "approvals" => self.popup = Some(Popup::Approvals(SelectState::new())),
            "new" => {
                let banner = Cell::Banner {
                    version: self.session.version.clone(),
                    rows: self.session.rows(),
                    tips: COMMANDS
                        .iter()
                        .take(4)
                        .map(|(c, b)| ((*c).to_string(), (*b).to_string()))
                        .collect(),
                };
                self.cells = vec![banner];
                self.agent = Agent::new();
                self.session.tokens = 0;
            }
            "compact" => self.cells.push(Cell::Notice {
                tone: Tone::Info,
                title: "Compacted context".into(),
                body: vec![format!(
                    "summarized the conversation so far — {}% context left",
                    self.session.context_left()
                )],
            }),
            "diff" => self.cells.push(Cell::Patch {
                header: "Working tree diff (1 file, +2 -1)".into(),
                hunk: vec![
                    "diff --git a/src/snapshots/composer.txt b/src/snapshots/composer.txt".into(),
                    "@@ -12,4 +12,5 @@".into(),
                    " ╰──────────────────────────────╯".into(),
                    "-".into(),
                    "+  ⏎ send   ⇧⏎ newline   ⌃C quit".into(),
                    "+".into(),
                ],
            }),
            "quit" | "exit" => self.quit_requested = true,
            "mention" => {
                self.composer.set_text("@");
                self.sync_popup();
            }
            other => self.cells.push(Cell::Notice {
                tone: Tone::Error,
                title: format!("Unknown command: /{other}"),
                body: vec!["press / to see the available commands".into()],
            }),
        }
    }

    /// Re-arm the stick-to-bottom follow after appending to the transcript.
    fn follow(&mut self) {
        self.scroll.jump_to_bottom(self.content_h, self.viewport_h);
    }

    /// Rebuild the transcript as one item per cell, laid out to `width`.
    ///
    /// Rebuilding every frame is the model working as intended: only the
    /// streaming answer holds a cache, and `ratatui` diffs the resulting cells.
    pub fn transcript(&mut self, width: u16, theme: &Theme, sheet: &StyleSheet) -> Vec<Element> {
        self.cells
            .iter_mut()
            .map(|cell| cell.view(width, theme, sheet))
            .collect()
    }
}

/// A palette close to what Codex draws on a dark terminal: near-black behind
/// everything, cyan for its own marks, and a muted gray for machine output.
pub fn codex_theme() -> Theme {
    Theme {
        background: Color::Rgb(13, 14, 16),
        surface: Color::Rgb(23, 25, 28),
        text: Color::Rgb(223, 226, 230),
        muted: Color::Rgb(142, 148, 158),
        dim: Color::Rgb(88, 94, 104),
        accent: Color::Rgb(94, 187, 209),
        accent_alt: Color::Rgb(197, 154, 231),
        border: Color::Rgb(60, 66, 74),
        border_focused: Color::Rgb(94, 187, 209),
        selection_bg: Color::Rgb(35, 44, 52),
        selection_fg: Color::Rgb(235, 238, 242),
        code: CodeTheme {
            link: Color::Rgb(122, 172, 240),
            string: Color::Rgb(140, 200, 140),
            heading: Color::Rgb(223, 226, 230),
            ..CodeTheme::default()
        },
    }
}