slt/cell.rs
1use crate::style::Style;
2
3/// A single terminal cell containing a character and style.
4///
5/// Each cell holds one grapheme cluster (stored as a `String` to support
6/// multi-byte Unicode) and the [`Style`] to render it with. Wide characters
7/// (e.g., CJK) occupy two adjacent cells; the second cell's `symbol` is left
8/// empty by the buffer layer.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Cell {
11 /// The grapheme cluster displayed in this cell. Defaults to a single space.
12 pub symbol: String,
13 /// The visual style (colors and modifiers) for this cell.
14 pub style: Style,
15}
16
17impl Default for Cell {
18 fn default() -> Self {
19 Self {
20 symbol: " ".into(),
21 style: Style::new(),
22 }
23 }
24}
25
26impl Cell {
27 /// Replace the cell's symbol with the given string slice.
28 pub fn set_symbol(&mut self, s: &str) -> &mut Self {
29 self.symbol.clear();
30 self.symbol.push_str(s);
31 self
32 }
33
34 /// Replace the cell's symbol with a single character.
35 pub fn set_char(&mut self, ch: char) -> &mut Self {
36 self.symbol.clear();
37 self.symbol.push(ch);
38 self
39 }
40
41 /// Set the cell's style.
42 pub fn set_style(&mut self, style: Style) -> &mut Self {
43 self.style = style;
44 self
45 }
46
47 /// Reset the cell to a blank space with default style.
48 pub fn reset(&mut self) {
49 self.symbol.clear();
50 self.symbol.push(' ');
51 self.style = Style::new();
52 }
53}