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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Option list widget
//!
//! A flexible list for displaying options with rich formatting, grouping,
//! separators, and keyboard navigation. Unlike SelectionList which is for
//! multi-select, OptionList is for single selection with enhanced display.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::{OptionList, Option, option_list};
//!
//! // Simple option list
//! let list = OptionList::new()
//!     .option("Open File", "Ctrl+O")
//!     .option("Save File", "Ctrl+S")
//!     .separator()
//!     .option("Exit", "Ctrl+Q");
//!
//! // With groups
//! let menu = option_list()
//!     .group("File")
//!     .option("New", "")
//!     .option("Open", "")
//!     .group("Edit")
//!     .option("Undo", "")
//!     .option("Redo", "");
//! ```

use crate::style::Color;
use crate::widget::theme::{DARK_GRAY, MUTED_TEXT, PLACEHOLDER_FG};
use crate::widget::traits::DISABLED_FG;
use crate::widget::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Option list entry type
#[derive(Clone, Debug)]
pub enum OptionEntry {
    /// Regular option
    Option(OptionItem),
    /// Separator line
    Separator,
    /// Group header
    Group(String),
}

/// Single option item
#[derive(Clone, Debug)]
pub struct OptionItem {
    /// Display text
    pub text: String,
    /// Optional secondary text (right-aligned)
    pub hint: Option<String>,
    /// Optional value/id
    pub value: Option<String>,
    /// Whether option is disabled
    pub disabled: bool,
    /// Optional icon/prefix
    pub icon: Option<String>,
    /// Optional description
    pub description: Option<String>,
}

impl OptionItem {
    /// Create a new option
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            hint: None,
            value: None,
            disabled: false,
            icon: None,
            description: None,
        }
    }

    /// Set hint text
    pub fn hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(hint.into());
        self
    }

    /// Set value
    pub fn value(mut self, value: impl Into<String>) -> Self {
        self.value = Some(value.into());
        self
    }

    /// Set disabled
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set icon
    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set description
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }
}

/// Separator style
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SeparatorStyle {
    /// Single line ─
    #[default]
    Line,
    /// Dashed ╌
    Dashed,
    /// Double ═
    Double,
    /// Blank line
    Blank,
}

/// Option list widget
#[derive(Clone, Debug)]
pub struct OptionList {
    /// Entries (options, separators, groups)
    entries: Vec<OptionEntry>,
    /// Highlighted index (option index, not entry index)
    highlighted: usize,
    /// Selected option index
    selected: Option<usize>,
    /// Separator style
    separator_style: SeparatorStyle,
    /// Title
    title: Option<String>,
    /// Width
    width: Option<u16>,
    /// Show descriptions
    show_descriptions: bool,
    /// Foreground color
    fg: Option<Color>,
    /// Highlighted color
    highlighted_fg: Option<Color>,
    /// Selected color
    selected_fg: Option<Color>,
    /// Disabled color
    disabled_fg: Option<Color>,
    /// Background color
    bg: Option<Color>,
    /// Highlighted background
    highlighted_bg: Option<Color>,
    /// Max visible items
    max_visible: usize,
    /// Scroll offset
    scroll_offset: usize,
    /// Whether list is focused
    focused: bool,
    /// Show icons
    show_icons: bool,
    /// Widget properties
    props: WidgetProps,
}

impl OptionList {
    /// Create a new option list
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            highlighted: 0,
            selected: None,
            separator_style: SeparatorStyle::default(),
            title: None,
            width: None,
            show_descriptions: false,
            fg: None,
            highlighted_fg: None,
            selected_fg: None,
            disabled_fg: None,
            bg: None,
            highlighted_bg: None,
            max_visible: 10,
            scroll_offset: 0,
            focused: false,
            show_icons: true,
            props: WidgetProps::new(),
        }
    }

    /// Add an option
    pub fn option(mut self, text: impl Into<String>, hint: impl Into<String>) -> Self {
        let hint_str = hint.into();
        let mut item = OptionItem::new(text);
        if !hint_str.is_empty() {
            item.hint = Some(hint_str);
        }
        self.entries.push(OptionEntry::Option(item));
        self
    }

    /// Add an option with full configuration
    pub fn add_option(mut self, option: OptionItem) -> Self {
        self.entries.push(OptionEntry::Option(option));
        self
    }

    /// Add a separator
    pub fn separator(mut self) -> Self {
        self.entries.push(OptionEntry::Separator);
        self
    }

    /// Add a group header
    pub fn group(mut self, name: impl Into<String>) -> Self {
        self.entries.push(OptionEntry::Group(name.into()));
        self
    }

    /// Set separator style
    pub fn separator_style(mut self, style: SeparatorStyle) -> Self {
        self.separator_style = style;
        self
    }

    /// Set title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set width
    pub fn width(mut self, width: u16) -> Self {
        self.width = Some(width);
        self
    }

    /// Show descriptions
    pub fn show_descriptions(mut self, show: bool) -> Self {
        self.show_descriptions = show;
        self
    }

    /// Set foreground color
    pub fn fg(mut self, color: Color) -> Self {
        self.fg = Some(color);
        self
    }

    /// Set highlighted foreground color
    pub fn highlighted_fg(mut self, color: Color) -> Self {
        self.highlighted_fg = Some(color);
        self
    }

    /// Set selected foreground color
    pub fn selected_fg(mut self, color: Color) -> Self {
        self.selected_fg = Some(color);
        self
    }

    /// Set disabled foreground color
    pub fn disabled_fg(mut self, color: Color) -> Self {
        self.disabled_fg = Some(color);
        self
    }

    /// Set background color
    pub fn bg(mut self, color: Color) -> Self {
        self.bg = Some(color);
        self
    }

    /// Set highlighted background color
    pub fn highlighted_bg(mut self, color: Color) -> Self {
        self.highlighted_bg = Some(color);
        self
    }

    /// Set max visible items
    pub fn max_visible(mut self, max: usize) -> Self {
        self.max_visible = max;
        self
    }

    /// Set focused state
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Show/hide icons
    pub fn show_icons(mut self, show: bool) -> Self {
        self.show_icons = show;
        self
    }

    /// Get option count (excluding separators and groups)
    pub fn option_count(&self) -> usize {
        self.entries
            .iter()
            .filter(|e| matches!(e, OptionEntry::Option(_)))
            .count()
    }

    /// Get highlighted option
    pub fn get_highlighted(&self) -> Option<&OptionItem> {
        let mut option_idx = 0;
        for entry in &self.entries {
            if let OptionEntry::Option(item) = entry {
                if option_idx == self.highlighted {
                    return Some(item);
                }
                option_idx += 1;
            }
        }
        None
    }

    /// Get selected option
    pub fn get_selected(&self) -> Option<&OptionItem> {
        let selected = self.selected?;
        let mut option_idx = 0;
        for entry in &self.entries {
            if let OptionEntry::Option(item) = entry {
                if option_idx == selected {
                    return Some(item);
                }
                option_idx += 1;
            }
        }
        None
    }

    /// Get selected value
    pub fn get_selected_value(&self) -> Option<&str> {
        self.get_selected()
            .and_then(|item| item.value.as_deref().or(Some(&item.text)))
    }

    /// Select highlighted option
    pub fn select_highlighted(&mut self) -> bool {
        if let Some(item) = self.get_highlighted() {
            if !item.disabled {
                self.selected = Some(self.highlighted);
                return true;
            }
        }
        false
    }

    /// Select by index
    pub fn select(&mut self, index: usize) {
        let mut option_idx = 0;
        for entry in &self.entries {
            if let OptionEntry::Option(item) = entry {
                if option_idx == index && !item.disabled {
                    self.selected = Some(index);
                    self.highlighted = index;
                    return;
                }
                option_idx += 1;
            }
        }
    }

    /// Clear selection
    pub fn clear_selection(&mut self) {
        self.selected = None;
    }

    /// Move highlight to previous option
    pub fn highlight_previous(&mut self) {
        if self.highlighted > 0 {
            self.highlighted -= 1;

            // Skip disabled items
            while self.highlighted > 0 {
                if let Some(item) = self.get_highlighted() {
                    if !item.disabled {
                        break;
                    }
                }
                self.highlighted -= 1;
            }

            self.ensure_visible();
        }
    }

    /// Move highlight to next option
    pub fn highlight_next(&mut self) {
        let max = self.option_count().saturating_sub(1);
        if self.highlighted < max {
            self.highlighted += 1;

            // Skip disabled items
            while self.highlighted < max {
                if let Some(item) = self.get_highlighted() {
                    if !item.disabled {
                        break;
                    }
                }
                self.highlighted += 1;
            }

            self.ensure_visible();
        }
    }

    /// Move to first option
    pub fn highlight_first(&mut self) {
        self.highlighted = 0;

        // Skip disabled items
        while self.highlighted < self.option_count() - 1 {
            if let Some(item) = self.get_highlighted() {
                if !item.disabled {
                    break;
                }
            }
            self.highlighted += 1;
        }

        self.scroll_offset = 0;
    }

    /// Move to last option
    pub fn highlight_last(&mut self) {
        self.highlighted = self.option_count().saturating_sub(1);

        // Skip disabled items
        while self.highlighted > 0 {
            if let Some(item) = self.get_highlighted() {
                if !item.disabled {
                    break;
                }
            }
            self.highlighted -= 1;
        }

        self.ensure_visible();
    }

    /// Ensure highlighted item is visible
    fn ensure_visible(&mut self) {
        if self.highlighted < self.scroll_offset {
            self.scroll_offset = self.highlighted;
        } else if self.highlighted >= self.scroll_offset + self.max_visible {
            self.scroll_offset = self.highlighted - self.max_visible + 1;
        }
    }

    /// Get separator character
    fn separator_char(&self) -> &str {
        match self.separator_style {
            SeparatorStyle::Line => "",
            SeparatorStyle::Dashed => "",
            SeparatorStyle::Double => "",
            SeparatorStyle::Blank => " ",
        }
    }
}

// ============================================================================
// Test-only getters (doc(hidden))
// ============================================================================

impl OptionList {
    /// Get entries (test-only)
    #[doc(hidden)]
    pub fn __test_entries(&self) -> &Vec<OptionEntry> {
        &self.entries
    }

    /// Get highlighted index (test-only)
    #[doc(hidden)]
    pub fn __test_highlighted(&self) -> usize {
        self.highlighted
    }

    /// Get selected index (test-only)
    #[doc(hidden)]
    pub fn __test_selected(&self) -> Option<usize> {
        self.selected
    }

    /// Get separator style (test-only)
    #[doc(hidden)]
    pub fn __test_separator_style(&self) -> SeparatorStyle {
        self.separator_style
    }

    /// Get title (test-only)
    #[doc(hidden)]
    pub fn __test_title(&self) -> &Option<String> {
        &self.title
    }

    /// Get width (test-only)
    #[doc(hidden)]
    pub fn __test_width(&self) -> &Option<u16> {
        &self.width
    }

    /// Get show_descriptions (test-only)
    #[doc(hidden)]
    pub fn __test_show_descriptions(&self) -> bool {
        self.show_descriptions
    }

    /// Get fg (test-only)
    #[doc(hidden)]
    pub fn __test_fg(&self) -> &Option<Color> {
        &self.fg
    }

    /// Get highlighted_fg (test-only)
    #[doc(hidden)]
    pub fn __test_highlighted_fg(&self) -> &Option<Color> {
        &self.highlighted_fg
    }

    /// Get selected_fg (test-only)
    #[doc(hidden)]
    pub fn __test_selected_fg(&self) -> &Option<Color> {
        &self.selected_fg
    }

    /// Get disabled_fg (test-only)
    #[doc(hidden)]
    pub fn __test_disabled_fg(&self) -> &Option<Color> {
        &self.disabled_fg
    }

    /// Get bg (test-only)
    #[doc(hidden)]
    pub fn __test_bg(&self) -> &Option<Color> {
        &self.bg
    }

    /// Get highlighted_bg (test-only)
    #[doc(hidden)]
    pub fn __test_highlighted_bg(&self) -> &Option<Color> {
        &self.highlighted_bg
    }

    /// Get max_visible (test-only)
    #[doc(hidden)]
    pub fn __test_max_visible(&self) -> usize {
        self.max_visible
    }

    /// Get scroll_offset (test-only)
    #[doc(hidden)]
    pub fn __test_scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Get focused (test-only)
    #[doc(hidden)]
    pub fn __test_focused(&self) -> bool {
        self.focused
    }

    /// Get show_icons (test-only)
    #[doc(hidden)]
    pub fn __test_show_icons(&self) -> bool {
        self.show_icons
    }

    /// Get separator character (test-only)
    #[doc(hidden)]
    pub fn __test_separator_char(&self) -> &str {
        match self.separator_style {
            SeparatorStyle::Line => "",
            SeparatorStyle::Dashed => "",
            SeparatorStyle::Double => "",
            SeparatorStyle::Blank => " ",
        }
    }
}

impl Default for OptionList {
    fn default() -> Self {
        Self::new()
    }
}

impl View for OptionList {
    crate::impl_view_meta!("OptionList");

    fn render(&self, ctx: &mut RenderContext) {
        use crate::widget::stack::vstack;
        use crate::widget::Text;

        let width = self.width.unwrap_or(40) as usize;

        let mut content = vstack();

        // Title
        if let Some(title) = &self.title {
            content = content.child(Text::new(title).bold());
        }

        let mut option_idx = 0;
        let mut visible_count = 0;
        let mut skipped = 0;

        for entry in &self.entries {
            // Handle scrolling
            if let OptionEntry::Option(_) = entry {
                if option_idx < self.scroll_offset {
                    option_idx += 1;
                    skipped += 1;
                    continue;
                }
                if visible_count >= self.max_visible {
                    break;
                }
            }

            match entry {
                OptionEntry::Option(item) => {
                    let is_highlighted = option_idx == self.highlighted && self.focused;
                    let is_selected = self.selected == Some(option_idx);

                    // Build option text
                    let icon = if self.show_icons {
                        item.icon.as_deref().unwrap_or("")
                    } else {
                        ""
                    };

                    let prefix = if is_selected {
                        ""
                    } else if is_highlighted {
                        "> "
                    } else {
                        "  "
                    };

                    let main_text = format!("{}{}{}", prefix, icon, item.text);

                    // Calculate padding for hint
                    let hint = item.hint.as_deref().unwrap_or("");
                    let padding = width.saturating_sub(main_text.len() + hint.len());

                    // Determine colors
                    let fg = if item.disabled {
                        self.disabled_fg.unwrap_or(DISABLED_FG)
                    } else if is_highlighted {
                        self.highlighted_fg.unwrap_or(Color::CYAN)
                    } else if is_selected {
                        self.selected_fg.unwrap_or(Color::GREEN)
                    } else {
                        self.fg.unwrap_or(Color::WHITE)
                    };

                    let bg = if is_highlighted {
                        self.highlighted_bg
                    } else {
                        self.bg
                    };

                    // Build row
                    let mut text =
                        Text::new(format!("{}{}{}", main_text, " ".repeat(padding), hint)).fg(fg);

                    if let Some(bg) = bg {
                        text = text.bg(bg);
                    }

                    if is_highlighted || is_selected {
                        text = text.bold();
                    }

                    content = content.child(text);

                    // Show description
                    if self.show_descriptions {
                        if let Some(desc) = &item.description {
                            content = content
                                .child(Text::new(format!("    {}", desc)).fg(PLACEHOLDER_FG));
                        }
                    }

                    option_idx += 1;
                    visible_count += 1;
                }
                OptionEntry::Separator => {
                    let line = self.separator_char().repeat(width);
                    content = content.child(Text::new(line).fg(DARK_GRAY));
                }
                OptionEntry::Group(name) => {
                    content = content.child(Text::new(name).fg(MUTED_TEXT).bold());
                }
            }
        }

        // Scroll indicators
        if skipped > 0 {
            content = vstack()
                .child(Text::new("").fg(DISABLED_FG))
                .child(content);
        }

        let remaining = self.option_count() - (self.scroll_offset + visible_count);
        if remaining > 0 {
            content = content.child(Text::new("").fg(DISABLED_FG));
        }

        content.render(ctx);
    }
}

impl_styled_view!(OptionList);
impl_props_builders!(OptionList);

/// Create an option list
pub fn option_list() -> OptionList {
    OptionList::new()
}

/// Create an option item
pub fn option_item(text: impl Into<String>) -> OptionItem {
    OptionItem::new(text)
}