envision 0.15.1

A ratatui framework for collaborative TUI development with headless testing support
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
#![allow(
    clippy::collapsible_if,
    clippy::single_match,
    clippy::type_complexity,
    clippy::collapsible_match
)]
// =============================================================================
// Chat Client — Reference Application #3
// =============================================================================
//
// An AI chat client demonstrating the "Claude Code" component suite:
//
// - **ConversationView**: Structured message thread with roles
// - **MessageHandle**: Stable streaming identity
// - **CommandPalette**: Slash commands (/help, /clear, /model)
// - **TabBar**: Multiple conversation tabs
// - **TextArea**: Multi-line input
// - **StatusBar**: Token count and model info
// - **Markdown**: Rendered assistant responses
// - **CodeBlock**: Syntax-highlighted code in responses
//
// Run: cargo run --example chat_client --features full
//
// Layout:
// ┌ Chat 1 │ Chat 2 • │ + ────────────────────────┐
// │ ● User                                         │
// │ How do I sort a vector?                         │
// │                                                 │
// │ ◆ Assistant                                     │
// │ Here's how to sort in Rust:                     │
// │ │ let mut v = vec![3, 1, 2];                    │
// │ │ v.sort();                                     │
// ├─────────────────────────────────────────────────┤
// │ > Type a message...                             │
// ├─────────────────────────────────────────────────┤
// │ Model: claude │ Messages: 4 │ Ctrl+P: commands  │
// └─────────────────────────────────────────────────┘

use envision::prelude::*;

// ---------------------------------------------------------------------------
// Focus
// ---------------------------------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum Focus {
    Conversation,
    Input,
}

// ---------------------------------------------------------------------------
// App message
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
enum Msg {
    Conv(ConversationViewMessage),
    Input(TextAreaMessage),
    TabBar(TabBarMessage),
    Palette(CommandPaletteMessage),

    SubmitInput,
    FocusToggle,
    TogglePalette,
    NewTab,
    CloseTab,
    SimulateResponse,
    Quit,
}

// ---------------------------------------------------------------------------
// Conversation state per tab
// ---------------------------------------------------------------------------

#[derive(Clone)]
struct Conversation {
    view: ConversationViewState,
    /// Handle to the currently streaming message (if any)
    streaming_handle: Option<MessageHandle>,
}

impl Conversation {
    fn new(title: &str) -> Self {
        Self {
            view: ConversationViewState::new()
                .with_title(title)
                .with_markdown(true)
                .with_show_role_labels(true),
            streaming_handle: None,
        }
    }
}

// ---------------------------------------------------------------------------
// App state
// ---------------------------------------------------------------------------

#[derive(Clone)]
struct State {
    conversations: Vec<Conversation>,
    active_tab: usize,
    tab_bar: TabBarState,
    input: TextAreaState,
    palette: CommandPaletteState,
    status: StatusBarState,
    focus: FocusManager<Focus>,
    message_count: usize,
}

impl State {
    fn active_conv(&self) -> &Conversation {
        &self.conversations[self.active_tab]
    }
    fn active_conv_mut(&mut self) -> &mut Conversation {
        &mut self.conversations[self.active_tab]
    }
}

// ---------------------------------------------------------------------------
// App
// ---------------------------------------------------------------------------

struct ChatClient;

impl App for ChatClient {
    type State = State;
    type Message = Msg;

    fn init() -> (State, Command<Msg>) {
        let palette_items = vec![
            PaletteItem::new("help", "Show Help")
                .with_shortcut("F1")
                .with_category("General"),
            PaletteItem::new("clear", "Clear Conversation")
                .with_shortcut("Ctrl+L")
                .with_category("Actions"),
            PaletteItem::new("new", "New Conversation")
                .with_shortcut("Ctrl+N")
                .with_category("Actions"),
            PaletteItem::new("close", "Close Tab")
                .with_shortcut("Ctrl+W")
                .with_category("Actions"),
            PaletteItem::new("quit", "Quit")
                .with_shortcut("Ctrl+Q")
                .with_category("General"),
        ];

        let mut status = StatusBarState::new();
        status.set_left(vec![StatusBarItem::new("Model: claude-3")]);
        status.set_center(vec![StatusBarItem::new("Messages: 0")]);
        status.set_right(vec![StatusBarItem::new(
            "Tab: switch │ Ctrl+P: commands │ Ctrl+Enter: send",
        )]);

        let initial_conv = Conversation::new("Chat 1");
        let tab_bar = TabBarState::new(vec![Tab::new("chat-1", "Chat 1").with_closable(true)]);

        let state = State {
            conversations: vec![initial_conv],
            active_tab: 0,
            tab_bar,
            input: TextAreaState::new().with_placeholder("Type a message... (Ctrl+Enter to send)"),
            palette: CommandPaletteState::new(palette_items)
                .with_title("Commands")
                .with_placeholder("Type a command..."),
            status,
            focus: FocusManager::with_initial_focus(vec![Focus::Input, Focus::Conversation]),
            message_count: 0,
        };

        (state, Command::none())
    }

    fn update(state: &mut State, msg: Msg) -> Command<Msg> {
        match msg {
            Msg::Conv(m) => {
                ConversationView::update(&mut state.active_conv_mut().view, m);
            }
            Msg::Input(m) => {
                TextArea::update(&mut state.input, m);
            }
            Msg::TabBar(m) => {
                if let Some(output) = TabBar::update(&mut state.tab_bar, m) {
                    match output {
                        TabBarOutput::TabSelected(idx) => {
                            if idx < state.conversations.len() {
                                state.active_tab = idx;
                                update_status(state);
                            }
                        }
                        TabBarOutput::TabClosed(_idx) => {
                            // Remove conversation
                            if state.conversations.len() > 1
                                && state.active_tab < state.conversations.len()
                            {
                                state.conversations.remove(state.active_tab);
                                if state.active_tab >= state.conversations.len() {
                                    state.active_tab = state.conversations.len().saturating_sub(1);
                                }
                                state.tab_bar.set_selected(Some(state.active_tab));
                            }
                        }
                        _ => {}
                    }
                }
            }
            Msg::Palette(m) => {
                if let Some(output) = CommandPalette::update(&mut state.palette, m) {
                    match output {
                        CommandPaletteOutput::Selected(item) => match item.id.as_str() {
                            "clear" => {
                                state.active_conv_mut().view.clear_messages();
                                state.message_count = 0;
                                update_status(state);
                            }
                            "new" => return Self::update(state, Msg::NewTab),
                            "close" => return Self::update(state, Msg::CloseTab),
                            "quit" => return Command::quit(),
                            "help" => {
                                state.active_conv_mut().view.push_system(
                                    "**Help**: Type a message and press Ctrl+Enter to send. \
                                     Use Ctrl+P for commands, Tab to switch focus.",
                                );
                            }
                            _ => {}
                        },
                        _ => {}
                    }
                }
            }

            Msg::SubmitInput => {
                let text = state.input.value();
                if !text.trim().is_empty() {
                    // Push user message
                    state.active_conv_mut().view.push_user(&text);
                    state.input.set_value("");
                    state.message_count += 1;

                    // Mark tab as modified
                    if let Some(tab) = state.tab_bar.active_tab_mut() {
                        tab.set_modified(true);
                    }

                    update_status(state);

                    // Simulate assistant response
                    return Command::message(Msg::SimulateResponse);
                }
            }

            Msg::SimulateResponse => {
                let response = generate_response(state.message_count);

                // Push assistant message with handle for streaming
                let handle = state.active_conv_mut().view.push_assistant("");
                state.active_conv_mut().streaming_handle = Some(handle);

                // Simulate "streaming" by setting the full content immediately
                // (In a real app, you'd append incrementally via update_by_handle)
                state
                    .active_conv_mut()
                    .view
                    .update_by_handle(handle, |msg| {
                        msg.set_blocks(vec![MessageBlock::text(&response)]);
                    });
                state.active_conv_mut().streaming_handle = None;

                state.message_count += 1;
                update_status(state);
            }

            Msg::FocusToggle => {
                state.focus.focus_next();
            }

            Msg::TogglePalette => {
                if state.palette.is_visible() {
                    state.palette.dismiss();
                } else {
                    state.palette.show();
                }
            }

            Msg::NewTab => {
                let idx = state.conversations.len() + 1;
                let name = format!("Chat {}", idx);
                let conv = Conversation::new(&name);
                state.conversations.push(conv);

                TabBar::update(
                    &mut state.tab_bar,
                    TabBarMessage::AddTab(
                        Tab::new(format!("chat-{}", idx), &name).with_closable(true),
                    ),
                );
                state.active_tab = state.conversations.len() - 1;
                state.tab_bar.set_selected(Some(state.active_tab));
            }

            Msg::CloseTab => {
                if state.conversations.len() > 1 {
                    TabBar::update(
                        &mut state.tab_bar,
                        TabBarMessage::CloseTab(state.active_tab),
                    );
                    state.conversations.remove(state.active_tab);
                    if state.active_tab >= state.conversations.len() {
                        state.active_tab = state.conversations.len().saturating_sub(1);
                    }
                    state.tab_bar.set_selected(Some(state.active_tab));
                }
            }

            Msg::Quit => return Command::quit(),
        }
        Command::none()
    }

    fn view(state: &State, frame: &mut Frame) {
        let theme = Theme::default();
        let area = frame.area();

        // Layout: tab bar + conversation + input + status
        let chunks = Layout::vertical([
            Constraint::Length(1), // Tab bar
            Constraint::Min(0),    // Conversation view
            Constraint::Length(3), // Input area
            Constraint::Length(1), // Status bar
        ])
        .split(area);

        let conv_focused = state.focus.is_focused(&Focus::Conversation);
        let input_focused = state.focus.is_focused(&Focus::Input);

        // Tab bar
        TabBar::view(
            &state.tab_bar,
            &mut RenderContext::new(frame, chunks[0], &theme).focused(true),
        );

        // Conversation
        ConversationView::view(
            &state.active_conv().view,
            &mut RenderContext::new(frame, chunks[1], &theme).focused(conv_focused),
        );

        // Input
        TextArea::view(
            &state.input,
            &mut RenderContext::new(frame, chunks[2], &theme).focused(input_focused),
        );

        // Status
        StatusBar::view(
            &state.status,
            &mut RenderContext::new(frame, chunks[3], &theme),
        );

        // Command palette overlay (render last)
        if state.palette.is_visible() {
            CommandPalette::view(
                &state.palette,
                &mut RenderContext::new(frame, area, &theme).focused(true),
            );
        }
    }

    fn handle_event(_event: &Event) -> Option<Msg> {
        None
    }

    fn handle_event_with_state(state: &State, event: &Event) -> Option<Msg> {
        let key = event.as_key()?;
        let ctrl = key.modifiers.ctrl();

        // Command palette gets priority
        if state.palette.is_visible() {
            return CommandPalette::handle_event(&state.palette, event, &EventContext::default())
                .map(Msg::Palette);
        }

        // Global shortcuts
        match key.code {
            Key::Char('q') if ctrl => return Some(Msg::Quit),
            Key::Char('p') if ctrl => return Some(Msg::TogglePalette),
            Key::Char('n') if ctrl => return Some(Msg::NewTab),
            Key::Char('w') if ctrl => return Some(Msg::CloseTab),
            Key::Char('l') if ctrl => {
                state
                    .conversations
                    .get(state.active_tab)
                    .map(|_| Msg::Palette(CommandPaletteMessage::Confirm));
            }
            Key::Tab => return Some(Msg::FocusToggle),
            Key::Esc => return Some(Msg::Quit),
            _ => {}
        }

        // Input-focused: Ctrl+Enter submits, other keys go to TextArea
        if state.focus.is_focused(&Focus::Input) {
            if ctrl && key.code == Key::Enter {
                return Some(Msg::SubmitInput);
            }
            return TextArea::handle_event(&state.input, event, &EventContext::default())
                .map(Msg::Input);
        }

        // Conversation-focused: route to ConversationView
        if state.focus.is_focused(&Focus::Conversation) {
            return ConversationView::handle_event(
                &state.active_conv().view,
                event,
                &EventContext::default(),
            )
            .map(Msg::Conv);
        }

        // Fall through: route to TabBar for left/right tab switching
        TabBar::handle_event(&state.tab_bar, event, &EventContext::default()).map(Msg::TabBar)
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn update_status(state: &mut State) {
    state.status.set_center(vec![StatusBarItem::new(format!(
        "Messages: {} │ Tab: {}",
        state.message_count,
        state.active_tab + 1,
    ))]);
}

fn generate_response(msg_num: usize) -> String {
    let responses = [
        "Here's how to sort a vector in Rust:\n\n```rust\nlet mut v = vec![3, 1, 2];\nv.sort();\nprintln!(\"{:?}\", v); // [1, 2, 3]\n```\n\nYou can also use `sort_by` for custom ordering.",
        "The **ownership** system in Rust prevents data races at compile time. Key rules:\n\n1. Each value has exactly one owner\n2. When the owner goes out of scope, the value is dropped\n3. You can have either one `&mut` reference OR many `&` references",
        "To handle errors in Rust, use the `Result<T, E>` type:\n\n```rust\nfn read_file(path: &str) -> Result<String, std::io::Error> {\n    std::fs::read_to_string(path)\n}\n\nmatch read_file(\"data.txt\") {\n    Ok(content) => println!(\"{}\", content),\n    Err(e) => eprintln!(\"Error: {}\", e),\n}\n```",
        "For async operations, Rust uses `async/await` with the **tokio** runtime:\n\n```rust\n#[tokio::main]\nasync fn main() {\n    let response = reqwest::get(\"https://api.example.com\")\n        .await\n        .unwrap();\n    println!(\"Status: {}\", response.status());\n}\n```",
    ];
    responses[msg_num % responses.len()].to_string()
}

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

fn main() -> envision::Result<()> {
    let mut vt = Runtime::<ChatClient, _>::virtual_builder(90, 28).build()?;

    // Simulate a conversation using vt.dispatch() so that returned
    // Command::message() values are automatically processed on tick().
    TextArea::update(
        &mut vt.state_mut().input,
        TextAreaMessage::SetValue("How do I sort a vector in Rust?".to_string()),
    );
    vt.dispatch(Msg::SubmitInput);
    vt.tick()?; // Processes Command::message(SimulateResponse) from SubmitInput

    // Push another user message and response
    TextArea::update(
        &mut vt.state_mut().input,
        TextAreaMessage::SetValue("What about ownership?".to_string()),
    );
    vt.dispatch(Msg::SubmitInput);
    vt.tick()?;

    println!("Chat Client — Reference Application");
    println!("====================================");
    println!();
    println!("{}", vt.display());
    println!();
    println!("This demonstrates:");
    println!("  - ConversationView with markdown rendering");
    println!("  - MessageHandle for streaming identity");
    println!("  - TabBar with closable conversation tabs");
    println!("  - TextArea multi-line input");
    println!("  - CommandPalette for slash commands");
    println!("  - FocusManager + EventContext focus routing");
    println!("  - StatusBar with message count");

    Ok(())
}