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
//! StepIndicatorState constructors, builders, accessors, setters, and instance methods.
//!
//! Extracted from the main module to keep file sizes manageable.

use ratatui::style::Style;

use super::{
    Step, StepIndicator, StepIndicatorMessage, StepIndicatorOutput, StepIndicatorState,
    StepOrientation, StepStatus,
};
use crate::component::Component;

impl StepIndicatorState {
    /// Creates a new step indicator with the given steps.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    ///
    /// let steps = vec![
    ///     Step::new("Step 1").with_status(StepStatus::Completed),
    ///     Step::new("Step 2").with_status(StepStatus::Active),
    ///     Step::new("Step 3"),
    /// ];
    /// let state = StepIndicatorState::new(steps);
    /// assert_eq!(state.steps().len(), 3);
    /// ```
    pub fn new(steps: Vec<Step>) -> Self {
        Self {
            steps,
            ..Self::default()
        }
    }

    /// Sets the orientation (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepOrientation};
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_orientation(StepOrientation::Vertical);
    /// assert_eq!(state.orientation(), &StepOrientation::Vertical);
    /// ```
    pub fn with_orientation(mut self, orientation: StepOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Sets the title (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_title("Pipeline");
    /// assert_eq!(state.title(), Some("Pipeline"));
    /// ```
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Sets the connector string (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_connector("-->");
    /// assert_eq!(state.connector(), "-->");
    /// ```
    pub fn with_connector(mut self, connector: impl Into<String>) -> Self {
        self.connector = connector.into();
        self
    }

    /// Sets whether descriptions are shown (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_show_descriptions(true);
    /// assert!(state.show_descriptions());
    /// ```
    pub fn with_show_descriptions(mut self, show: bool) -> Self {
        self.show_descriptions = show;
        self
    }

    /// Sets whether the border is shown (builder pattern).
    ///
    /// Defaults to `true`. When set to `false`, the `StepIndicator` renders
    /// its steps directly into the full widget area with no surrounding
    /// box — useful for inline breadcrumbs and single-row layouts.
    ///
    /// # Title interaction
    ///
    /// When the border is hidden, the state's [`title`](Self::title) is
    /// **not rendered**. The title is drawn as part of the border block,
    /// so disabling the border silently suppresses it. If you want this
    /// to be explicit, set the title to `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_show_border(false);
    /// assert!(!state.show_border());
    /// ```
    pub fn with_show_border(mut self, show: bool) -> Self {
        self.show_border = show;
        self
    }

    /// Sets a style override for a specific step status (builder pattern).
    ///
    /// When set, this style is used instead of the default theme-based
    /// style for steps with the given status.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("Build")])
    ///     .with_status_style(StepStatus::Completed, Style::default().fg(Color::Cyan))
    ///     .with_status_style(StepStatus::Failed, Style::default().fg(Color::Red));
    /// assert!(state.status_style_override(&StepStatus::Completed).is_some());
    /// ```
    pub fn with_status_style(mut self, status: StepStatus, style: Style) -> Self {
        self.status_style_overrides.insert(status, style);
        self
    }

    /// Sets a style override for a specific step by index (builder pattern).
    ///
    /// When set, this style is used for the step at the given index
    /// regardless of its current status. Per-index overrides take
    /// precedence over per-status overrides.
    ///
    /// Use this to give specific steps a fixed color (e.g., "intake"
    /// is always Cyan, "review" is always Yellow) regardless of whether
    /// they are pending, active, or completed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let state = StepIndicatorState::new(vec![
    ///     Step::new("Intake"),
    ///     Step::new("Review"),
    ///     Step::new("Approve"),
    /// ])
    /// .with_step_style(0, Style::default().fg(Color::Cyan))
    /// .with_step_style(1, Style::default().fg(Color::Yellow));
    /// assert!(state.step_style_override(0).is_some());
    /// ```
    pub fn with_step_style(mut self, index: usize, style: Style) -> Self {
        self.step_style_overrides.insert(index, style);
        self
    }

    /// Returns the steps.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("Build"), Step::new("Test")]);
    /// assert_eq!(state.steps().len(), 2);
    /// ```
    pub fn steps(&self) -> &[Step] {
        &self.steps
    }

    /// Returns a specific step, if it exists.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("Build"), Step::new("Test")]);
    /// assert_eq!(state.step(0).unwrap().label(), "Build");
    /// assert!(state.step(99).is_none());
    /// ```
    pub fn step(&self, index: usize) -> Option<&Step> {
        self.steps.get(index)
    }

    /// Returns the orientation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepOrientation};
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")]);
    /// assert_eq!(state.orientation(), &StepOrientation::Horizontal);
    /// ```
    pub fn orientation(&self) -> &StepOrientation {
        &self.orientation
    }

    /// Returns the focused step index.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A"), Step::new("B")]);
    /// assert_eq!(state.focused_index(), 0);
    /// ```
    pub fn focused_index(&self) -> usize {
        self.focused_index
    }

    /// Returns the index of the currently active step, if any.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![
    ///     Step::new("Build").with_status(StepStatus::Completed),
    ///     Step::new("Test").with_status(StepStatus::Active),
    ///     Step::new("Deploy"),
    /// ]);
    /// assert_eq!(state.active_step_index(), Some(1));
    /// ```
    pub fn active_step_index(&self) -> Option<usize> {
        self.steps
            .iter()
            .position(|s| s.status == StepStatus::Active)
    }

    /// Returns true if all steps are completed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![
    ///     Step::new("Build").with_status(StepStatus::Completed),
    ///     Step::new("Test").with_status(StepStatus::Completed),
    /// ]);
    /// assert!(state.is_all_completed());
    /// ```
    pub fn is_all_completed(&self) -> bool {
        !self.steps.is_empty()
            && self
                .steps
                .iter()
                .all(|s| s.status == StepStatus::Completed || s.status == StepStatus::Skipped)
    }

    /// Returns the connector string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")]).with_connector("→");
    /// assert_eq!(state.connector(), "→");
    /// ```
    pub fn connector(&self) -> &str {
        &self.connector
    }

    /// Returns the title, if any.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")]);
    /// assert_eq!(state.title(), None);
    /// ```
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// Sets the title.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StepIndicatorState;
    /// use envision::component::step_indicator::Step;
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("Step 1")]);
    /// state.set_title("Progress");
    /// assert_eq!(state.title(), Some("Progress"));
    /// ```
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = Some(title.into());
    }

    /// Returns whether descriptions are shown.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::default();
    /// assert!(!state.show_descriptions());
    /// ```
    pub fn show_descriptions(&self) -> bool {
        self.show_descriptions
    }

    /// Returns whether the border is shown.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StepIndicatorState;
    ///
    /// let state = StepIndicatorState::default();
    /// assert!(state.show_border());
    /// ```
    pub fn show_border(&self) -> bool {
        self.show_border
    }

    /// Sets whether descriptions are shown.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StepIndicatorState;
    /// use envision::component::step_indicator::Step;
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A"), Step::new("B")]);
    /// state.set_show_descriptions(true);
    /// assert!(state.show_descriptions());
    /// ```
    pub fn set_show_descriptions(&mut self, show: bool) {
        self.show_descriptions = show;
    }

    /// Sets the orientation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StepIndicatorState;
    /// use envision::component::step_indicator::{Step, StepOrientation};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A"), Step::new("B")]);
    /// state.set_orientation(StepOrientation::Vertical);
    /// assert_eq!(state.orientation(), &StepOrientation::Vertical);
    /// ```
    pub fn set_orientation(&mut self, orientation: StepOrientation) {
        self.orientation = orientation;
    }

    /// Sets whether the border is shown.
    ///
    /// See [`with_show_border`](Self::with_show_border) for the title
    /// interaction when `show` is `false`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StepIndicatorState;
    /// use envision::component::step_indicator::Step;
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A")]);
    /// state.set_show_border(false);
    /// assert!(!state.show_border());
    /// ```
    pub fn set_show_border(&mut self, show: bool) {
        self.show_border = show;
    }

    /// Returns the per-status style override, if one is set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_status_style(StepStatus::Active, Style::default().fg(Color::Yellow));
    /// assert!(state.status_style_override(&StepStatus::Active).is_some());
    /// assert!(state.status_style_override(&StepStatus::Pending).is_none());
    /// ```
    pub fn status_style_override(&self, status: &StepStatus) -> Option<&Style> {
        self.status_style_overrides.get(status)
    }

    /// Returns the per-status style override, if one is set.
    ///
    /// This is an alias for [`status_style_override`](Self::status_style_override).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A")]);
    /// state.set_status_style(StepStatus::Active, Style::default().fg(Color::Yellow));
    /// assert_eq!(
    ///     state.status_style(&StepStatus::Active),
    ///     Some(&Style::default().fg(Color::Yellow)),
    /// );
    /// assert!(state.status_style(&StepStatus::Pending).is_none());
    /// ```
    pub fn status_style(&self, status: &StepStatus) -> Option<&Style> {
        self.status_style_override(status)
    }

    /// Sets a per-status style override.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A")]);
    /// state.set_status_style(StepStatus::Active, Style::default().fg(Color::Yellow));
    /// assert!(state.status_style_override(&StepStatus::Active).is_some());
    /// ```
    pub fn set_status_style(&mut self, status: StepStatus, style: Style) {
        self.status_style_overrides.insert(status, style);
    }

    /// Removes a per-status style override.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::{Step, StepStatus};
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_status_style(StepStatus::Active, Style::default().fg(Color::Yellow));
    /// state.clear_status_style(&StepStatus::Active);
    /// assert!(state.status_style_override(&StepStatus::Active).is_none());
    /// ```
    pub fn clear_status_style(&mut self, status: &StepStatus) {
        self.status_style_overrides.remove(status);
    }

    /// Returns the per-step-index style override, if one is set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let state = StepIndicatorState::new(vec![Step::new("A"), Step::new("B")])
    ///     .with_step_style(0, Style::default().fg(Color::Cyan));
    /// assert!(state.step_style_override(0).is_some());
    /// assert!(state.step_style_override(1).is_none());
    /// ```
    pub fn step_style_override(&self, index: usize) -> Option<&Style> {
        self.step_style_overrides.get(&index)
    }

    /// Returns the per-step-index style override, if one is set.
    ///
    /// This is an alias for [`step_style_override`](Self::step_style_override).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A"), Step::new("B")]);
    /// state.set_step_style(0, Style::default().fg(Color::Cyan));
    /// assert_eq!(
    ///     state.step_style(0),
    ///     Some(&Style::default().fg(Color::Cyan)),
    /// );
    /// assert!(state.step_style(1).is_none());
    /// ```
    pub fn step_style(&self, index: usize) -> Option<&Style> {
        self.step_style_override(index)
    }

    /// Sets a per-step-index style override.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("Intake"), Step::new("Review")]);
    /// state.set_step_style(0, Style::default().fg(Color::Cyan));
    /// assert!(state.step_style_override(0).is_some());
    /// ```
    pub fn set_step_style(&mut self, index: usize, style: Style) {
        self.step_style_overrides.insert(index, style);
    }

    /// Removes a per-step-index style override.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::step_indicator::Step;
    /// use envision::component::StepIndicatorState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = StepIndicatorState::new(vec![Step::new("A")])
    ///     .with_step_style(0, Style::default().fg(Color::Cyan));
    /// state.clear_step_style(0);
    /// assert!(state.step_style_override(0).is_none());
    /// ```
    pub fn clear_step_style(&mut self, index: usize) {
        self.step_style_overrides.remove(&index);
    }

    /// Updates the state with a message, returning any output.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{StepIndicatorState, StepIndicatorMessage, StepIndicatorOutput};
    /// use envision::component::step_indicator::{Step, StepStatus};
    ///
    /// let steps = vec![
    ///     Step::new("Build").with_status(StepStatus::Active),
    ///     Step::new("Test"),
    /// ];
    /// let mut state = StepIndicatorState::new(steps);
    /// let output = state.update(StepIndicatorMessage::CompleteActive);
    /// assert!(matches!(output, Some(StepIndicatorOutput::StatusChanged { .. })));
    /// ```
    pub fn update(&mut self, msg: StepIndicatorMessage) -> Option<StepIndicatorOutput> {
        StepIndicator::update(self, msg)
    }
}