envision 0.16.0

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
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
//! A pipeline/workflow visualization component showing step-by-step progress.
//!
//! [`StepIndicator`] displays a series of steps with their current status,
//! connected by configurable connectors. Supports both horizontal and vertical
//! orientations. State is stored in [`StepIndicatorState`], updated via
//! [`StepIndicatorMessage`], and produces [`StepIndicatorOutput`]. Steps are
//! defined with [`Step`] and have a [`StepStatus`].
//!
//!
//! # Example
//!
//! ```rust
//! use envision::component::{
//!     StepIndicator, StepIndicatorMessage, StepIndicatorOutput,
//!     StepIndicatorState, Component,
//!     step_indicator::{Step, StepStatus},
//! };
//!
//! let steps = vec![
//!     Step::new("Build").with_status(StepStatus::Completed),
//!     Step::new("Test").with_status(StepStatus::Active),
//!     Step::new("Deploy").with_status(StepStatus::Pending),
//! ];
//! let mut state = StepIndicatorState::new(steps);
//!
//! // Complete the active step and activate the next
//! StepIndicator::update(&mut state, StepIndicatorMessage::CompleteActive);
//! StepIndicator::update(&mut state, StepIndicatorMessage::ActivateNext);
//! ```

mod state;

use std::collections::HashMap;

use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Paragraph};

use super::{Component, EventContext, RenderContext};
use crate::input::{Event, Key};
use crate::theme::Theme;

/// The status of a single step in a workflow.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum StepStatus {
    /// Step has not started yet.
    Pending,
    /// Step is currently in progress.
    Active,
    /// Step completed successfully.
    Completed,
    /// Step failed.
    Failed,
    /// Step was skipped.
    Skipped,
}

impl StepStatus {
    fn icon(&self) -> &'static str {
        match self {
            StepStatus::Pending => "",
            StepStatus::Active => "",
            StepStatus::Completed => "",
            StepStatus::Failed => "",
            StepStatus::Skipped => "",
        }
    }
}

/// The orientation of the step indicator layout.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum StepOrientation {
    /// Steps are displayed left to right.
    Horizontal,
    /// Steps are displayed top to bottom.
    Vertical,
}

/// A single step in a workflow.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct Step {
    label: String,
    status: StepStatus,
    description: Option<String>,
}

impl Step {
    /// Creates a new pending step.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    ///
    /// let step = Step::new("Build");
    /// assert_eq!(step.label(), "Build");
    /// ```
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            status: StepStatus::Pending,
            description: None,
        }
    }

    /// Sets the step status (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    ///
    /// let step = Step::new("Build").with_status(StepStatus::Completed);
    /// assert_eq!(step.status(), &StepStatus::Completed);
    /// ```
    pub fn with_status(mut self, status: StepStatus) -> Self {
        self.status = status;
        self
    }

    /// Sets the step description (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    ///
    /// let step = Step::new("Test").with_description("Run unit tests");
    /// assert_eq!(step.description(), Some("Run unit tests"));
    /// ```
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Returns the step label.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    ///
    /// let step = Step::new("Build");
    /// assert_eq!(step.label(), "Build");
    /// ```
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Returns the step status.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    ///
    /// let step = Step::new("Test");
    /// assert_eq!(step.status(), &StepStatus::Pending);
    /// ```
    pub fn status(&self) -> &StepStatus {
        &self.status
    }

    /// Returns the step description, if any.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    ///
    /// let step = Step::new("Deploy");
    /// assert_eq!(step.description(), None);
    /// ```
    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }
}

/// Messages that can be sent to a StepIndicator.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StepIndicatorMessage {
    /// Set the status of a specific step.
    SetStatus {
        /// The step index.
        index: usize,
        /// The new status.
        status: StepStatus,
    },
    /// Activate the next pending step after the current active step.
    ActivateNext,
    /// Complete the currently active step.
    CompleteActive,
    /// Fail the currently active step.
    FailActive,
    /// Skip a specific step.
    Skip(usize),
    /// Reset all steps to pending.
    Reset,
    /// Move keyboard focus to the next step.
    FocusNext,
    /// Move keyboard focus to the previous step.
    FocusPrev,
    /// Select the currently focused step.
    Select,
    /// Jump focus to the first step.
    First,
    /// Jump focus to the last step.
    Last,
}

/// Output messages from a StepIndicator.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StepIndicatorOutput {
    /// A step's status changed.
    StatusChanged {
        /// The step index.
        index: usize,
        /// The new status.
        status: StepStatus,
    },
    /// All steps are completed.
    AllCompleted,
    /// A step was selected via keyboard.
    Selected(usize),
    /// Focus moved to a different step.
    FocusChanged(usize),
    /// All steps were reset.
    Reset,
}

/// State for a StepIndicator component.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct StepIndicatorState {
    steps: Vec<Step>,
    orientation: StepOrientation,
    focused_index: usize,
    show_descriptions: bool,
    title: Option<String>,
    connector: String,
    show_border: bool,
    /// Per-status style overrides. When set, these take precedence over
    /// the default theme-based styles.
    #[cfg_attr(feature = "serialization", serde(skip, default))]
    status_style_overrides: HashMap<StepStatus, Style>,
    /// Per-step-index style overrides. When set, these take precedence
    /// over both status-based overrides and the default theme styles.
    /// Use this to give specific steps (e.g., "intake", "review") a
    /// fixed color regardless of their current status.
    #[cfg_attr(feature = "serialization", serde(skip, default))]
    step_style_overrides: HashMap<usize, Style>,
}

impl Default for StepIndicatorState {
    fn default() -> Self {
        Self {
            steps: Vec::new(),
            orientation: StepOrientation::Horizontal,
            focused_index: 0,
            show_descriptions: false,
            title: None,
            connector: "───".to_string(),
            show_border: true,
            status_style_overrides: HashMap::new(),
            step_style_overrides: HashMap::new(),
        }
    }
}

/// A pipeline/workflow visualization component.
///
/// `StepIndicator` displays a series of steps connected by configurable
/// connectors, with status icons and color-coded styling.
///
/// # Visual Format (Horizontal)
///
/// ```text
/// ✓ Build ─── ● Test ─── ○ Deploy
/// ```
///
/// # Visual Format (Vertical)
///
/// ```text
/// ✓ Build
////// ● Test
////// ○ Deploy
/// ```
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     StepIndicator, StepIndicatorMessage, StepIndicatorState, Component,
///     step_indicator::{Step, StepStatus},
/// };
///
/// let steps = vec![
///     Step::new("Build").with_status(StepStatus::Completed),
///     Step::new("Test").with_status(StepStatus::Active),
///     Step::new("Deploy"),
/// ];
/// let mut state = StepIndicatorState::new(steps);
/// StepIndicator::update(&mut state, StepIndicatorMessage::CompleteActive);
/// ```
pub struct StepIndicator;

impl Component for StepIndicator {
    type State = StepIndicatorState;
    type Message = StepIndicatorMessage;
    type Output = StepIndicatorOutput;

    fn init() -> Self::State {
        StepIndicatorState::default()
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            StepIndicatorMessage::SetStatus { index, status } => {
                if let Some(step) = state.steps.get_mut(index) {
                    step.status = status.clone();
                    let output = StepIndicatorOutput::StatusChanged { index, status };
                    if state.is_all_completed() {
                        return Some(StepIndicatorOutput::AllCompleted);
                    }
                    return Some(output);
                }
                None
            }
            StepIndicatorMessage::ActivateNext => {
                // Find the first pending step after the last completed/active
                let next = state
                    .steps
                    .iter()
                    .position(|s| s.status == StepStatus::Pending);
                if let Some(index) = next {
                    state.steps[index].status = StepStatus::Active;
                    Some(StepIndicatorOutput::StatusChanged {
                        index,
                        status: StepStatus::Active,
                    })
                } else {
                    None
                }
            }
            StepIndicatorMessage::CompleteActive => {
                if let Some(index) = state.active_step_index() {
                    state.steps[index].status = StepStatus::Completed;
                    if state.is_all_completed() {
                        return Some(StepIndicatorOutput::AllCompleted);
                    }
                    Some(StepIndicatorOutput::StatusChanged {
                        index,
                        status: StepStatus::Completed,
                    })
                } else {
                    None
                }
            }
            StepIndicatorMessage::FailActive => {
                if let Some(index) = state.active_step_index() {
                    state.steps[index].status = StepStatus::Failed;
                    Some(StepIndicatorOutput::StatusChanged {
                        index,
                        status: StepStatus::Failed,
                    })
                } else {
                    None
                }
            }
            StepIndicatorMessage::Skip(index) => {
                if let Some(step) = state.steps.get_mut(index) {
                    step.status = StepStatus::Skipped;
                    if state.is_all_completed() {
                        return Some(StepIndicatorOutput::AllCompleted);
                    }
                    Some(StepIndicatorOutput::StatusChanged {
                        index,
                        status: StepStatus::Skipped,
                    })
                } else {
                    None
                }
            }
            StepIndicatorMessage::Reset => {
                for step in &mut state.steps {
                    step.status = StepStatus::Pending;
                }
                state.focused_index = 0;
                Some(StepIndicatorOutput::Reset)
            }
            StepIndicatorMessage::FocusNext => {
                if state.steps.is_empty() {
                    return None;
                }
                state.focused_index = (state.focused_index + 1) % state.steps.len();
                Some(StepIndicatorOutput::FocusChanged(state.focused_index))
            }
            StepIndicatorMessage::FocusPrev => {
                if state.steps.is_empty() {
                    return None;
                }
                state.focused_index = state
                    .focused_index
                    .checked_sub(1)
                    .unwrap_or(state.steps.len() - 1);
                Some(StepIndicatorOutput::FocusChanged(state.focused_index))
            }
            StepIndicatorMessage::Select => {
                if state.steps.is_empty() {
                    return None;
                }
                Some(StepIndicatorOutput::Selected(state.focused_index))
            }
            StepIndicatorMessage::First => {
                if state.steps.is_empty() {
                    return None;
                }
                state.focused_index = 0;
                Some(StepIndicatorOutput::FocusChanged(0))
            }
            StepIndicatorMessage::Last => {
                if state.steps.is_empty() {
                    return None;
                }
                state.focused_index = state.steps.len() - 1;
                Some(StepIndicatorOutput::FocusChanged(state.focused_index))
            }
        }
    }

    fn handle_event(
        _state: &Self::State,
        event: &Event,
        ctx: &EventContext,
    ) -> Option<Self::Message> {
        if !ctx.focused || ctx.disabled {
            return None;
        }
        if let Some(key) = event.as_key() {
            match key.code {
                Key::Left | Key::Char('h') => Some(StepIndicatorMessage::FocusPrev),
                Key::Right | Key::Char('l') => Some(StepIndicatorMessage::FocusNext),
                Key::Home => Some(StepIndicatorMessage::First),
                Key::End => Some(StepIndicatorMessage::Last),
                Key::Enter => Some(StepIndicatorMessage::Select),
                _ => None,
            }
        } else {
            None
        }
    }

    fn view(state: &Self::State, ctx: &mut RenderContext<'_, '_>) {
        crate::annotation::with_registry(|reg| {
            reg.register(
                ctx.area,
                crate::annotation::Annotation::new(crate::annotation::WidgetType::StepIndicator)
                    .with_id("step_indicator")
                    .with_focus(ctx.focused)
                    .with_disabled(ctx.disabled),
            );
        });

        let inner = if state.show_border {
            let mut block = Block::default()
                .borders(Borders::ALL)
                .border_style(if ctx.focused {
                    ctx.theme.focused_border_style()
                } else {
                    ctx.theme.border_style()
                });
            if let Some(title) = &state.title {
                block = block.title(format!(" {} ", title));
            }
            let inner = block.inner(ctx.area);
            ctx.frame.render_widget(block, ctx.area);
            inner
        } else {
            ctx.area
        };

        if state.steps.is_empty() {
            return;
        }

        match state.orientation {
            StepOrientation::Horizontal => {
                render_horizontal(state, ctx.frame, inner, ctx.theme, ctx.focused);
            }
            StepOrientation::Vertical => {
                render_vertical(state, ctx.frame, inner, ctx.theme, ctx.focused);
            }
        }
    }
}

fn step_style(
    index: usize,
    status: &StepStatus,
    is_focused_step: bool,
    theme: &Theme,
    step_overrides: &HashMap<usize, Style>,
    status_overrides: &HashMap<StepStatus, Style>,
) -> Style {
    // Priority: per-step-index > per-status > theme default
    let base = step_overrides
        .get(&index)
        .or_else(|| status_overrides.get(status))
        .copied()
        .unwrap_or_else(|| match status {
            StepStatus::Completed => theme.success_style(),
            StepStatus::Active => theme.info_style(),
            StepStatus::Failed => theme.error_style(),
            StepStatus::Skipped => theme.disabled_style(),
            StepStatus::Pending => theme.normal_style(),
        });
    if is_focused_step {
        base.add_modifier(Modifier::BOLD | Modifier::UNDERLINED)
    } else {
        base
    }
}

fn render_horizontal(
    state: &StepIndicatorState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
    focused: bool,
) {
    let mut spans = Vec::new();

    for (i, step) in state.steps.iter().enumerate() {
        if i > 0 {
            spans.push(Span::styled(
                format!(" {} ", state.connector),
                theme.normal_style(),
            ));
        }

        let is_focused_step = focused && i == state.focused_index;
        let style = step_style(
            i,
            &step.status,
            is_focused_step,
            theme,
            &state.step_style_overrides,
            &state.status_style_overrides,
        );

        spans.push(Span::styled(
            format!("{} {}", step.status.icon(), step.label),
            style,
        ));
    }

    let line = Line::from(spans);
    let paragraph = Paragraph::new(line);
    frame.render_widget(paragraph, area);
}

fn render_vertical(
    state: &StepIndicatorState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
    focused: bool,
) {
    let mut lines = Vec::new();

    for (i, step) in state.steps.iter().enumerate() {
        if i > 0 {
            lines.push(Line::from(Span::styled("", theme.normal_style())));
        }

        let is_focused_step = focused && i == state.focused_index;
        let style = step_style(
            i,
            &step.status,
            is_focused_step,
            theme,
            &state.step_style_overrides,
            &state.status_style_overrides,
        );

        lines.push(Line::from(Span::styled(
            format!("{} {}", step.status.icon(), step.label),
            style,
        )));

        if state.show_descriptions {
            if let Some(desc) = &step.description {
                lines.push(Line::from(Span::styled(
                    format!("  {}", desc),
                    theme.normal_style(),
                )));
            }
        }
    }

    let paragraph = Paragraph::new(lines);
    frame.render_widget(paragraph, area);
}

#[cfg(test)]
mod tests;