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
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
//! New Widgets Showcase - Demonstrates Digits, Link, MaskedInput, SelectionList, OptionList
//!
//! Run with: cargo run --example new_widgets

use revue::prelude::*;
use revue::widget::{
    DigitStyle, Digits, Link, LinkStyle, MaskedInput, OptionItem, OptionList, SelectionItem,
    SelectionList, SelectionStyle,
};

/// Current demo tab
#[derive(Clone, Copy, PartialEq)]
enum DemoTab {
    Digits,
    Links,
    MaskedInput,
    SelectionList,
    OptionList,
}

impl DemoTab {
    fn name(&self) -> &str {
        match self {
            DemoTab::Digits => "Digits",
            DemoTab::Links => "Links",
            DemoTab::MaskedInput => "MaskedInput",
            DemoTab::SelectionList => "SelectionList",
            DemoTab::OptionList => "OptionList",
        }
    }

    fn all() -> &'static [DemoTab] {
        &[
            DemoTab::Digits,
            DemoTab::Links,
            DemoTab::MaskedInput,
            DemoTab::SelectionList,
            DemoTab::OptionList,
        ]
    }
}

/// Demo application state
struct NewWidgetsDemo {
    /// Current tab
    tab: DemoTab,
    /// Timer seconds
    timer_secs: u64,
    /// Digit style index
    digit_style: usize,
    /// Password input
    password: MaskedInput,
    /// PIN input
    pin: MaskedInput,
    /// Selection list
    features: SelectionList,
    /// Option list
    menu: OptionList,
    /// Active input (for MaskedInput tab)
    active_input: usize,
    /// Frame counter for animations
    frame: usize,
}

impl NewWidgetsDemo {
    fn new() -> Self {
        let password = MaskedInput::password()
            .placeholder("Enter password...")
            .label("Password")
            .show_strength(true)
            .allow_reveal(true)
            .min_length(8)
            .width(30);

        let pin = MaskedInput::pin(6)
            .placeholder("______")
            .label("PIN Code")
            .width(20);

        let features = SelectionList::new(vec![
            SelectionItem::new("Dark Mode").description("Enable dark theme"),
            SelectionItem::new("Notifications").description("Push notifications"),
            SelectionItem::new("Auto-save").description("Save every 5 minutes"),
            SelectionItem::new("Spell Check").description("Check spelling as you type"),
            SelectionItem::new("Line Numbers").description("Show line numbers in editor"),
            SelectionItem::new("Word Wrap").description("Wrap long lines"),
            SelectionItem::new("Minimap").description("Show code minimap"),
            SelectionItem::new("Breadcrumbs").description("Show file path breadcrumbs"),
        ])
        .title("Editor Features")
        .show_descriptions(true)
        .show_count(true)
        .max_selections(5)
        .style(SelectionStyle::Checkbox)
        .selected(vec![0, 2, 4])
        .focused(true);

        let menu = OptionList::new()
            .title("Actions")
            .group("File")
            .add_option(OptionItem::new("New File").hint("Ctrl+N").icon("📄 "))
            .add_option(OptionItem::new("Open File").hint("Ctrl+O").icon("📂 "))
            .add_option(OptionItem::new("Save").hint("Ctrl+S").icon("💾 "))
            .add_option(
                OptionItem::new("Save As...")
                    .hint("Ctrl+Shift+S")
                    .icon("📝 "),
            )
            .separator()
            .group("Edit")
            .add_option(OptionItem::new("Undo").hint("Ctrl+Z").icon("↩️ "))
            .add_option(OptionItem::new("Redo").hint("Ctrl+Y").icon("↪️ "))
            .add_option(OptionItem::new("Cut").hint("Ctrl+X").icon("✂️ "))
            .add_option(OptionItem::new("Copy").hint("Ctrl+C").icon("📋 "))
            .add_option(OptionItem::new("Paste").hint("Ctrl+V").icon("📌 "))
            .separator()
            .group("View")
            .add_option(OptionItem::new("Zoom In").hint("Ctrl++"))
            .add_option(OptionItem::new("Zoom Out").hint("Ctrl+-"))
            .add_option(OptionItem::new("Full Screen").hint("F11").disabled(true))
            .max_visible(12)
            .show_icons(true)
            .focused(true);

        Self {
            tab: DemoTab::Digits,
            timer_secs: 3661, // 1:01:01
            digit_style: 0,
            password,
            pin,
            features,
            menu,
            active_input: 0,
            frame: 0,
        }
    }

    fn handle_key(&mut self, key: &Key) -> bool {
        // Tab switching
        match key {
            Key::Char('1') => {
                self.tab = DemoTab::Digits;
                return true;
            }
            Key::Char('2') => {
                self.tab = DemoTab::Links;
                return true;
            }
            Key::Char('3') => {
                self.tab = DemoTab::MaskedInput;
                return true;
            }
            Key::Char('4') => {
                self.tab = DemoTab::SelectionList;
                return true;
            }
            Key::Char('5') => {
                self.tab = DemoTab::OptionList;
                return true;
            }
            Key::Tab => {
                let tabs = DemoTab::all();
                let idx = tabs.iter().position(|&t| t == self.tab).unwrap_or(0);
                self.tab = tabs[(idx + 1) % tabs.len()];
                return true;
            }
            Key::BackTab => {
                let tabs = DemoTab::all();
                let idx = tabs.iter().position(|&t| t == self.tab).unwrap_or(0);
                self.tab = tabs[(idx + tabs.len() - 1) % tabs.len()];
                return true;
            }
            _ => {}
        }

        // Tab-specific handling
        match self.tab {
            DemoTab::Digits => self.handle_digits_key(key),
            DemoTab::Links => false, // Links are display-only
            DemoTab::MaskedInput => self.handle_masked_input_key(key),
            DemoTab::SelectionList => self.handle_selection_list_key(key),
            DemoTab::OptionList => self.handle_option_list_key(key),
        }
    }

    fn handle_digits_key(&mut self, key: &Key) -> bool {
        match key {
            Key::Up | Key::Char('+') => {
                self.timer_secs += 1;
                true
            }
            Key::Down | Key::Char('-') => {
                self.timer_secs = self.timer_secs.saturating_sub(1);
                true
            }
            Key::Left => {
                self.timer_secs = self.timer_secs.saturating_sub(60);
                true
            }
            Key::Right => {
                self.timer_secs += 60;
                true
            }
            Key::Char('s') => {
                self.digit_style = (self.digit_style + 1) % 4;
                true
            }
            Key::Char('r') => {
                self.timer_secs = 0;
                true
            }
            _ => false,
        }
    }

    fn handle_masked_input_key(&mut self, key: &Key) -> bool {
        // Switch between inputs
        if matches!(key, Key::Down | Key::Up) {
            self.active_input = 1 - self.active_input;
            return true;
        }

        let input = if self.active_input == 0 {
            &mut self.password
        } else {
            &mut self.pin
        };

        match key {
            Key::Backspace => {
                input.delete_backward();
                true
            }
            Key::Delete => {
                input.delete_forward();
                true
            }
            Key::Left => {
                input.move_left();
                true
            }
            Key::Right => {
                input.move_right();
                true
            }
            Key::Home => {
                input.move_start();
                true
            }
            Key::End => {
                input.move_end();
                true
            }
            Key::Enter => {
                input.validate();
                true
            }
            Key::Char(c) => {
                // 'r' toggles reveal for password input
                if *c == 'r' && self.active_input == 0 {
                    self.password.toggle_reveal();
                } else {
                    input.insert_char(*c);
                }
                true
            }
            _ => false,
        }
    }

    fn handle_selection_list_key(&mut self, key: &Key) -> bool {
        match key {
            Key::Up | Key::Char('k') => {
                self.features.highlight_previous();
                true
            }
            Key::Down | Key::Char('j') => {
                self.features.highlight_next();
                true
            }
            Key::Char(' ') | Key::Enter => {
                self.features.toggle_highlighted();
                true
            }
            Key::Char('a') => {
                self.features.select_all();
                true
            }
            Key::Char('n') => {
                self.features.deselect_all();
                true
            }
            Key::Home | Key::Char('g') => {
                self.features.highlight_first();
                true
            }
            Key::End | Key::Char('G') => {
                self.features.highlight_last();
                true
            }
            _ => false,
        }
    }

    fn handle_option_list_key(&mut self, key: &Key) -> bool {
        match key {
            Key::Up | Key::Char('k') => {
                self.menu.highlight_previous();
                true
            }
            Key::Down | Key::Char('j') => {
                self.menu.highlight_next();
                true
            }
            Key::Enter | Key::Char(' ') => {
                self.menu.select_highlighted();
                true
            }
            Key::Home | Key::Char('g') => {
                self.menu.highlight_first();
                true
            }
            Key::End | Key::Char('G') => {
                self.menu.highlight_last();
                true
            }
            Key::Escape => {
                self.menu.clear_selection();
                true
            }
            _ => false,
        }
    }

    fn render_tabs(&self) -> impl View {
        let mut tabs = hstack().gap(2);

        for (i, tab) in DemoTab::all().iter().enumerate() {
            let label = format!("[{}] {}", i + 1, tab.name());
            let text = if *tab == self.tab {
                Text::new(label).fg(Color::CYAN).bold()
            } else {
                Text::new(label).fg(Color::rgb(128, 128, 128))
            };
            tabs = tabs.child(text);
        }

        tabs
    }

    fn render_digits_demo(&self) -> impl View {
        let styles = [
            DigitStyle::Block,
            DigitStyle::Thin,
            DigitStyle::Ascii,
            DigitStyle::Braille,
        ];
        let style = styles[self.digit_style];
        let style_name = match style {
            DigitStyle::Block => "Block",
            DigitStyle::Thin => "Thin",
            DigitStyle::Ascii => "ASCII",
            DigitStyle::Braille => "Braille",
        };

        let timer_display = Digits::timer(self.timer_secs).style(style).fg(Color::CYAN);

        let counter = Digits::new(self.frame)
            .style(style)
            .fg(Color::GREEN)
            .min_width(4);

        let price = Digits::from_float(1234.56, 2)
            .style(style)
            .fg(Color::YELLOW)
            .separator(',');

        vstack()
            .gap(1)
            .child(Text::new(format!("Style: {} (press 's' to change)", style_name)).bold())
            .child(Text::new(""))
            .child(Text::new("Timer (↑↓: +/-1s, ←→: +/-1m, r: reset):"))
            .child(timer_display)
            .child(Text::new(""))
            .child(Text::new("Frame Counter:"))
            .child(counter)
            .child(Text::new(""))
            .child(Text::new("Price Display:"))
            .child(price)
    }

    fn render_links_demo(&self) -> impl View {
        let link1 = Link::new("https://github.com/anthropics/claude-code")
            .text("Claude Code on GitHub")
            .style(LinkStyle::Underline)
            .fg(Color::CYAN);

        let link2 = Link::new("https://docs.rs")
            .text("Rust Documentation")
            .style(LinkStyle::Bracketed)
            .fg(Color::GREEN);

        let link3 = Link::new("https://crates.io")
            .text("Crates.io")
            .style(LinkStyle::Arrow)
            .fg(Color::YELLOW);

        let link4 = Link::new("https://rust-lang.org")
            .text("Rust Language")
            .style(LinkStyle::Icon)
            .fg(Color::MAGENTA);

        let link5 = Link::new("https://example.com/disabled")
            .text("Disabled Link")
            .disabled(true);

        vstack()
            .gap(1)
            .child(Text::new("Link Styles:").bold())
            .child(Text::new(""))
            .child(hstack().child(Text::new("Underline: ")).child(link1))
            .child(hstack().child(Text::new("Bracketed: ")).child(link2))
            .child(hstack().child(Text::new("Arrow:     ")).child(link3))
            .child(hstack().child(Text::new("Icon:      ")).child(link4))
            .child(hstack().child(Text::new("Disabled:  ")).child(link5))
            .child(Text::new(""))
            .child(
                Text::new("Links support OSC 8 hyperlinks in compatible terminals.")
                    .fg(Color::rgb(100, 100, 100)),
            )
            .child(
                Text::new("Click or Ctrl+Click to open in browser.").fg(Color::rgb(100, 100, 100)),
            )
    }

    fn render_masked_input_demo(&self) -> impl View {
        let pwd_focused = self.active_input == 0;
        let pin_focused = self.active_input == 1;

        let password = self.password.clone().focused(pwd_focused);
        let pin = self.pin.clone().focused(pin_focused);

        vstack()
            .gap(1)
            .child(Text::new("Masked Input Fields:").bold())
            .child(Text::new(
                "(↑↓: switch fields, type to enter, 'r': toggle reveal)",
            ))
            .child(Text::new(""))
            .child(if pwd_focused {
                Text::new("> Password").fg(Color::CYAN)
            } else {
                Text::new("  Password")
            })
            .child(password)
            .child(Text::new(""))
            .child(if pin_focused {
                Text::new("> PIN").fg(Color::CYAN)
            } else {
                Text::new("  PIN")
            })
            .child(pin)
            .child(Text::new(""))
            .child(Text::new("Features:").bold())
            .child(Text::new("  - Password strength indicator"))
            .child(Text::new("  - Reveal toggle (press 'r')"))
            .child(Text::new("  - Validation on Enter"))
            .child(Text::new("  - Show last 4 digits for credit cards"))
    }

    fn render_selection_list_demo(&self) -> impl View {
        let selected_count = self.features.get_selected().len();
        let selected_items: Vec<&str> = self
            .features
            .get_selected_items()
            .iter()
            .map(|item| item.text.as_str())
            .collect();

        vstack()
            .gap(1)
            .child(Text::new("Multi-Selection List:").bold())
            .child(Text::new("(↑↓: navigate, Space: toggle, a: all, n: none)"))
            .child(Text::new(""))
            .child(self.features.clone())
            .child(Text::new(""))
            .child(
                Text::new(format!(
                    "Selected ({}): {}",
                    selected_count,
                    selected_items.join(", ")
                ))
                .fg(Color::GREEN),
            )
    }

    fn render_option_list_demo(&self) -> impl View {
        let selected = self
            .menu
            .get_selected()
            .map(|item| item.text.as_str())
            .unwrap_or("None");

        vstack()
            .gap(1)
            .child(Text::new("Option List (Menu):").bold())
            .child(Text::new("(↑↓: navigate, Enter: select, Esc: clear)"))
            .child(Text::new(""))
            .child(self.menu.clone())
            .child(Text::new(""))
            .child(Text::new(format!("Selected: {}", selected)).fg(Color::GREEN))
    }

    fn update(&mut self) {
        self.frame += 1;
        self.password.update();
        self.pin.update();
    }
}

impl View for NewWidgetsDemo {
    fn render(&self, ctx: &mut RenderContext) {
        let header = hstack()
            .child(Text::new(" New Widgets Demo ").fg(Color::CYAN).bold())
            .child(Text::new(" | Tab/Shift+Tab or 1-5 to switch").fg(Color::rgb(100, 100, 100)));

        let tabs = self.render_tabs();

        let content = match self.tab {
            DemoTab::Digits => Border::rounded()
                .title("Digits Widget")
                .child(self.render_digits_demo()),
            DemoTab::Links => Border::rounded()
                .title("Link Widget")
                .child(self.render_links_demo()),
            DemoTab::MaskedInput => Border::rounded()
                .title("MaskedInput Widget")
                .child(self.render_masked_input_demo()),
            DemoTab::SelectionList => Border::rounded()
                .title("SelectionList Widget")
                .child(self.render_selection_list_demo()),
            DemoTab::OptionList => Border::rounded()
                .title("OptionList Widget")
                .child(self.render_option_list_demo()),
        };

        let help =
            Text::new("Press 'q' to quit | Tab: next | Shift+Tab: prev").fg(Color::rgb(80, 80, 80));

        vstack()
            .child(header)
            .child(tabs)
            .child(Text::new(""))
            .child(content)
            .child(Text::new(""))
            .child(help)
            .render(ctx);
    }
}

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

    app.run_with_handler(demo, |key_event, demo| {
        demo.update();
        demo.handle_key(&key_event.key)
    })
}