revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! IDE Example - Demonstrates Screen System, Command Palette, SplitPane, StatusBar
//!
//! A mini IDE-like application showing Revue's advanced features working together.
//!
//! Run with: cargo run --example ide

use revue::prelude::*;
use revue::widget::{Command, CommandPalette, TextArea};

/// Main IDE application state
struct IdeApp {
    /// Command palette visibility
    command_palette_open: bool,
    /// Command palette state
    command_palette: CommandPalette,
    /// Current file name
    current_file: String,
    /// File tree items
    files: Vec<FileItem>,
    /// Selected file index
    selected_file: usize,
    /// Editor content
    editor: TextArea,
    /// Status messages
    status_message: String,
    /// Mode (Normal, Insert, Command)
    mode: EditorMode,
    /// Split ratio
    split_ratio: f32,
    /// Notifications
    notifications: Vec<String>,
}

#[derive(Clone, Copy, PartialEq)]
enum EditorMode {
    Normal,
    Insert,
    Command,
}

impl EditorMode {
    fn name(&self) -> &str {
        match self {
            EditorMode::Normal => "NORMAL",
            EditorMode::Insert => "INSERT",
            EditorMode::Command => "COMMAND",
        }
    }

    fn color(&self) -> Color {
        match self {
            EditorMode::Normal => Color::BLUE,
            EditorMode::Insert => Color::GREEN,
            EditorMode::Command => Color::YELLOW,
        }
    }
}

struct FileItem {
    name: String,
    is_dir: bool,
    modified: bool,
}

impl IdeApp {
    fn new() -> Self {
        let commands = vec![
            Command::new("file.new", "New File").shortcut("Ctrl+N"),
            Command::new("file.open", "Open File").shortcut("Ctrl+O"),
            Command::new("file.save", "Save File").shortcut("Ctrl+S"),
            Command::new("file.close", "Close File").shortcut("Ctrl+W"),
            Command::new("edit.undo", "Undo").shortcut("Ctrl+Z"),
            Command::new("edit.redo", "Redo").shortcut("Ctrl+Y"),
            Command::new("edit.find", "Find").shortcut("Ctrl+F"),
            Command::new("edit.replace", "Replace").shortcut("Ctrl+H"),
            Command::new("view.sidebar", "Toggle Sidebar").shortcut("Ctrl+B"),
            Command::new("view.terminal", "Toggle Terminal").shortcut("Ctrl+`"),
            Command::new("goto.line", "Go to Line").shortcut("Ctrl+G"),
            Command::new("goto.symbol", "Go to Symbol").shortcut("Ctrl+Shift+O"),
        ];

        let files = vec![
            FileItem {
                name: "src/".into(),
                is_dir: true,
                modified: false,
            },
            FileItem {
                name: "  main.rs".into(),
                is_dir: false,
                modified: true,
            },
            FileItem {
                name: "  lib.rs".into(),
                is_dir: false,
                modified: false,
            },
            FileItem {
                name: "  app/".into(),
                is_dir: true,
                modified: false,
            },
            FileItem {
                name: "    mod.rs".into(),
                is_dir: false,
                modified: false,
            },
            FileItem {
                name: "    screen.rs".into(),
                is_dir: false,
                modified: true,
            },
            FileItem {
                name: "Cargo.toml".into(),
                is_dir: false,
                modified: false,
            },
            FileItem {
                name: "README.md".into(),
                is_dir: false,
                modified: false,
            },
        ];

        let sample_code = r#"//! Revue - A Vue-style TUI framework for Rust
//!
//! This is a sample file showing syntax highlighting.

use revue::prelude::*;

fn main() -> Result<()> {
    let mut app = App::builder()
        .style("styles.css")
        .hot_reload(true)
        .build();

    let view = Text::new("Hello, Revue!")
        .fg(Color::CYAN)
        .bold();

    app.run(&view)
}

// TODO: Add more features
// FIXME: Handle edge cases
"#;

        let mut editor = TextArea::new();
        editor.set_content(sample_code);

        Self {
            command_palette_open: false,
            command_palette: CommandPalette::new().commands(commands),
            current_file: "main.rs".into(),
            files,
            selected_file: 1,
            editor,
            status_message: "Ready".into(),
            mode: EditorMode::Normal,
            split_ratio: 0.2,
            notifications: Vec::new(),
        }
    }

    fn handle_key(&mut self, key: &Key) -> bool {
        // Command palette handling
        if self.command_palette_open {
            match key {
                Key::Escape => {
                    self.command_palette_open = false;
                    self.command_palette.clear_query();
                    return true;
                }
                Key::Enter => {
                    if let Some(cmd) = self.command_palette.selected_command() {
                        self.execute_command(&cmd.id.clone());
                    }
                    self.command_palette_open = false;
                    self.command_palette.clear_query();
                    return true;
                }
                Key::Up => {
                    self.command_palette.select_prev();
                    return true;
                }
                Key::Down => {
                    self.command_palette.select_next();
                    return true;
                }
                Key::Char(c) => {
                    self.command_palette.input(*c);
                    return true;
                }
                Key::Backspace => {
                    self.command_palette.backspace();
                    return true;
                }
                _ => return false,
            }
        }

        // Mode-specific handling
        match self.mode {
            EditorMode::Normal => self.handle_normal_mode(key),
            EditorMode::Insert => self.handle_insert_mode(key),
            EditorMode::Command => self.handle_command_mode(key),
        }
    }

    fn handle_normal_mode(&mut self, key: &Key) -> bool {
        match key {
            Key::Char('i') => {
                self.mode = EditorMode::Insert;
                self.status_message = "-- INSERT --".into();
                true
            }
            Key::Char(':') => {
                self.mode = EditorMode::Command;
                self.status_message = ":".into();
                true
            }
            Key::Char('j') | Key::Down => {
                self.editor.move_down();
                true
            }
            Key::Char('k') | Key::Up => {
                self.editor.move_up();
                true
            }
            Key::Char('h') | Key::Left => {
                self.editor.move_left();
                true
            }
            Key::Char('l') | Key::Right => {
                self.editor.move_right();
                true
            }
            Key::Char('p') if key == &Key::Char('p') => {
                // Ctrl+P opens command palette
                self.command_palette_open = true;
                true
            }
            Key::Tab => {
                // Navigate file tree
                self.selected_file = (self.selected_file + 1) % self.files.len();
                true
            }
            _ => false,
        }
    }

    fn handle_insert_mode(&mut self, key: &Key) -> bool {
        match key {
            Key::Escape => {
                self.mode = EditorMode::Normal;
                self.status_message = "Ready".into();
                true
            }
            Key::Char(c) => {
                self.editor.insert_char(*c);
                true
            }
            Key::Enter => {
                self.editor.insert_char('\n');
                true
            }
            Key::Backspace => {
                self.editor.delete_char_before();
                true
            }
            Key::Delete => {
                self.editor.delete_char_at();
                true
            }
            Key::Up => {
                self.editor.move_up();
                true
            }
            Key::Down => {
                self.editor.move_down();
                true
            }
            Key::Left => {
                self.editor.move_left();
                true
            }
            Key::Right => {
                self.editor.move_right();
                true
            }
            _ => false,
        }
    }

    fn handle_command_mode(&mut self, key: &Key) -> bool {
        match key {
            Key::Escape => {
                self.mode = EditorMode::Normal;
                self.status_message = "Ready".into();
                true
            }
            Key::Enter => {
                let cmd = self.status_message.trim_start_matches(':').to_string();
                self.execute_vim_command(&cmd);
                self.mode = EditorMode::Normal;
                true
            }
            Key::Char(c) => {
                self.status_message.push(*c);
                true
            }
            Key::Backspace => {
                if self.status_message.len() > 1 {
                    self.status_message.pop();
                }
                true
            }
            _ => false,
        }
    }

    fn execute_command(&mut self, id: &str) {
        self.status_message = format!("Executed: {}", id);
        self.add_notification(format!("Command: {}", id));

        match id {
            "file.new" => self.status_message = "New file created".into(),
            "file.save" => self.status_message = format!("Saved: {}", self.current_file),
            "view.sidebar" => self.split_ratio = if self.split_ratio > 0.1 { 0.0 } else { 0.2 },
            _ => {}
        }
    }

    fn execute_vim_command(&mut self, cmd: &str) {
        match cmd {
            "w" => self.status_message = format!("\"{}\" written", self.current_file),
            "q" => self.status_message = "Use Ctrl+C to quit".into(),
            "wq" => self.status_message = "Saved and quit".into(),
            _ => self.status_message = format!("Unknown command: {}", cmd),
        }
    }

    fn add_notification(&mut self, msg: String) {
        self.notifications.push(msg);
        if self.notifications.len() > 3 {
            self.notifications.remove(0);
        }
    }

    fn render_file_tree(&self) -> impl View {
        let mut tree = vstack();

        for (i, file) in self.files.iter().enumerate() {
            let icon = if file.is_dir { "📁 " } else { "📄 " };
            let modified = if file.modified { " [+]" } else { "" };
            let name = format!("{}{}{}", icon, file.name, modified);

            let text = if i == self.selected_file {
                Text::new(name).fg(Color::CYAN).bold()
            } else if file.is_dir {
                Text::new(name).fg(Color::BLUE)
            } else if file.modified {
                Text::new(name).fg(Color::YELLOW)
            } else {
                Text::new(name)
            };

            tree = tree.child(text);
        }

        Border::rounded().title("Explorer").child(tree)
    }

    fn render_editor(&self) -> impl View {
        // Create line numbers
        let text = self.editor.get_content();
        let lines = text.lines().count().max(1);
        let line_width = lines.to_string().len();

        let mut content = vstack();
        for (i, line) in text.lines().enumerate() {
            let line_num = format!("{:>width$} ", i + 1, width = line_width);
            let row = hstack()
                .child(Text::new(line_num).fg(Color::rgb(100, 100, 100)))
                .child(Text::new(line));
            content = content.child(row);
        }

        let (cursor_row, cursor_col) = self.editor.cursor_position();
        let cursor_info = format!("Ln {}, Col {}", cursor_row + 1, cursor_col + 1);

        let header = hstack()
            .child(Text::new(format!(" {} ", self.current_file)).bg(Color::rgb(50, 50, 50)))
            .child(if self.files[self.selected_file].modified {
                Text::new(" [Modified]").fg(Color::YELLOW)
            } else {
                Text::new("")
            });

        vstack()
            .child(header)
            .child(Border::single().child(content))
            .child(Text::new(cursor_info).fg(Color::rgb(128, 128, 128)))
    }

    fn render_status_bar(&self) -> impl View {
        let mode_text = Text::new(format!(" {} ", self.mode.name()))
            .fg(Color::BLACK)
            .bg(self.mode.color())
            .bold();

        let file_text = Text::new(format!("  {} ", self.current_file));
        let status_text =
            Text::new(format!("  {} ", self.status_message)).fg(Color::rgb(180, 180, 180));

        let (cursor_row, cursor_col) = self.editor.cursor_position();
        let pos_text = Text::new(format!(" {}:{} ", cursor_row + 1, cursor_col + 1));

        hstack()
            .child(mode_text)
            .child(file_text)
            .child(status_text)
            .child(pos_text)
    }

    fn render_command_palette(&self) -> impl View {
        if !self.command_palette_open {
            return vstack(); // Empty
        }

        // Use the CommandPalette's built-in rendering
        // Just show a simple overlay since CommandPalette has its own render
        let search_box = Border::rounded().title("Command Palette").child(
            vstack()
                .child(Text::new(format!("> {}", self.command_palette.get_query())))
                .child(Text::new("".repeat(40)).fg(Color::rgb(80, 80, 80)))
                .child(
                    Text::new("(Use ↑↓ to select, Enter to execute)").fg(Color::rgb(100, 100, 100)),
                ),
        );

        vstack().child(search_box)
    }
}

impl View for IdeApp {
    fn render(&self, ctx: &mut RenderContext) {
        // Main layout: sidebar + editor
        let sidebar = self.render_file_tree();
        let editor = self.render_editor();

        // Create split pane
        let main_content = if self.split_ratio > 0.05 {
            hstack().child(sidebar).child(editor)
        } else {
            hstack().child(editor)
        };

        // Header
        let header = hstack()
            .child(Text::new(" Revue IDE ").fg(Color::CYAN).bold())
            .child(
                Text::new(" | Ctrl+P: Commands | Tab: Files | i: Insert | :: Command ")
                    .fg(Color::rgb(100, 100, 100)),
            );

        // Main view
        let main_view = vstack()
            .child(header)
            .child(main_content)
            .child(self.render_status_bar());

        main_view.render(ctx);

        // Overlay command palette
        if self.command_palette_open {
            self.render_command_palette().render(ctx);
        }

        // Notifications (bottom right)
        if !self.notifications.is_empty() {
            let mut notif_stack = vstack();
            for msg in &self.notifications {
                notif_stack = notif_stack.child(
                    Text::new(format!(" {} ", msg))
                        .fg(Color::WHITE)
                        .bg(Color::rgb(60, 60, 60)),
                );
            }
            notif_stack.render(ctx);
        }
    }
}

fn main() -> Result<()> {
    let mut app = App::builder().build();
    let ide = IdeApp::new();

    app.run_with_handler(ide, |key_event, ide| ide.handle_key(&key_event.key))
}