envision 0.15.1

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
//! Status bar item types and content.
//!
//! This module contains the types for individual status bar items,
//! their content variants, and visual styles.

use crate::theme::Theme;
use ratatui::prelude::*;

/// Content type for status bar items.
///
/// Items can display static text or dynamic content that updates over time.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum StatusBarItemContent {
    /// Static text content.
    Static(String),
    /// Elapsed time display.
    ///
    /// Shows time elapsed since the timer was started. Format depends on
    /// `long_format`: short format is "MM:SS", long format is "HH:MM:SS".
    ElapsedTime {
        /// Accumulated elapsed time in milliseconds.
        elapsed_ms: u64,
        /// Whether the timer is currently running.
        running: bool,
        /// Whether to use long format (HH:MM:SS vs MM:SS).
        long_format: bool,
    },
    /// Numeric counter display.
    ///
    /// Shows a counter value with an optional label.
    Counter {
        /// Current counter value.
        value: u64,
        /// Optional label (displayed before value).
        label: Option<String>,
    },
    /// Animated heartbeat indicator.
    ///
    /// Shows an animated indicator to show activity.
    Heartbeat {
        /// Whether the heartbeat is active.
        active: bool,
        /// Current animation frame.
        frame: usize,
    },
}

impl StatusBarItemContent {
    /// Creates static text content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItemContent;
    ///
    /// let content = StatusBarItemContent::static_text("Ready");
    /// assert!(matches!(content, StatusBarItemContent::Static(_)));
    /// ```
    pub fn static_text(text: impl Into<String>) -> Self {
        Self::Static(text.into())
    }

    /// Creates an elapsed time display.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItemContent;
    ///
    /// let content = StatusBarItemContent::elapsed_time();
    /// assert!(matches!(content, StatusBarItemContent::ElapsedTime { .. }));
    /// ```
    pub fn elapsed_time() -> Self {
        Self::ElapsedTime {
            elapsed_ms: 0,
            running: false,
            long_format: false,
        }
    }

    /// Creates a counter display.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItemContent;
    ///
    /// let content = StatusBarItemContent::counter();
    /// assert!(matches!(content, StatusBarItemContent::Counter { value: 0, label: None }));
    /// ```
    pub fn counter() -> Self {
        Self::Counter {
            value: 0,
            label: None,
        }
    }

    /// Creates a heartbeat indicator.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItemContent;
    ///
    /// let content = StatusBarItemContent::heartbeat();
    /// assert!(matches!(content, StatusBarItemContent::Heartbeat { active: false, .. }));
    /// ```
    pub fn heartbeat() -> Self {
        Self::Heartbeat {
            active: false,
            frame: 0,
        }
    }

    /// Returns the display text for this content.
    pub(super) fn display_text(&self) -> String {
        match self {
            Self::Static(text) => text.clone(),
            Self::ElapsedTime {
                elapsed_ms,
                long_format,
                ..
            } => {
                let total_seconds = elapsed_ms / 1000;
                let hours = total_seconds / 3600;
                let minutes = (total_seconds % 3600) / 60;
                let seconds = total_seconds % 60;

                if *long_format || hours > 0 {
                    format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
                } else {
                    format!("{:02}:{:02}", minutes, seconds)
                }
            }
            Self::Counter { value, label } => {
                if let Some(label) = label {
                    format!("{}: {}", label, value)
                } else {
                    value.to_string()
                }
            }
            Self::Heartbeat { active, frame } => {
                const FRAMES: [&str; 4] = ["♡", "♥", "♥", "♡"];
                if *active {
                    FRAMES[*frame % FRAMES.len()].to_string()
                } else {
                    "♡".to_string()
                }
            }
        }
    }

    /// Returns true if this is dynamic content that needs ticking.
    pub(super) fn is_dynamic(&self) -> bool {
        !matches!(self, Self::Static(_))
    }
}

/// Style variants for status bar items.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum StatusBarStyle {
    /// Default style (no special coloring).
    #[default]
    Default,
    /// Informational style (typically blue).
    Info,
    /// Success style (typically green).
    Success,
    /// Warning style (typically yellow).
    Warning,
    /// Error style (typically red).
    Error,
    /// Muted/secondary style (typically gray).
    Muted,
}

impl StatusBarStyle {
    /// Returns the ratatui style for this status bar style variant.
    pub(super) fn style(self, theme: &Theme) -> Style {
        match self {
            Self::Default => theme.normal_style(),
            Self::Info => theme.info_style(),
            Self::Success => theme.success_style(),
            Self::Warning => theme.warning_style(),
            Self::Error => theme.error_style(),
            Self::Muted => theme.disabled_style(),
        }
    }
}

/// A single item in the status bar.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct StatusBarItem {
    /// The content of the item.
    pub(super) content: StatusBarItemContent,
    /// The style of the item.
    pub(super) style: StatusBarStyle,
    /// Whether to show a separator after this item.
    separator: bool,
}

impl StatusBarItem {
    /// Creates a new status bar item with static text content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::new("Ready");
    /// assert_eq!(item.text(), "Ready");
    /// ```
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            content: StatusBarItemContent::Static(text.into()),
            style: StatusBarStyle::Default,
            separator: true,
        }
    }

    /// Creates a new status bar item with an elapsed time display.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::elapsed_time();
    /// assert_eq!(item.text(), "00:00");
    /// ```
    pub fn elapsed_time() -> Self {
        Self {
            content: StatusBarItemContent::elapsed_time(),
            style: StatusBarStyle::Default,
            separator: true,
        }
    }

    /// Creates an elapsed time display with long format (HH:MM:SS).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::elapsed_time_long();
    /// assert_eq!(item.text(), "00:00:00");
    /// ```
    pub fn elapsed_time_long() -> Self {
        Self {
            content: StatusBarItemContent::ElapsedTime {
                elapsed_ms: 0,
                running: false,
                long_format: true,
            },
            style: StatusBarStyle::Default,
            separator: true,
        }
    }

    /// Creates a new status bar item with a counter display.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::counter().with_label("Items");
    /// ```
    pub fn counter() -> Self {
        Self {
            content: StatusBarItemContent::counter(),
            style: StatusBarStyle::Default,
            separator: true,
        }
    }

    /// Creates a new status bar item with a heartbeat indicator.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::heartbeat();
    /// ```
    pub fn heartbeat() -> Self {
        Self {
            content: StatusBarItemContent::heartbeat(),
            style: StatusBarStyle::Default,
            separator: true,
        }
    }

    /// Sets the label for counter items.
    ///
    /// This only has an effect on Counter content types.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::counter().with_label("Items");
    /// assert_eq!(item.text(), "Items: 0");
    /// ```
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        if let StatusBarItemContent::Counter {
            value,
            label: ref mut lbl,
        } = self.content
        {
            *lbl = Some(label.into());
            self.content = StatusBarItemContent::Counter {
                value,
                label: lbl.clone(),
            };
        }
        self
    }

    /// Sets long format for elapsed time items.
    ///
    /// This only has an effect on ElapsedTime content types.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::elapsed_time().with_long_format(true);
    /// assert_eq!(item.text(), "00:00:00");
    /// ```
    pub fn with_long_format(mut self, long: bool) -> Self {
        if let StatusBarItemContent::ElapsedTime {
            elapsed_ms,
            running,
            ..
        } = self.content
        {
            self.content = StatusBarItemContent::ElapsedTime {
                elapsed_ms,
                running,
                long_format: long,
            };
        }
        self
    }

    /// Sets the style for this item.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{StatusBarItem, StatusBarStyle};
    ///
    /// let item = StatusBarItem::new("Error").with_style(StatusBarStyle::Error);
    /// assert_eq!(item.style(), StatusBarStyle::Error);
    /// ```
    pub fn with_style(mut self, style: StatusBarStyle) -> Self {
        self.style = style;
        self
    }

    /// Sets whether to show a separator after this item.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::new("Last").with_separator(false);
    /// assert!(!item.has_separator());
    /// ```
    pub fn with_separator(mut self, separator: bool) -> Self {
        self.separator = separator;
        self
    }

    /// Returns the display text for this item.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::new("Ready");
    /// assert_eq!(item.text(), "Ready");
    /// ```
    pub fn text(&self) -> String {
        self.content.display_text()
    }

    /// Returns the content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{StatusBarItem, StatusBarItemContent};
    ///
    /// let item = StatusBarItem::new("Status");
    /// assert!(matches!(item.content(), StatusBarItemContent::Static(_)));
    /// ```
    pub fn content(&self) -> &StatusBarItemContent {
        &self.content
    }

    /// Returns a mutable reference to the content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{StatusBarItem, StatusBarItemContent};
    ///
    /// let mut item = StatusBarItem::new("Status");
    /// *item.content_mut() = StatusBarItemContent::static_text("Updated");
    /// assert_eq!(item.text(), "Updated");
    /// ```
    pub fn content_mut(&mut self) -> &mut StatusBarItemContent {
        &mut self.content
    }

    /// Sets the text content (converts to static content).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let mut item = StatusBarItem::new("Old");
    /// item.set_text("New");
    /// assert_eq!(item.text(), "New");
    /// ```
    pub fn set_text(&mut self, text: impl Into<String>) {
        self.content = StatusBarItemContent::Static(text.into());
    }

    /// Returns the style.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{StatusBarItem, StatusBarStyle};
    ///
    /// let item = StatusBarItem::new("OK").with_style(StatusBarStyle::Success);
    /// assert_eq!(item.style(), StatusBarStyle::Success);
    /// ```
    pub fn style(&self) -> StatusBarStyle {
        self.style
    }

    /// Sets the style.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{StatusBarItem, StatusBarStyle};
    ///
    /// let mut item = StatusBarItem::new("Error");
    /// item.set_style(StatusBarStyle::Error);
    /// assert_eq!(item.style(), StatusBarStyle::Error);
    /// ```
    pub fn set_style(&mut self, style: StatusBarStyle) {
        self.style = style;
    }

    /// Returns whether this item has a separator.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let item = StatusBarItem::new("Text");
    /// assert!(item.has_separator()); // enabled by default
    /// ```
    pub fn has_separator(&self) -> bool {
        self.separator
    }

    /// Sets whether to show a separator.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let mut item = StatusBarItem::new("Last");
    /// item.set_separator(false);
    /// assert!(!item.has_separator());
    /// ```
    pub fn set_separator(&mut self, separator: bool) {
        self.separator = separator;
    }

    /// Returns true if this item has dynamic content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::StatusBarItem;
    ///
    /// let static_item = StatusBarItem::new("Text");
    /// assert!(!static_item.is_dynamic());
    ///
    /// let timer = StatusBarItem::elapsed_time();
    /// assert!(timer.is_dynamic());
    /// ```
    pub fn is_dynamic(&self) -> bool {
        self.content.is_dynamic()
    }

    /// Processes a tick for dynamic content.
    ///
    /// Returns true if the content was updated.
    pub(super) fn tick(&mut self, delta_ms: u64) -> bool {
        match &mut self.content {
            StatusBarItemContent::ElapsedTime {
                elapsed_ms,
                running: true,
                ..
            } => {
                *elapsed_ms += delta_ms;
                true
            }
            StatusBarItemContent::Heartbeat {
                active: true,
                frame,
            } => {
                *frame = (*frame + 1) % 4;
                true
            }
            _ => false,
        }
    }
}