envision 0.10.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
//! Enhanced cell type that captures more information than ratatui's Cell.
//!
//! Uses [`CompactString`] for cell symbols to avoid heap allocation for typical
//! single-character cells. At 80×24, a terminal has 1,920 cells per frame;
//! `CompactString` stores up to 24 bytes inline, eliminating thousands of
//! small allocations per render cycle.

use compact_str::CompactString;
use ratatui::style::{Color, Modifier, Style};
use unicode_width::UnicodeWidthStr;

/// An enhanced cell that captures all styling information plus metadata.
///
/// Unlike ratatui's `Cell`, this type:
/// - Is fully serializable for snapshots and JSON export
/// - Tracks when the cell was last modified (frame number)
/// - Can store optional semantic annotations
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct EnhancedCell {
    /// The symbol (grapheme cluster) displayed in this cell
    symbol: CompactString,

    /// Foreground color
    pub fg: SerializableColor,

    /// Background color
    pub bg: SerializableColor,

    /// Text modifiers (bold, italic, etc.)
    pub modifiers: SerializableModifier,

    /// Underline color (if different from foreground)
    pub underline_color: Option<SerializableColor>,

    /// Frame number when this cell was last modified
    pub last_modified_frame: u64,

    /// Whether this cell should be skipped during rendering
    pub skip: bool,
}

impl EnhancedCell {
    /// Creates a new empty cell
    pub fn new() -> Self {
        Self {
            symbol: CompactString::from(" "),
            fg: SerializableColor::Reset,
            bg: SerializableColor::Reset,
            modifiers: SerializableModifier::empty(),
            underline_color: None,
            last_modified_frame: 0,
            skip: false,
        }
    }

    /// Creates a cell with the given symbol
    pub fn with_symbol(symbol: impl Into<CompactString>) -> Self {
        Self {
            symbol: symbol.into(),
            ..Self::new()
        }
    }

    /// Creates an EnhancedCell from a ratatui Cell
    pub fn from_ratatui_cell(cell: &ratatui::buffer::Cell, frame: u64) -> Self {
        let style = cell.style();
        Self {
            symbol: CompactString::from(cell.symbol()),
            fg: style
                .fg
                .map(SerializableColor::from)
                .unwrap_or(SerializableColor::Reset),
            bg: style
                .bg
                .map(SerializableColor::from)
                .unwrap_or(SerializableColor::Reset),
            modifiers: SerializableModifier::from(style.add_modifier),
            underline_color: style.underline_color.map(SerializableColor::from),
            last_modified_frame: frame,
            skip: cell.skip,
        }
    }

    /// Returns the symbol in this cell
    pub fn symbol(&self) -> &str {
        &self.symbol
    }

    /// Sets the symbol in this cell
    pub fn set_symbol(&mut self, symbol: impl Into<CompactString>) {
        self.symbol = symbol.into();
    }

    /// Sets the symbol to a single character
    pub fn set_char(&mut self, c: char) {
        self.symbol.clear();
        self.symbol.push(c);
    }

    /// Returns the display width of the symbol
    pub fn symbol_width(&self) -> usize {
        self.symbol.width()
    }

    /// Sets the style from a ratatui Style
    pub fn set_style(&mut self, style: Style) {
        if let Some(fg) = style.fg {
            self.fg = SerializableColor::from(fg);
        }
        if let Some(bg) = style.bg {
            self.bg = SerializableColor::from(bg);
        }
        self.modifiers = self
            .modifiers
            .union(SerializableModifier::from(style.add_modifier));
        self.modifiers = self
            .modifiers
            .difference(SerializableModifier::from(style.sub_modifier));
        if let Some(underline) = style.underline_color {
            self.underline_color = Some(SerializableColor::from(underline));
        }
    }

    /// Returns the style as a ratatui Style
    pub fn style(&self) -> Style {
        Style::new()
            .fg(self.fg.into())
            .bg(self.bg.into())
            .add_modifier(self.modifiers.into())
    }

    /// Resets the cell to empty state
    pub fn reset(&mut self) {
        self.symbol = CompactString::from(" ");
        self.fg = SerializableColor::Reset;
        self.bg = SerializableColor::Reset;
        self.modifiers = SerializableModifier::empty();
        self.underline_color = None;
        self.skip = false;
    }

    /// Returns true if this cell is empty (space with default styling)
    pub fn is_empty(&self) -> bool {
        self.symbol == " "
            && self.fg == SerializableColor::Reset
            && self.bg == SerializableColor::Reset
            && self.modifiers.is_empty()
    }
}

impl Default for EnhancedCell {
    fn default() -> Self {
        Self::new()
    }
}

/// A serializable version of ratatui's Color enum
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub enum SerializableColor {
    Reset,
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    Gray,
    DarkGray,
    LightRed,
    LightGreen,
    LightYellow,
    LightBlue,
    LightMagenta,
    LightCyan,
    White,
    Rgb { r: u8, g: u8, b: u8 },
    Indexed(u8),
}

impl From<Color> for SerializableColor {
    fn from(color: Color) -> Self {
        match color {
            Color::Reset => SerializableColor::Reset,
            Color::Black => SerializableColor::Black,
            Color::Red => SerializableColor::Red,
            Color::Green => SerializableColor::Green,
            Color::Yellow => SerializableColor::Yellow,
            Color::Blue => SerializableColor::Blue,
            Color::Magenta => SerializableColor::Magenta,
            Color::Cyan => SerializableColor::Cyan,
            Color::Gray => SerializableColor::Gray,
            Color::DarkGray => SerializableColor::DarkGray,
            Color::LightRed => SerializableColor::LightRed,
            Color::LightGreen => SerializableColor::LightGreen,
            Color::LightYellow => SerializableColor::LightYellow,
            Color::LightBlue => SerializableColor::LightBlue,
            Color::LightMagenta => SerializableColor::LightMagenta,
            Color::LightCyan => SerializableColor::LightCyan,
            Color::White => SerializableColor::White,
            Color::Rgb(r, g, b) => SerializableColor::Rgb { r, g, b },
            Color::Indexed(i) => SerializableColor::Indexed(i),
        }
    }
}

impl From<SerializableColor> for Color {
    fn from(color: SerializableColor) -> Self {
        match color {
            SerializableColor::Reset => Color::Reset,
            SerializableColor::Black => Color::Black,
            SerializableColor::Red => Color::Red,
            SerializableColor::Green => Color::Green,
            SerializableColor::Yellow => Color::Yellow,
            SerializableColor::Blue => Color::Blue,
            SerializableColor::Magenta => Color::Magenta,
            SerializableColor::Cyan => Color::Cyan,
            SerializableColor::Gray => Color::Gray,
            SerializableColor::DarkGray => Color::DarkGray,
            SerializableColor::LightRed => Color::LightRed,
            SerializableColor::LightGreen => Color::LightGreen,
            SerializableColor::LightYellow => Color::LightYellow,
            SerializableColor::LightBlue => Color::LightBlue,
            SerializableColor::LightMagenta => Color::LightMagenta,
            SerializableColor::LightCyan => Color::LightCyan,
            SerializableColor::White => Color::White,
            SerializableColor::Rgb { r, g, b } => Color::Rgb(r, g, b),
            SerializableColor::Indexed(i) => Color::Indexed(i),
        }
    }
}

impl SerializableColor {
    /// Returns the ANSI escape code for this color as foreground
    pub fn to_ansi_fg(self) -> String {
        match self {
            SerializableColor::Reset => "\x1b[39m".to_string(),
            SerializableColor::Black => "\x1b[30m".to_string(),
            SerializableColor::Red => "\x1b[31m".to_string(),
            SerializableColor::Green => "\x1b[32m".to_string(),
            SerializableColor::Yellow => "\x1b[33m".to_string(),
            SerializableColor::Blue => "\x1b[34m".to_string(),
            SerializableColor::Magenta => "\x1b[35m".to_string(),
            SerializableColor::Cyan => "\x1b[36m".to_string(),
            SerializableColor::Gray => "\x1b[37m".to_string(),
            SerializableColor::DarkGray => "\x1b[90m".to_string(),
            SerializableColor::LightRed => "\x1b[91m".to_string(),
            SerializableColor::LightGreen => "\x1b[92m".to_string(),
            SerializableColor::LightYellow => "\x1b[93m".to_string(),
            SerializableColor::LightBlue => "\x1b[94m".to_string(),
            SerializableColor::LightMagenta => "\x1b[95m".to_string(),
            SerializableColor::LightCyan => "\x1b[96m".to_string(),
            SerializableColor::White => "\x1b[97m".to_string(),
            SerializableColor::Rgb { r, g, b } => format!("\x1b[38;2;{};{};{}m", r, g, b),
            SerializableColor::Indexed(i) => format!("\x1b[38;5;{}m", i),
        }
    }

    /// Returns the ANSI escape code for this color as background
    pub fn to_ansi_bg(self) -> String {
        match self {
            SerializableColor::Reset => "\x1b[49m".to_string(),
            SerializableColor::Black => "\x1b[40m".to_string(),
            SerializableColor::Red => "\x1b[41m".to_string(),
            SerializableColor::Green => "\x1b[42m".to_string(),
            SerializableColor::Yellow => "\x1b[43m".to_string(),
            SerializableColor::Blue => "\x1b[44m".to_string(),
            SerializableColor::Magenta => "\x1b[45m".to_string(),
            SerializableColor::Cyan => "\x1b[46m".to_string(),
            SerializableColor::Gray => "\x1b[47m".to_string(),
            SerializableColor::DarkGray => "\x1b[100m".to_string(),
            SerializableColor::LightRed => "\x1b[101m".to_string(),
            SerializableColor::LightGreen => "\x1b[102m".to_string(),
            SerializableColor::LightYellow => "\x1b[103m".to_string(),
            SerializableColor::LightBlue => "\x1b[104m".to_string(),
            SerializableColor::LightMagenta => "\x1b[105m".to_string(),
            SerializableColor::LightCyan => "\x1b[106m".to_string(),
            SerializableColor::White => "\x1b[107m".to_string(),
            SerializableColor::Rgb { r, g, b } => format!("\x1b[48;2;{};{};{}m", r, g, b),
            SerializableColor::Indexed(i) => format!("\x1b[48;5;{}m", i),
        }
    }
}

/// A serializable version of ratatui's Modifier flags
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct SerializableModifier {
    pub bold: bool,
    pub dim: bool,
    pub italic: bool,
    pub underlined: bool,
    pub slow_blink: bool,
    pub rapid_blink: bool,
    pub reversed: bool,
    pub hidden: bool,
    pub crossed_out: bool,
}

impl SerializableModifier {
    /// Creates an empty modifier set
    pub const fn empty() -> Self {
        Self {
            bold: false,
            dim: false,
            italic: false,
            underlined: false,
            slow_blink: false,
            rapid_blink: false,
            reversed: false,
            hidden: false,
            crossed_out: false,
        }
    }

    /// Returns true if no modifiers are set
    pub fn is_empty(&self) -> bool {
        !self.bold
            && !self.dim
            && !self.italic
            && !self.underlined
            && !self.slow_blink
            && !self.rapid_blink
            && !self.reversed
            && !self.hidden
            && !self.crossed_out
    }

    /// Returns the union of two modifier sets
    pub fn union(self, other: Self) -> Self {
        Self {
            bold: self.bold || other.bold,
            dim: self.dim || other.dim,
            italic: self.italic || other.italic,
            underlined: self.underlined || other.underlined,
            slow_blink: self.slow_blink || other.slow_blink,
            rapid_blink: self.rapid_blink || other.rapid_blink,
            reversed: self.reversed || other.reversed,
            hidden: self.hidden || other.hidden,
            crossed_out: self.crossed_out || other.crossed_out,
        }
    }

    /// Returns self with modifiers from other removed
    pub fn difference(self, other: Self) -> Self {
        Self {
            bold: self.bold && !other.bold,
            dim: self.dim && !other.dim,
            italic: self.italic && !other.italic,
            underlined: self.underlined && !other.underlined,
            slow_blink: self.slow_blink && !other.slow_blink,
            rapid_blink: self.rapid_blink && !other.rapid_blink,
            reversed: self.reversed && !other.reversed,
            hidden: self.hidden && !other.hidden,
            crossed_out: self.crossed_out && !other.crossed_out,
        }
    }

    /// Returns the ANSI escape codes for these modifiers
    pub fn to_ansi(self) -> String {
        let mut codes = Vec::new();
        if self.bold {
            codes.push("1");
        }
        if self.dim {
            codes.push("2");
        }
        if self.italic {
            codes.push("3");
        }
        if self.underlined {
            codes.push("4");
        }
        if self.slow_blink {
            codes.push("5");
        }
        if self.rapid_blink {
            codes.push("6");
        }
        if self.reversed {
            codes.push("7");
        }
        if self.hidden {
            codes.push("8");
        }
        if self.crossed_out {
            codes.push("9");
        }
        if codes.is_empty() {
            String::new()
        } else {
            format!("\x1b[{}m", codes.join(";"))
        }
    }
}

impl From<Modifier> for SerializableModifier {
    fn from(modifier: Modifier) -> Self {
        Self {
            bold: modifier.contains(Modifier::BOLD),
            dim: modifier.contains(Modifier::DIM),
            italic: modifier.contains(Modifier::ITALIC),
            underlined: modifier.contains(Modifier::UNDERLINED),
            slow_blink: modifier.contains(Modifier::SLOW_BLINK),
            rapid_blink: modifier.contains(Modifier::RAPID_BLINK),
            reversed: modifier.contains(Modifier::REVERSED),
            hidden: modifier.contains(Modifier::HIDDEN),
            crossed_out: modifier.contains(Modifier::CROSSED_OUT),
        }
    }
}

impl From<SerializableModifier> for Modifier {
    fn from(modifier: SerializableModifier) -> Self {
        let mut m = Modifier::empty();
        if modifier.bold {
            m |= Modifier::BOLD;
        }
        if modifier.dim {
            m |= Modifier::DIM;
        }
        if modifier.italic {
            m |= Modifier::ITALIC;
        }
        if modifier.underlined {
            m |= Modifier::UNDERLINED;
        }
        if modifier.slow_blink {
            m |= Modifier::SLOW_BLINK;
        }
        if modifier.rapid_blink {
            m |= Modifier::RAPID_BLINK;
        }
        if modifier.reversed {
            m |= Modifier::REVERSED;
        }
        if modifier.hidden {
            m |= Modifier::HIDDEN;
        }
        if modifier.crossed_out {
            m |= Modifier::CROSSED_OUT;
        }
        m
    }
}

#[cfg(test)]
mod tests;