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
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
//! A stylish display-only component for application titles.
//!
//! [`TitleCard`] provides a centered title display with optional emoji
//! prefix/suffix, subtitle, configurable styles, and borders. State is
//! stored in [`TitleCardState`] and updated via [`TitleCardMessage`].
//!
//! # Example
//!
//! ```rust
//! use envision::component::{Component, TitleCard, TitleCardState, TitleCardMessage};
//! use ratatui::style::{Color, Modifier, Style};
//!
//! let state = TitleCardState::new("My App")
//!     .with_subtitle("A TUI Application")
//!     .with_prefix("🚀 ")
//!     .with_suffix(" ✨");
//!
//! assert_eq!(state.title(), "My App");
//! assert_eq!(state.subtitle(), Some("A TUI Application"));
//! assert_eq!(state.prefix(), Some("🚀 "));
//! assert_eq!(state.suffix(), Some(" ✨"));
//! ```

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

use super::{Component, RenderContext};

/// Messages that can be sent to a TitleCard.
#[derive(Clone, Debug, PartialEq)]
pub enum TitleCardMessage {
    /// Set the title text.
    SetTitle(String),
    /// Set the subtitle text.
    SetSubtitle(Option<String>),
    /// Set the prefix decoration.
    SetPrefix(Option<String>),
    /// Set the suffix decoration.
    SetSuffix(Option<String>),
    /// Set the title style.
    SetTitleStyle(Style),
    /// Set the subtitle style.
    SetSubtitleStyle(Style),
}

/// State for a TitleCard component.
///
/// Contains the title text, optional decorations, and styling configuration.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct TitleCardState {
    /// The title text.
    title: String,
    /// Optional subtitle displayed below the title.
    subtitle: Option<String>,
    /// Optional prefix displayed before the title (e.g., emoji).
    prefix: Option<String>,
    /// Optional suffix displayed after the title (e.g., emoji).
    suffix: Option<String>,
    /// Style for the title text.
    title_style: Style,
    /// Style for the subtitle text.
    subtitle_style: Style,
    /// Whether to show a border.
    bordered: bool,
    /// Whether the component is disabled.
    disabled: bool,
}

impl Default for TitleCardState {
    fn default() -> Self {
        Self {
            title: String::new(),
            subtitle: None,
            prefix: None,
            suffix: None,
            title_style: Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
            subtitle_style: Style::default().fg(Color::DarkGray),
            bordered: true,
            disabled: false,
        }
    }
}

impl TitleCardState {
    /// Creates a new title card with the given title.
    ///
    /// The default title style is Cyan + Bold, and the default subtitle
    /// style is DarkGray.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("My App");
    /// assert_eq!(state.title(), "My App");
    /// assert!(state.is_bordered());
    /// ```
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            ..Self::default()
        }
    }

    // ---- Builders ----

    /// Sets the subtitle (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App")
    ///     .with_subtitle("Version 1.0");
    /// assert_eq!(state.subtitle(), Some("Version 1.0"));
    /// ```
    pub fn with_subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// Sets the prefix decoration (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App")
    ///     .with_prefix("📚 ");
    /// assert_eq!(state.prefix(), Some("📚 "));
    /// ```
    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = Some(prefix.into());
        self
    }

    /// Sets the suffix decoration (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App")
    ///     .with_suffix(" v2");
    /// assert_eq!(state.suffix(), Some(" v2"));
    /// ```
    pub fn with_suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffix = Some(suffix.into());
        self
    }

    /// Sets the title style (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let style = Style::default().fg(Color::Red);
    /// let state = TitleCardState::new("App").with_title_style(style);
    /// assert_eq!(state.title_style(), style);
    /// ```
    pub fn with_title_style(mut self, style: Style) -> Self {
        self.title_style = style;
        self
    }

    /// Sets the subtitle style (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let style = Style::default().fg(Color::Gray);
    /// let state = TitleCardState::new("App").with_subtitle_style(style);
    /// assert_eq!(state.subtitle_style(), style);
    /// ```
    pub fn with_subtitle_style(mut self, style: Style) -> Self {
        self.subtitle_style = style;
        self
    }

    /// Sets whether to show a border (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App").with_bordered(false);
    /// assert!(!state.is_bordered());
    /// ```
    pub fn with_bordered(mut self, bordered: bool) -> Self {
        self.bordered = bordered;
        self
    }

    /// Sets the disabled state (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App").with_disabled(true);
    /// assert!(state.is_disabled());
    /// ```
    pub fn with_disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    // ---- Getters ----

    /// Returns the title text.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("My App");
    /// assert_eq!(state.title(), "My App");
    /// ```
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Returns the subtitle text.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App").with_subtitle("v1.0");
    /// assert_eq!(state.subtitle(), Some("v1.0"));
    /// ```
    pub fn subtitle(&self) -> Option<&str> {
        self.subtitle.as_deref()
    }

    /// Returns the prefix decoration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App").with_prefix(">> ");
    /// assert_eq!(state.prefix(), Some(">> "));
    /// ```
    pub fn prefix(&self) -> Option<&str> {
        self.prefix.as_deref()
    }

    /// Returns the suffix decoration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App");
    /// assert_eq!(state.suffix(), None);
    /// ```
    pub fn suffix(&self) -> Option<&str> {
        self.suffix.as_deref()
    }

    /// Returns the title style.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let style = Style::default().fg(Color::Magenta);
    /// let state = TitleCardState::new("App").with_title_style(style);
    /// assert_eq!(state.title_style(), style);
    /// ```
    pub fn title_style(&self) -> Style {
        self.title_style
    }

    /// Returns the subtitle style.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let style = Style::default().fg(Color::Gray);
    /// let state = TitleCardState::new("App").with_subtitle_style(style);
    /// assert_eq!(state.subtitle_style(), style);
    /// ```
    pub fn subtitle_style(&self) -> Style {
        self.subtitle_style
    }

    /// Returns whether the border is shown.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App").with_bordered(false);
    /// assert!(!state.is_bordered());
    /// ```
    pub fn is_bordered(&self) -> bool {
        self.bordered
    }

    /// Returns whether the component is disabled.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let state = TitleCardState::new("App");
    /// assert!(!state.is_disabled());
    /// ```
    pub fn is_disabled(&self) -> bool {
        self.disabled
    }

    // ---- Setters ----

    /// Sets the title text.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let mut state = TitleCardState::new("Old");
    /// state.set_title("New");
    /// assert_eq!(state.title(), "New");
    /// ```
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = title.into();
    }

    /// Sets the subtitle text.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let mut state = TitleCardState::new("App");
    /// state.set_subtitle(Some("Version 2.0".to_string()));
    /// assert_eq!(state.subtitle(), Some("Version 2.0"));
    /// ```
    pub fn set_subtitle(&mut self, subtitle: Option<String>) {
        self.subtitle = subtitle;
    }

    /// Sets the prefix decoration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let mut state = TitleCardState::new("App");
    /// state.set_prefix(Some(">> ".to_string()));
    /// assert_eq!(state.prefix(), Some(">> "));
    /// ```
    pub fn set_prefix(&mut self, prefix: Option<String>) {
        self.prefix = prefix;
    }

    /// Sets the suffix decoration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let mut state = TitleCardState::new("App");
    /// state.set_suffix(Some(" <<".to_string()));
    /// assert_eq!(state.suffix(), Some(" <<"));
    /// ```
    pub fn set_suffix(&mut self, suffix: Option<String>) {
        self.suffix = suffix;
    }

    /// Sets the title style.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = TitleCardState::new("App");
    /// let style = Style::default().fg(Color::Green);
    /// state.set_title_style(style);
    /// assert_eq!(state.title_style(), style);
    /// ```
    pub fn set_title_style(&mut self, style: Style) {
        self.title_style = style;
    }

    /// Sets the subtitle style.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    /// use ratatui::style::{Color, Style};
    ///
    /// let mut state = TitleCardState::new("App");
    /// let style = Style::default().fg(Color::White);
    /// state.set_subtitle_style(style);
    /// assert_eq!(state.subtitle_style(), style);
    /// ```
    pub fn set_subtitle_style(&mut self, style: Style) {
        self.subtitle_style = style;
    }

    /// Sets whether to show a border.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let mut state = TitleCardState::new("App");
    /// state.set_bordered(false);
    /// assert!(!state.is_bordered());
    /// ```
    pub fn set_bordered(&mut self, bordered: bool) {
        self.bordered = bordered;
    }

    /// Sets the disabled state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::TitleCardState;
    ///
    /// let mut state = TitleCardState::new("App");
    /// state.set_disabled(true);
    /// assert!(state.is_disabled());
    /// ```
    pub fn set_disabled(&mut self, disabled: bool) {
        self.disabled = disabled;
    }
}

/// A stylish display-only component for application titles.
///
/// Renders a centered title with optional decorations, subtitle, and borders.
/// This is a display-only component that does not receive keyboard focus.
///
/// # Example
///
/// ```rust
/// use envision::component::{Component, TitleCard, TitleCardState};
///
/// let state = TitleCardState::new("My Application")
///     .with_subtitle("v1.0.0")
///     .with_prefix("🎯 ");
///
/// assert_eq!(state.title(), "My Application");
/// assert_eq!(state.subtitle(), Some("v1.0.0"));
/// ```
pub struct TitleCard;

impl Component for TitleCard {
    type State = TitleCardState;
    type Message = TitleCardMessage;
    type Output = ();

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

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            TitleCardMessage::SetTitle(title) => state.title = title,
            TitleCardMessage::SetSubtitle(subtitle) => state.subtitle = subtitle,
            TitleCardMessage::SetPrefix(prefix) => state.prefix = prefix,
            TitleCardMessage::SetSuffix(suffix) => state.suffix = suffix,
            TitleCardMessage::SetTitleStyle(style) => state.title_style = style,
            TitleCardMessage::SetSubtitleStyle(style) => state.subtitle_style = style,
        }
        None
    }

    fn view(state: &Self::State, ctx: &mut RenderContext<'_, '_>) {
        crate::annotation::with_registry(|reg| {
            reg.register(
                ctx.area,
                crate::annotation::Annotation::title_card("title_card")
                    .with_label(state.title.as_str())
                    .with_disabled(ctx.disabled),
            );
        });

        let render_area = if state.bordered {
            let border_style = if ctx.disabled {
                ctx.theme.disabled_style()
            } else {
                ctx.theme.border_style()
            };

            let block = Block::default()
                .borders(Borders::ALL)
                .border_style(border_style);

            let inner = block.inner(ctx.area);
            ctx.frame.render_widget(block, ctx.area);
            inner
        } else {
            ctx.area
        };

        if render_area.height == 0 || render_area.width == 0 {
            return;
        }

        // Build title line with optional prefix and suffix
        let title_style = if ctx.disabled {
            ctx.theme.disabled_style()
        } else {
            state.title_style
        };

        let subtitle_style = if ctx.disabled {
            ctx.theme.disabled_style()
        } else {
            state.subtitle_style
        };

        let mut title_spans = Vec::new();
        if let Some(prefix) = &state.prefix {
            title_spans.push(Span::styled(prefix.as_str(), title_style));
        }
        title_spans.push(Span::styled(state.title.as_str(), title_style));
        if let Some(suffix) = &state.suffix {
            title_spans.push(Span::styled(suffix.as_str(), title_style));
        }

        let title_line = Line::from(title_spans);

        // Calculate vertical centering
        let content_height = if state.subtitle.is_some() { 2 } else { 1 };
        let vertical_offset = render_area.height.saturating_sub(content_height) / 2;

        // Render title
        let title_area = Rect::new(
            render_area.x,
            render_area.y + vertical_offset,
            render_area.width,
            1.min(render_area.height.saturating_sub(vertical_offset)),
        );

        if title_area.height > 0 {
            let title_paragraph = Paragraph::new(title_line).alignment(Alignment::Center);
            ctx.frame.render_widget(title_paragraph, title_area);
        }

        // Render subtitle if present
        if let Some(subtitle) = &state.subtitle {
            let subtitle_y = render_area.y + vertical_offset + 1;
            if subtitle_y < render_area.y + render_area.height {
                let subtitle_area = Rect::new(render_area.x, subtitle_y, render_area.width, 1);

                let subtitle_paragraph =
                    Paragraph::new(Span::styled(subtitle.as_str(), subtitle_style))
                        .alignment(Alignment::Center);
                ctx.frame.render_widget(subtitle_paragraph, subtitle_area);
            }
        }
    }
}

#[cfg(test)]
mod tests;