vt100/
cell.rs

1use unicode_width::UnicodeWidthChar as _;
2
3const CODEPOINTS_IN_CELL: usize = 6;
4
5/// Represents a single terminal cell.
6#[derive(Clone, Debug, Default, Eq)]
7pub struct Cell {
8    contents: [char; CODEPOINTS_IN_CELL],
9    len: u8,
10    attrs: crate::attrs::Attrs,
11}
12
13impl PartialEq<Self> for Cell {
14    fn eq(&self, other: &Self) -> bool {
15        if self.len != other.len {
16            return false;
17        }
18        if self.attrs != other.attrs {
19            return false;
20        }
21        let len = self.len();
22        // self.len() always returns a valid value
23        self.contents[..len] == other.contents[..len]
24    }
25}
26
27impl Cell {
28    #[inline]
29    fn len(&self) -> usize {
30        usize::from(self.len & 0x0f)
31    }
32
33    pub(crate) fn set(&mut self, c: char, a: crate::attrs::Attrs) {
34        self.contents[0] = c;
35        self.len = 1;
36        // strings in this context should always be an arbitrary character
37        // followed by zero or more zero-width characters, so we should only
38        // have to look at the first character
39        self.set_wide(c.width().unwrap_or(1) > 1);
40        self.attrs = a;
41    }
42
43    pub(crate) fn append(&mut self, c: char) {
44        let len = self.len();
45        if len >= CODEPOINTS_IN_CELL {
46            return;
47        }
48        if len == 0 {
49            // 0 is always less than 6
50            self.contents[0] = ' ';
51            self.len += 1;
52        }
53
54        let len = self.len();
55        // we already checked that len < CODEPOINTS_IN_CELL
56        self.contents[len] = c;
57        self.len += 1;
58    }
59
60    pub(crate) fn clear(&mut self, attrs: crate::attrs::Attrs) {
61        self.len = 0;
62        self.attrs = attrs;
63    }
64
65    /// Returns the text contents of the cell.
66    ///
67    /// Can include multiple unicode characters if combining characters are
68    /// used, but will contain at most one character with a non-zero character
69    /// width.
70    #[must_use]
71    pub fn contents(&self) -> String {
72        let mut s = String::with_capacity(CODEPOINTS_IN_CELL * 4);
73        for c in self.contents.iter().take(self.len()) {
74            s.push(*c);
75        }
76        s
77    }
78
79    /// Returns whether the cell contains any text data.
80    #[must_use]
81    pub fn has_contents(&self) -> bool {
82        self.len > 0
83    }
84
85    /// Returns whether the text data in the cell represents a wide character.
86    #[must_use]
87    pub fn is_wide(&self) -> bool {
88        self.len & 0x80 == 0x80
89    }
90
91    /// Returns whether the cell contains the second half of a wide character
92    /// (in other words, whether the previous cell in the row contains a wide
93    /// character)
94    #[must_use]
95    pub fn is_wide_continuation(&self) -> bool {
96        self.len & 0x40 == 0x40
97    }
98
99    fn set_wide(&mut self, wide: bool) {
100        if wide {
101            self.len |= 0x80;
102        } else {
103            self.len &= 0x7f;
104        }
105    }
106
107    pub(crate) fn set_wide_continuation(&mut self, wide: bool) {
108        if wide {
109            self.len |= 0x40;
110        } else {
111            self.len &= 0xbf;
112        }
113    }
114
115    pub(crate) fn attrs(&self) -> &crate::attrs::Attrs {
116        &self.attrs
117    }
118
119    /// Returns the foreground color of the cell.
120    #[must_use]
121    pub fn fgcolor(&self) -> crate::attrs::Color {
122        self.attrs.fgcolor
123    }
124
125    /// Returns the background color of the cell.
126    #[must_use]
127    pub fn bgcolor(&self) -> crate::attrs::Color {
128        self.attrs.bgcolor
129    }
130
131    /// Returns whether the cell should be rendered with the bold text
132    /// attribute.
133    #[must_use]
134    pub fn bold(&self) -> bool {
135        self.attrs.bold()
136    }
137
138    /// Returns whether the cell should be rendered with the italic text
139    /// attribute.
140    #[must_use]
141    pub fn italic(&self) -> bool {
142        self.attrs.italic()
143    }
144
145    /// Returns whether the cell should be rendered with the underlined text
146    /// attribute.
147    #[must_use]
148    pub fn underline(&self) -> bool {
149        self.attrs.underline()
150    }
151
152    /// Returns whether the cell should be rendered with the inverse text
153    /// attribute.
154    #[must_use]
155    pub fn inverse(&self) -> bool {
156        self.attrs.inverse()
157    }
158}