Skip to main content

slt/
cell.rs

1//! Single terminal cell — the smallest unit of the render buffer.
2
3use compact_str::CompactString;
4
5use crate::style::Style;
6
7/// A single terminal cell containing a character and style.
8///
9/// Each cell holds one grapheme cluster (stored as a [`CompactString`] for
10/// inline storage of short strings — no heap allocation for ≤24 bytes).
11/// Wide characters (e.g., CJK) occupy two adjacent cells; the second cell's
12/// `symbol` is left empty by the buffer layer.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Cell {
15    /// The grapheme cluster displayed in this cell. Defaults to a single space.
16    pub symbol: CompactString,
17    /// The visual style (colors and modifiers) for this cell.
18    pub style: Style,
19    /// Optional OSC 8 hyperlink URL. When set, the terminal renders this cell
20    /// as a clickable link.
21    pub hyperlink: Option<CompactString>,
22}
23
24impl Default for Cell {
25    fn default() -> Self {
26        Self {
27            symbol: CompactString::const_new(" "),
28            style: Style::new(),
29            hyperlink: None,
30        }
31    }
32}
33
34impl Cell {
35    /// Replace the cell's symbol with the given string slice.
36    pub fn set_symbol(&mut self, s: &str) -> &mut Self {
37        self.symbol.clear();
38        self.symbol.push_str(s);
39        self
40    }
41
42    /// Replace the cell's symbol with a single character.
43    pub fn set_char(&mut self, ch: char) -> &mut Self {
44        self.symbol.clear();
45        self.symbol.push(ch);
46        self
47    }
48
49    /// Set the cell's style.
50    pub fn set_style(&mut self, style: Style) -> &mut Self {
51        self.style = style;
52        self
53    }
54
55    /// Reset the cell to a blank space with default style.
56    pub fn reset(&mut self) {
57        self.symbol.clear();
58        self.symbol.push(' ');
59        self.style = Style::new();
60        self.hyperlink = None;
61    }
62}