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
//! Stepper widget for multi-step processes
//!
//! Shows progress through a series of steps with status indicators.

use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::widget::theme::{DISABLED_FG, SEPARATOR_COLOR, SUBTLE_GRAY};
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Step status
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum StepStatus {
    /// Step not started
    #[default]
    Pending,
    /// Step in progress
    Active,
    /// Step completed
    Completed,
    /// Step has error
    Error,
    /// Step skipped
    Skipped,
}

impl StepStatus {
    fn icon(&self) -> char {
        match self {
            StepStatus::Pending => '',
            StepStatus::Active => '',
            StepStatus::Completed => '',
            StepStatus::Error => '',
            StepStatus::Skipped => '',
        }
    }
}

/// Step definition
#[derive(Clone, Debug)]
pub struct Step {
    /// Step title
    pub title: String,
    /// Step description
    pub description: Option<String>,
    /// Step status
    pub status: StepStatus,
    /// Custom icon
    pub icon: Option<char>,
}

impl Step {
    /// Create a new step
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            description: None,
            status: StepStatus::Pending,
            icon: None,
        }
    }

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

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

    /// Set custom icon
    pub fn icon(mut self, icon: char) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Mark as completed
    pub fn complete(mut self) -> Self {
        self.status = StepStatus::Completed;
        self
    }

    /// Mark as active
    pub fn active(mut self) -> Self {
        self.status = StepStatus::Active;
        self
    }

    /// Get display icon
    fn display_icon(&self) -> char {
        self.icon.unwrap_or_else(|| self.status.icon())
    }
}

/// Stepper orientation
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum StepperOrientation {
    /// Horizontal steps
    #[default]
    Horizontal,
    /// Vertical steps
    Vertical,
}

/// Stepper style
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum StepperStyle {
    /// Simple dots
    #[default]
    Dots,
    /// Numbered steps
    Numbered,
    /// With connector lines
    Connected,
    /// Progress bar style
    Progress,
}

/// Stepper widget
#[derive(Clone, Debug)]
pub struct Stepper {
    /// Steps
    steps: Vec<Step>,
    /// Current step index
    current: usize,
    /// Orientation
    orientation: StepperOrientation,
    /// Style
    style: StepperStyle,
    /// Show descriptions
    show_descriptions: bool,
    /// Active color
    active_color: Color,
    /// Completed color
    completed_color: Color,
    /// Pending color
    pending_color: Color,
    /// Error color
    error_color: Color,
    /// Connector color
    connector_color: Color,
    /// Show step numbers
    show_numbers: bool,
    /// Widget properties
    props: WidgetProps,
}

impl Stepper {
    /// Create a new stepper
    pub fn new() -> Self {
        Self {
            steps: Vec::new(),
            current: 0,
            orientation: StepperOrientation::Horizontal,
            style: StepperStyle::Connected,
            show_descriptions: true,
            active_color: Color::CYAN,
            completed_color: Color::GREEN,
            pending_color: DISABLED_FG,
            error_color: Color::RED,
            connector_color: SEPARATOR_COLOR,
            show_numbers: true,
            props: WidgetProps::new(),
        }
    }

    /// Add a step
    pub fn step(mut self, step: Step) -> Self {
        self.steps.push(step);
        self
    }

    /// Add step from string
    pub fn add_step(mut self, title: impl Into<String>) -> Self {
        self.steps.push(Step::new(title));
        self
    }

    /// Set all steps
    pub fn steps(mut self, steps: Vec<Step>) -> Self {
        self.steps = steps;
        self
    }

    /// Set current step
    pub fn current(mut self, index: usize) -> Self {
        self.current = index.min(self.steps.len().saturating_sub(1));
        self.update_statuses();
        self
    }

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

    /// Set horizontal orientation
    pub fn horizontal(mut self) -> Self {
        self.orientation = StepperOrientation::Horizontal;
        self
    }

    /// Set vertical orientation
    pub fn vertical(mut self) -> Self {
        self.orientation = StepperOrientation::Vertical;
        self
    }

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

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

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

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

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

    /// Update step statuses based on current index
    fn update_statuses(&mut self) {
        for (i, step) in self.steps.iter_mut().enumerate() {
            if step.status != StepStatus::Error && step.status != StepStatus::Skipped {
                step.status = if i < self.current {
                    StepStatus::Completed
                } else if i == self.current {
                    StepStatus::Active
                } else {
                    StepStatus::Pending
                };
            }
        }
    }

    /// Go to next step
    pub fn next_step(&mut self) -> bool {
        if self.current < self.steps.len().saturating_sub(1) {
            self.current += 1;
            self.update_statuses();
            true
        } else {
            false
        }
    }

    /// Go to previous step
    pub fn prev(&mut self) -> bool {
        if self.current > 0 {
            self.current -= 1;
            self.update_statuses();
            true
        } else {
            false
        }
    }

    /// Go to specific step
    pub fn go_to(&mut self, index: usize) {
        if index < self.steps.len() {
            self.current = index;
            self.update_statuses();
        }
    }

    /// Complete current step and advance
    pub fn complete_current(&mut self) {
        if let Some(step) = self.steps.get_mut(self.current) {
            step.status = StepStatus::Completed;
        }
        self.next_step();
    }

    /// Mark step as error
    pub fn mark_error(&mut self, index: usize) {
        if let Some(step) = self.steps.get_mut(index) {
            step.status = StepStatus::Error;
        }
    }

    /// Mark step as skipped
    pub fn skip(&mut self, index: usize) {
        if let Some(step) = self.steps.get_mut(index) {
            step.status = StepStatus::Skipped;
        }
    }

    /// Get current step
    pub fn current_step(&self) -> Option<&Step> {
        self.steps.get(self.current)
    }

    /// Get step count
    pub fn len(&self) -> usize {
        self.steps.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }

    /// Check if completed (on last step and it's completed)
    pub fn is_completed(&self) -> bool {
        self.steps
            .last()
            .is_some_and(|s| s.status == StepStatus::Completed)
    }

    /// Get progress as percentage
    pub fn progress(&self) -> f64 {
        if self.steps.is_empty() {
            return 0.0;
        }
        let completed = self
            .steps
            .iter()
            .filter(|s| s.status == StepStatus::Completed)
            .count();
        completed as f64 / self.steps.len() as f64
    }

    /// Get color for step
    fn step_color(&self, step: &Step) -> Color {
        match step.status {
            StepStatus::Active => self.active_color,
            StepStatus::Completed => self.completed_color,
            StepStatus::Error => self.error_color,
            StepStatus::Pending | StepStatus::Skipped => self.pending_color,
        }
    }
}

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

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

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        if area.width < 3 || area.height < 1 || self.steps.is_empty() {
            return;
        }

        match self.orientation {
            StepperOrientation::Horizontal => self.render_horizontal(ctx),
            StepperOrientation::Vertical => self.render_vertical(ctx),
        }
    }
}

impl Stepper {
    fn render_horizontal(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let step_count = self.steps.len();
        let available_width = area.width as usize;

        // Calculate spacing
        let step_width = available_width / step_count.max(1);

        let y: u16 = 0;

        for (i, step) in self.steps.iter().enumerate() {
            let x = (i * step_width) as u16;
            let color = self.step_color(step);

            // Step indicator
            match self.style {
                StepperStyle::Numbered => {
                    let num = format!("{}", i + 1);
                    for (j, ch) in num.chars().enumerate() {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(color);
                        if step.status == StepStatus::Active {
                            cell.modifier |= Modifier::BOLD;
                        }
                        ctx.set(x + j as u16, y, cell);
                    }
                }
                _ => {
                    let mut cell = Cell::new(step.display_icon());
                    cell.fg = Some(color);
                    if step.status == StepStatus::Active {
                        cell.modifier |= Modifier::BOLD;
                    }
                    ctx.set(x, y, cell);
                }
            }

            // Connector (except last)
            if matches!(self.style, StepperStyle::Connected | StepperStyle::Progress)
                && i < step_count - 1
            {
                let connector_start = x + 2;
                let connector_end = ((i + 1) * step_width) as u16;

                for cx in connector_start..connector_end {
                    let ch = if matches!(self.style, StepperStyle::Progress)
                        && step.status == StepStatus::Completed
                    {
                        ''
                    } else {
                        ''
                    };
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(if step.status == StepStatus::Completed {
                        self.completed_color
                    } else {
                        self.connector_color
                    });
                    ctx.set(cx, y, cell);
                }
            }

            // Title (below indicator)
            if y + 1 < area.height {
                let max_title_len = step_width.saturating_sub(1);
                let title = if step.title.len() > max_title_len {
                    format!("{}", &step.title[..max_title_len.saturating_sub(1)])
                } else {
                    step.title.clone()
                };

                for (j, ch) in title.chars().enumerate() {
                    if x + j as u16 >= area.width {
                        break;
                    }
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(color);
                    if step.status == StepStatus::Active {
                        cell.modifier |= Modifier::BOLD;
                    }
                    ctx.set(x + j as u16, y + 1, cell);
                }
            }

            // Description (if enabled and space available)
            if self.show_descriptions && y + 2 < area.height {
                if let Some(ref desc) = step.description {
                    let max_desc_len = step_width.saturating_sub(1);
                    let desc_str = if desc.len() > max_desc_len {
                        format!("{}", &desc[..max_desc_len.saturating_sub(1)])
                    } else {
                        desc.clone()
                    };

                    for (j, ch) in desc_str.chars().enumerate() {
                        if x + j as u16 >= area.width {
                            break;
                        }
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(SUBTLE_GRAY);
                        ctx.set(x + j as u16, y + 2, cell);
                    }
                }
            }
        }
    }

    fn render_vertical(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let mut y: u16 = 0;

        for (i, step) in self.steps.iter().enumerate() {
            if y >= area.height {
                break;
            }

            let color = self.step_color(step);
            let x: u16 = 0;

            // Step indicator
            let indicator = if self.show_numbers {
                format!("{}", i + 1)
            } else {
                step.display_icon().to_string()
            };

            for (j, ch) in indicator.chars().enumerate() {
                let mut cell = Cell::new(ch);
                cell.fg = Some(color);
                if step.status == StepStatus::Active {
                    cell.modifier |= Modifier::BOLD;
                }
                ctx.set(x + j as u16, y, cell);
            }

            // Title
            let title_x = x + 3;
            for (j, ch) in step.title.chars().enumerate() {
                if title_x + j as u16 >= area.width {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(color);
                if step.status == StepStatus::Active {
                    cell.modifier |= Modifier::BOLD;
                }
                ctx.set(title_x + j as u16, y, cell);
            }

            y += 1;

            // Description
            if self.show_descriptions {
                if let Some(ref desc) = step.description {
                    if y < area.height {
                        let desc_x = x + 3;
                        for (j, ch) in desc.chars().enumerate() {
                            if desc_x + j as u16 >= area.width {
                                break;
                            }
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(SUBTLE_GRAY);
                            ctx.set(desc_x + j as u16, y, cell);
                        }
                        y += 1;
                    }
                }
            }

            // Connector (except last)
            if matches!(self.style, StepperStyle::Connected)
                && i < self.steps.len() - 1
                && y < area.height
            {
                let mut cell = Cell::new('');
                cell.fg = Some(if step.status == StepStatus::Completed {
                    self.completed_color
                } else {
                    self.connector_color
                });
                ctx.set(x, y, cell);
                y += 1;
            }
        }
    }
}

impl_styled_view!(Stepper);
impl_props_builders!(Stepper);

/// Helper to create a stepper
pub fn stepper() -> Stepper {
    Stepper::new()
}

/// Helper to create a step
pub fn step(title: impl Into<String>) -> Step {
    Step::new(title)
}