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
//! A horizontal or vertical separator line with optional label.
//!
//! [`Divider`] provides a simple visual separator for dividing content areas.
//! It can be oriented horizontally or vertically, and optionally displays a
//! centered label. This is a **display-only** component that does not receive
//! keyboard focus. State is stored in [`DividerState`] and updated via
//! [`DividerMessage`].
//!
//! # Example
//!
//! ```rust
//! use envision::component::{Divider, DividerMessage, DividerState, DividerOrientation, Component};
//!
//! // Create a horizontal divider with a label
//! let state = DividerState::new()
//!     .with_label("Section");
//! assert_eq!(state.label(), Some("Section"));
//! assert_eq!(state.orientation(), &DividerOrientation::Horizontal);
//!
//! // Create a vertical divider
//! let state = DividerState::vertical();
//! assert_eq!(state.orientation(), &DividerOrientation::Vertical);
//! ```

use ratatui::prelude::*;
use ratatui::widgets::Paragraph;

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

/// Orientation of the divider.
///
/// # Example
///
/// ```rust
/// use envision::component::DividerOrientation;
///
/// let orientation = DividerOrientation::default();
/// assert_eq!(orientation, DividerOrientation::Horizontal);
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum DividerOrientation {
    /// Horizontal separator line spanning the full width.
    #[default]
    Horizontal,
    /// Vertical separator line spanning the full height.
    Vertical,
}

/// Messages that can be sent to a Divider.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DividerMessage {
    /// Change the label text.
    SetLabel(Option<String>),
    /// Change the orientation.
    SetOrientation(DividerOrientation),
}

/// State for a Divider component.
///
/// Contains the orientation, optional label, optional color override,
/// and disabled state.
///
/// # Example
///
/// ```rust
/// use envision::component::DividerState;
///
/// let state = DividerState::new();
/// assert!(state.label().is_none());
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct DividerState {
    /// The orientation of the divider.
    orientation: DividerOrientation,
    /// Optional centered label text.
    label: Option<String>,
    /// Optional color override for the divider line.
    color: Option<Color>,
}

impl Default for DividerState {
    fn default() -> Self {
        Self {
            orientation: DividerOrientation::Horizontal,
            label: None,
            color: None,
        }
    }
}

impl DividerState {
    /// Creates a new horizontal divider with no label.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerOrientation};
    ///
    /// let state = DividerState::new();
    /// assert_eq!(state.orientation(), &DividerOrientation::Horizontal);
    /// assert!(state.label().is_none());
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new horizontal divider.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerOrientation};
    ///
    /// let state = DividerState::horizontal();
    /// assert_eq!(state.orientation(), &DividerOrientation::Horizontal);
    /// ```
    pub fn horizontal() -> Self {
        Self {
            orientation: DividerOrientation::Horizontal,
            ..Self::default()
        }
    }

    /// Creates a new vertical divider.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerOrientation};
    ///
    /// let state = DividerState::vertical();
    /// assert_eq!(state.orientation(), &DividerOrientation::Vertical);
    /// ```
    pub fn vertical() -> Self {
        Self {
            orientation: DividerOrientation::Vertical,
            ..Self::default()
        }
    }

    // ---- Builders ----

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

    /// Sets the color override (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    /// use ratatui::style::Color;
    ///
    /// let state = DividerState::new().with_color(Color::Red);
    /// assert_eq!(state.color(), Some(Color::Red));
    /// ```
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Sets the orientation (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerOrientation};
    ///
    /// let state = DividerState::new()
    ///     .with_orientation(DividerOrientation::Vertical);
    /// assert_eq!(state.orientation(), &DividerOrientation::Vertical);
    /// ```
    pub fn with_orientation(mut self, orientation: DividerOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    // ---- Getters ----

    /// Returns the label text if set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    ///
    /// let state = DividerState::new();
    /// assert_eq!(state.label(), None);
    ///
    /// let state = DividerState::new().with_label("Title");
    /// assert_eq!(state.label(), Some("Title"));
    /// ```
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// Returns the orientation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerOrientation};
    ///
    /// let state = DividerState::new();
    /// assert_eq!(state.orientation(), &DividerOrientation::Horizontal);
    /// ```
    pub fn orientation(&self) -> &DividerOrientation {
        &self.orientation
    }

    /// Returns the color override if set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    ///
    /// let state = DividerState::new();
    /// assert_eq!(state.color(), None);
    /// ```
    pub fn color(&self) -> Option<Color> {
        self.color
    }

    // ---- Setters ----

    /// Sets the label text.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    ///
    /// let mut state = DividerState::new();
    /// state.set_label(Some("New Label".to_string()));
    /// assert_eq!(state.label(), Some("New Label"));
    ///
    /// state.set_label(None);
    /// assert_eq!(state.label(), None);
    /// ```
    pub fn set_label(&mut self, label: Option<String>) {
        self.label = label;
    }

    /// Sets the color override.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    /// use ratatui::style::Color;
    ///
    /// let mut state = DividerState::new();
    /// state.set_color(Some(Color::Red));
    /// assert_eq!(state.color(), Some(Color::Red));
    /// ```
    pub fn set_color(&mut self, color: Option<Color>) {
        self.color = color;
    }

    /// Sets the orientation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerOrientation};
    ///
    /// let mut state = DividerState::new();
    /// state.set_orientation(DividerOrientation::Vertical);
    /// assert_eq!(state.orientation(), &DividerOrientation::Vertical);
    /// ```
    pub fn set_orientation(&mut self, orientation: DividerOrientation) {
        self.orientation = orientation;
    }

    // ---- Instance methods ----

    /// Updates the divider state with a message.
    ///
    /// This is an instance method that delegates to [`Divider::update`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{DividerState, DividerMessage};
    ///
    /// let mut state = DividerState::new();
    /// state.update(DividerMessage::SetLabel(Some("Updated".to_string())));
    /// assert_eq!(state.label(), Some("Updated"));
    /// ```
    pub fn update(&mut self, msg: DividerMessage) -> Option<()> {
        Divider::update(self, msg)
    }

    /// Maps an input event to a divider message.
    ///
    /// This is an instance method that delegates to [`Divider::handle_event`].
    /// Since the divider is display-only, this always returns `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    /// use envision::input::{Event, Key};
    ///
    /// let state = DividerState::new();
    /// assert!(state.handle_event(&Event::key(Key::Enter)).is_none());
    /// ```
    pub fn handle_event(&self, event: &Event) -> Option<DividerMessage> {
        Divider::handle_event(self, event, &EventContext::default())
    }

    /// Dispatches an event by mapping it to a message and updating state.
    ///
    /// This is an instance method that delegates to [`Divider::dispatch_event`].
    /// Since the divider is display-only, this always returns `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::DividerState;
    /// use envision::input::{Event, Key};
    ///
    /// let mut state = DividerState::new();
    /// assert!(state.dispatch_event(&Event::key(Key::Enter)).is_none());
    /// ```
    pub fn dispatch_event(&mut self, event: &Event) -> Option<()> {
        Divider::dispatch_event(self, event, &EventContext::default())
    }
}

/// A horizontal or vertical separator line with optional label.
///
/// `Divider` renders a line of box-drawing characters (`─` for horizontal,
/// `│` for vertical) to visually separate content areas. An optional label
/// can be centered on the divider line.
///
/// This is a display-only component that does not receive keyboard focus.
///
/// # Example
///
/// ```rust
/// use envision::component::{Component, Divider, DividerState, DividerMessage};
///
/// let mut state = DividerState::new().with_label("Settings");
/// assert_eq!(state.label(), Some("Settings"));
///
/// Divider::update(&mut state, DividerMessage::SetLabel(None));
/// assert_eq!(state.label(), None);
/// ```
pub struct Divider;

impl Component for Divider {
    type State = DividerState;
    type Message = DividerMessage;
    type Output = ();

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

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            DividerMessage::SetLabel(label) => state.label = label,
            DividerMessage::SetOrientation(orientation) => state.orientation = orientation,
        }
        None
    }

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

        if ctx.area.width == 0 || ctx.area.height == 0 {
            return;
        }

        let style = if ctx.disabled {
            ctx.theme.disabled_style()
        } else if let Some(color) = state.color {
            Style::default().fg(color)
        } else {
            ctx.theme.normal_style()
        };

        match state.orientation {
            DividerOrientation::Horizontal => {
                render_horizontal(state, ctx.frame, ctx.area, style);
            }
            DividerOrientation::Vertical => {
                render_vertical(state, ctx.frame, ctx.area, style);
            }
        }
    }
}

/// Renders a horizontal divider line with optional centered label.
fn render_horizontal(state: &DividerState, frame: &mut Frame, area: Rect, style: Style) {
    let line_char = "\u{2500}"; //    let width = area.width as usize;

    let text = match &state.label {
        Some(label) => {
            let label_with_padding = format!(" {} ", label);
            let label_len = label_with_padding.len();
            if label_len >= width {
                // Label too long, just show the label truncated
                let truncated: String = label_with_padding.chars().take(width).collect();
                Line::from(Span::styled(truncated, style))
            } else {
                let remaining = width - label_len;
                let left = remaining / 2;
                let right = remaining - left;
                let line = format!(
                    "{}{}{}",
                    line_char.repeat(left),
                    label_with_padding,
                    line_char.repeat(right),
                );
                Line::from(Span::styled(line, style))
            }
        }
        None => Line::from(Span::styled(line_char.repeat(width), style)),
    };

    let render_area = Rect::new(area.x, area.y, area.width, 1.min(area.height));
    let paragraph = Paragraph::new(text);
    frame.render_widget(paragraph, render_area);
}

/// Renders a vertical divider line with optional centered label.
fn render_vertical(state: &DividerState, frame: &mut Frame, area: Rect, style: Style) {
    let line_char = "\u{2502}"; //    let height = area.height as usize;

    let middle_row = height / 2;

    let mut lines = Vec::with_capacity(height);
    for row in 0..height {
        let content = match &state.label {
            Some(label) if row == middle_row => {
                // Show first character of label in the middle
                label
                    .chars()
                    .next()
                    .map_or_else(|| line_char.to_string(), |c| c.to_string())
            }
            _ => line_char.to_string(),
        };
        lines.push(Line::from(Span::styled(content, style)));
    }

    let render_area = Rect::new(area.x, area.y, 1.min(area.width), area.height);
    let paragraph = Paragraph::new(lines);
    frame.render_widget(paragraph, render_area);
}

#[cfg(test)]
mod tests;