Skip to main content

justerm_core/
cell.rs

1//! The cell — one character position in the grid (see CONTEXT.md "Cell").
2
3use crate::color::Color;
4
5bitflags::bitflags! {
6    /// Per-cell flags: the standard SGR attributes plus layout markers.
7    ///
8    /// The high bits are intentionally left free so underline-style + underline
9    /// colour and an OSC 8 hyperlink id can be added later without a format
10    /// change (see `docs/architecture.md` "Cell").
11    #[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
12    pub struct CellFlags: u16 {
13        // --- standard SGR attributes ---
14        const BOLD          = 1 << 0;
15        const DIM           = 1 << 1;
16        const ITALIC        = 1 << 2;
17        const UNDERLINE     = 1 << 3;
18        const BLINK         = 1 << 4;
19        const INVERSE       = 1 << 5;
20        const HIDDEN        = 1 << 6;
21        const STRIKETHROUGH = 1 << 7;
22
23        // --- layout markers (not SGR): a width-2 glyph occupies two cells ---
24        /// The first cell of a width-2 glyph; holds the actual character.
25        const WIDE_CHAR        = 1 << 8;
26        /// The trailing cell of a width-2 glyph. A distinct marker, *not* a
27        /// plain blank — overwrite, erase, selection, and cursor positioning all
28        /// depend on knowing this column belongs to the wide char to its left.
29        const WIDE_CHAR_SPACER = 1 << 9;
30        /// Set on the last cell of a row that soft-wrapped (auto-wrap) into the
31        /// next — distinguishes a soft wrap from a hard CR/LF line-end so reflow
32        /// (#7) can merge and re-split logical lines.
33        const WRAPLINE = 1 << 10;
34        // bits 11..=15 reserved (underline style/colour, hyperlink id).
35    }
36}
37
38// --- packed bit layout (#44) ----------------------------------------------
39//
40// Three 32-bit words mirroring xterm.js's `BufferLine` cell (verified against
41// `xtermjs/xterm.js@master` `src/common/buffer/Constants.ts`). The fg/bg colour
42// words are byte-identical to xterm's `Attributes` + `FgFlags`/`BgFlags`; the
43// content word keeps justerm's explicit layout-marker flags where xterm stores a
44// 2-bit `wcwidth` value (justerm's model is flag-based — the spacer and per-cell
45// WRAPLINE are load-bearing for overwrite/selection/reflow).
46//
47//   content u32: codepoint(21) | COMBINED(1) | WIDE | SPACER | WRAP | reserved
48//   fg/bg   u32: colour value(24) | colour mode(2) | flags(6)
49//
50// COMBINED_PRESENT (content) and LINK_PRESENT (bg, xterm's HAS_EXTENDED slot) are
51// both live: combining clusters (#45) and OSC 8 hyperlink indices (#46) live in
52// per-row, column-keyed maps, and these bits gate every read of them. The cell is
53// now pure packed words — three u32, no `Option` field (the epic's 12 B target).
54
55const CODEPOINT_MASK: u32 = 0x001F_FFFF; // bits 0..21
56const C_COMBINED: u32 = 1 << 21; // a combining cluster lives in the row's map at this column (#45)
57const C_WIDE: u32 = 1 << 22;
58const C_SPACER: u32 = 1 << 23;
59const C_WRAP: u32 = 1 << 24;
60// The vacated column left when a width-2 glyph wraps off the right edge (#113):
61// a blank that holds no character but isn't a hard line-end. Unlike C_SPACER it
62// has *no wide lead to its left*, so the overwrite/erase repair paths (which key
63// off C_SPACER) must not treat it as one — it's a separate marker the text
64// extractors skip. Engine-internal: it stays in the content word and never
65// reaches `flags()` / the wire (a frame-mode consumer gets the already-correct
66// text, and the cell renders as the blank it is).
67const C_LEADING_SPACER: u32 = 1 << 25;
68const CONTENT_MARKER_MASK: u32 = C_WIDE | C_SPACER | C_WRAP;
69
70const COLOR_VALUE_MASK: u32 = 0x00FF_FFFF; // bits 0..24
71const COLOR_MODE_SHIFT: u32 = 24; // bits 24..26
72const CM_DEFAULT: u32 = 0;
73const CM_INDEXED: u32 = 1;
74const CM_RGB: u32 = 2;
75
76// fg flags, bits 26..32 — xterm FgFlags order (HIDDEN == xterm INVISIBLE).
77const FG_INVERSE: u32 = 1 << 26;
78const FG_BOLD: u32 = 1 << 27;
79const FG_UNDERLINE: u32 = 1 << 28;
80const FG_BLINK: u32 = 1 << 29;
81const FG_HIDDEN: u32 = 1 << 30;
82const FG_STRIKE: u32 = 1 << 31;
83const FG_FLAG_MASK: u32 = FG_INVERSE | FG_BOLD | FG_UNDERLINE | FG_BLINK | FG_HIDDEN | FG_STRIKE;
84
85// bg flags, bits 26..28 — xterm BgFlags order.
86const BG_ITALIC: u32 = 1 << 26;
87const BG_DIM: u32 = 1 << 27;
88// LINK_PRESENT: an OSC 8 hyperlink index lives in the row's link map at this
89// column (#46). Reuses xterm's `BgFlags.HAS_EXTENDED = 0x10000000` (bit 28)
90// exactly — in xterm this is a *shared* "extended attrs present" gate (link +
91// underline colour); justerm models only the link, so it is link-only here and
92// would widen to an extended-attrs map if underline colour is ever added.
93const BG_LINK: u32 = 1 << 28;
94const BG_FLAG_MASK: u32 = BG_ITALIC | BG_DIM;
95
96/// Pack a colour reference into the low 26 bits of a colour word (mode + value);
97/// the high 6 bits are left for the SGR flags.
98fn pack_color(c: Color) -> u32 {
99    match c {
100        Color::Default => CM_DEFAULT << COLOR_MODE_SHIFT,
101        Color::Indexed(i) => (CM_INDEXED << COLOR_MODE_SHIFT) | i as u32,
102        Color::Rgb(r, g, b) => {
103            (CM_RGB << COLOR_MODE_SHIFT) | (r as u32) << 16 | (g as u32) << 8 | b as u32
104        }
105    }
106}
107
108/// Inverse of [`pack_color`] — reads only the mode + value bits, ignoring the
109/// flag bits that share the word.
110fn unpack_color(w: u32) -> Color {
111    match (w >> COLOR_MODE_SHIFT) & 0b11 {
112        CM_INDEXED => Color::Indexed((w & 0xFF) as u8),
113        CM_RGB => Color::Rgb((w >> 16) as u8, (w >> 8) as u8, w as u8),
114        _ => Color::Default, // CM_DEFAULT (and the unused mode 3) resolve to Default
115    }
116}
117
118/// Scatter a `CellFlags` bit set (as a `u32`) into the three words' flag-bit
119/// positions: `(content_markers, fg_flags, bg_flags)`. Branchless — each group is
120/// masked and shifted in one step. The `CellFlags` bit values are frozen by the
121/// wire format (`serialize` encodes `flags().bits()`), so the source positions are
122/// fixed; see the shift comments. One place for store / insert / remove to share.
123#[inline]
124fn flag_words(f: u32) -> (u32, u32, u32) {
125    let content = (f & 0x0700) << 14; // WIDE/SPACER/WRAP bits 8,9,10 -> 22,23,24
126    let fg = ((f & 0x0001) << 27)     // BOLD     bit 0  -> 27
127        | ((f & 0x0020) << 21)        // INVERSE  bit 5  -> 26
128        | ((f & 0x0018) << 25)        // UNDERLINE/BLINK bits 3,4 -> 28,29
129        | ((f & 0x00C0) << 24); // HIDDEN/STRIKE   bits 6,7 -> 30,31
130    let bg = ((f & 0x0004) << 24)     // ITALIC bit 2 -> 26
131        | ((f & 0x0002) << 26); // DIM    bit 1 -> 27
132    (content, fg, bg)
133}
134
135/// One character position: a base glyph, fg/bg colour references, and flags.
136/// Combining marks (#45) and an OSC 8 hyperlink (#46) attach via per-row maps,
137/// signalled by the `COMBINED_PRESENT` / `LINK_PRESENT` bits — the cell itself is
138/// three packed words, no `Option` field. All access is through the accessor seam
139/// (#44); construct with [`Cell::from_parts`] or [`Cell::default`].
140///
141/// `Eq` is a derived bitwise compare, which is exact because the packing is
142/// canonical — every logical cell maps to one bit pattern (unused bits stay 0).
143#[derive(Clone, Copy, PartialEq, Eq)]
144pub struct Cell {
145    content: u32,
146    fg: u32,
147    bg: u32,
148}
149
150impl Default for Cell {
151    fn default() -> Self {
152        // The packed form of a blank cell: ' ' (U+0020) in the codepoint field,
153        // every other word zero (Default colours, no flags, no combining/link
154        // bits). Built directly rather than through `from_parts` so scroll/erase
155        // blanking — which constructs defaults by the rowful — stays a cheap copy.
156        Cell {
157            content: ' ' as u32,
158            fg: 0,
159            bg: 0,
160        }
161    }
162}
163
164impl core::fmt::Debug for Cell {
165    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
166        f.debug_struct("Cell")
167            .field("c", &self.c())
168            .field("fg", &self.fg())
169            .field("bg", &self.bg())
170            .field("flags", &self.flags())
171            .field("combined", &self.is_combined())
172            .field("linked", &self.is_linked())
173            .finish()
174    }
175}
176
177impl Cell {
178    /// Assemble a cell from its logical parts. The single construction seam —
179    /// `Pen::cell` and the wire decoder funnel through here, so the bit-packing
180    /// lives in exactly one place (#44).
181    pub fn from_parts(c: char, fg: Color, bg: Color, flags: CellFlags) -> Self {
182        let mut cell = Cell {
183            content: c as u32, // a `char` is <= U+10FFFF, so it fits the 21-bit field
184            fg: pack_color(fg),
185            bg: pack_color(bg),
186        };
187        cell.store_flags(flags);
188        cell
189    }
190
191    /// Replace the flag bits across the three words from `flags`, preserving the
192    /// codepoint, colours, and the dormant presence bits. The inverse is
193    /// [`Cell::flags`].
194    fn store_flags(&mut self, flags: CellFlags) {
195        let (content, fg, bg) = flag_words(flags.bits() as u32);
196        self.content = (self.content & !CONTENT_MARKER_MASK) | content;
197        self.fg = (self.fg & !FG_FLAG_MASK) | fg;
198        self.bg = (self.bg & !BG_FLAG_MASK) | bg;
199    }
200
201    /// The base code point.
202    pub fn c(&self) -> char {
203        char::from_u32(self.content & CODEPOINT_MASK)
204            .expect("codepoint bits always hold a valid char")
205    }
206
207    /// The foreground colour reference.
208    pub fn fg(&self) -> Color {
209        unpack_color(self.fg)
210    }
211
212    /// The background colour reference.
213    pub fn bg(&self) -> Color {
214        unpack_color(self.bg)
215    }
216
217    /// The cell's flags (SGR attributes + layout markers), reassembled from the
218    /// three words — the branchless inverse of [`Cell::store_flags`].
219    pub fn flags(&self) -> CellFlags {
220        let bits = ((self.content & CONTENT_MARKER_MASK) >> 14)         // 22,23,24 -> 8,9,10
221            | ((self.fg & FG_BOLD) >> 27)                              // 27 -> 0
222            | ((self.fg & FG_INVERSE) >> 21)                           // 26 -> 5
223            | ((self.fg & (FG_UNDERLINE | FG_BLINK)) >> 25)            // 28,29 -> 3,4
224            | ((self.fg & (FG_HIDDEN | FG_STRIKE)) >> 24)              // 30,31 -> 6,7
225            | ((self.bg & BG_ITALIC) >> 24)                           // 26 -> 2
226            | ((self.bg & BG_DIM) >> 26); // 27 -> 1
227        CellFlags::from_bits_retain(bits as u16)
228    }
229
230    /// Does this column carry combining marks? When true, the cluster lives in
231    /// the row's combining map at this column (#45) — a flag-gated cache: never
232    /// read the map without first checking this bit.
233    pub fn is_combined(&self) -> bool {
234        self.content & C_COMBINED != 0
235    }
236
237    /// Does this column carry an OSC 8 hyperlink? When true, the hyperlink-pool
238    /// index lives in the row's link map at this column (#46) — flag-gated like
239    /// combining: never read the link map without first checking this bit.
240    pub fn is_linked(&self) -> bool {
241        self.bg & BG_LINK != 0
242    }
243
244    /// Overwrite the base code point, preserving the layout markers.
245    pub fn set_c(&mut self, c: char) {
246        self.content = (self.content & !CODEPOINT_MASK) | c as u32;
247    }
248
249    /// Overwrite the background colour (the BCE erase fill, #16), preserving the
250    /// bg-word flag bits.
251    pub fn set_bg(&mut self, bg: Color) {
252        self.bg = pack_color(bg) | (self.bg & !(COLOR_VALUE_MASK | (0b11 << COLOR_MODE_SHIFT)));
253    }
254
255    /// Mark (or unmark) this column as carrying combining marks in the row map.
256    pub fn set_combined(&mut self, on: bool) {
257        if on {
258            self.content |= C_COMBINED;
259        } else {
260            self.content &= !C_COMBINED;
261        }
262    }
263
264    /// Mark (or unmark) this column as carrying an OSC 8 hyperlink in the row map.
265    pub fn set_linked(&mut self, on: bool) {
266        if on {
267            self.bg |= BG_LINK;
268        } else {
269            self.bg &= !BG_LINK;
270        }
271    }
272
273    /// Add the given flags (leaving the others set). Sets the word bits directly —
274    /// no round-trip through `flags()`/`store_flags`.
275    pub fn insert_flags(&mut self, flags: CellFlags) {
276        let (content, fg, bg) = flag_words(flags.bits() as u32);
277        self.content |= content;
278        self.fg |= fg;
279        self.bg |= bg;
280    }
281
282    /// Clear the given flags (leaving the others as they are).
283    pub fn remove_flags(&mut self, flags: CellFlags) {
284        let (content, fg, bg) = flag_words(flags.bits() as u32);
285        self.content &= !content;
286        self.fg &= !fg;
287        self.bg &= !bg;
288    }
289
290    /// Reset to a blank default cell.
291    pub fn reset(&mut self) {
292        *self = Cell::default();
293    }
294
295    /// Is this the lead cell of a width-2 glyph? Direct content-bit query — the
296    /// hot overwrite/erase/reflow paths use this instead of reconstructing the
297    /// full `flags()` to test one marker.
298    pub fn is_wide(&self) -> bool {
299        self.content & C_WIDE != 0
300    }
301
302    /// Is this the trailing spacer cell of a width-2 glyph?
303    pub fn is_wide_spacer(&self) -> bool {
304        self.content & C_SPACER != 0
305    }
306
307    /// Is this the blank column vacated when a wide glyph wrapped off the right
308    /// edge (#113)? It holds no character; unlike a trailing spacer it has no
309    /// wide lead to its left, so only the *text* extractors skip it.
310    pub fn is_leading_spacer(&self) -> bool {
311        self.content & C_LEADING_SPACER != 0
312    }
313
314    /// Does this column hold no text — either half of a wide glyph's trailing
315    /// spacer or a wide-wrap leading spacer? Used by the text extractors (search,
316    /// selection text, logical lines) to skip non-character columns.
317    pub fn is_spacer(&self) -> bool {
318        self.content & (C_SPACER | C_LEADING_SPACER) != 0
319    }
320
321    /// Mark this (blank) column as the leading spacer of a wrapped wide glyph.
322    pub fn set_leading_spacer(&mut self) {
323        self.content |= C_LEADING_SPACER;
324    }
325
326    /// Did this row soft-wrap into the next (WRAPLINE on its last cell)?
327    pub fn is_wrapline(&self) -> bool {
328        self.content & C_WRAP != 0
329    }
330}
331
332#[cfg(test)]
333mod tests {
334    use super::{Cell, CellFlags};
335    use crate::color::Color;
336
337    /// Size pin: slice C moves `link` out of the cell into the row's link map (a
338    /// cell now signals a hyperlink with only the `LINK_PRESENT` bg bit), so `Cell`
339    /// is **12 bytes** — three packed `u32` words, matching xterm.js's `BufferLine`
340    /// cell. This is the epic's target (#43): combining and link both ride per-row
341    /// maps, the cell is pure packed words. Flood throughput is
342    /// memory-bandwidth-bound, so this size is touched on every print/scroll-blank.
343    /// [#42, #46]
344    #[test]
345    fn cell_is_12_bytes() {
346        assert_eq!(std::mem::size_of::<Cell>(), 12);
347    }
348
349    /// The packing must be lossless: every colour reference read back equal in
350    /// both the fg and bg word, including the tag-distinguished trio that must not
351    /// collapse (`Default` / `Indexed(0)` / `Rgb(0,0,0)`).
352    #[test]
353    fn every_colour_round_trips_in_both_words() {
354        let colours = [
355            Color::Default,
356            Color::Indexed(0),
357            Color::Indexed(255),
358            Color::Rgb(0, 0, 0),
359            Color::Rgb(255, 128, 1),
360        ];
361        for &fg in &colours {
362            for &bg in &colours {
363                let cell = Cell::from_parts('x', fg, bg, CellFlags::empty());
364                assert_eq!(cell.fg(), fg, "fg {fg:?} / bg {bg:?}");
365                assert_eq!(cell.bg(), bg, "fg {fg:?} / bg {bg:?}");
366            }
367        }
368    }
369
370    /// Every flag bit — SGR attributes (split across the fg/bg words) and the
371    /// layout markers (in the content word) — round-trips, alone and combined.
372    #[test]
373    fn every_flag_round_trips() {
374        let all = CellFlags::all();
375        for bit in all.iter() {
376            let cell = Cell::from_parts('x', Color::Default, Color::Default, bit);
377            assert_eq!(cell.flags(), bit, "single {bit:?}");
378        }
379        let cell = Cell::from_parts('x', Color::Default, Color::Default, all);
380        assert_eq!(cell.flags(), all, "all flags at once");
381    }
382
383    /// The codepoint occupies 21 bits — the full Unicode range, up to the
384    /// maximum scalar value, survives alongside flags set in the same word.
385    #[test]
386    fn codepoint_round_trips_to_the_unicode_max() {
387        for c in ['a', ' ', '한', '🦀', '\u{10FFFF}'] {
388            let cell = Cell::from_parts(c, Color::Default, Color::Default, CellFlags::WIDE_CHAR);
389            assert_eq!(cell.c(), c, "codepoint {c:?}");
390            assert!(cell.flags().contains(CellFlags::WIDE_CHAR));
391        }
392    }
393
394    /// The combining-presence bit (content word) and link-presence bit (bg word)
395    /// are independent of each other, of the codepoint/markers, of the colours, and
396    /// of the SGR flags — toggling one must disturb none of the others.
397    #[test]
398    fn combined_and_linked_bits_are_independent() {
399        let mut cell = Cell::from_parts(
400            'e',
401            Color::Indexed(3),
402            Color::Rgb(1, 2, 3),
403            CellFlags::WIDE_CHAR | CellFlags::DIM,
404        );
405        assert!(!cell.is_combined());
406        assert!(!cell.is_linked());
407
408        cell.set_combined(true);
409        cell.set_linked(true);
410        assert!(cell.is_combined() && cell.is_linked());
411        // Everything else survives both bits being set.
412        assert_eq!(cell.c(), 'e');
413        assert_eq!(cell.fg(), Color::Indexed(3));
414        assert_eq!(
415            cell.bg(),
416            Color::Rgb(1, 2, 3),
417            "link bit shares the bg word"
418        );
419        assert!(cell.flags().contains(CellFlags::WIDE_CHAR | CellFlags::DIM));
420
421        cell.set_linked(false);
422        assert!(cell.is_combined() && !cell.is_linked());
423        cell.set_combined(false);
424        assert!(!cell.is_combined() && !cell.is_linked());
425        assert_eq!(
426            cell.bg(),
427            Color::Rgb(1, 2, 3),
428            "bg colour intact after clearing"
429        );
430
431        let spacer = Cell::from_parts(
432            ' ',
433            Color::Default,
434            Color::Default,
435            CellFlags::WIDE_CHAR_SPACER,
436        );
437        assert!(spacer.is_wide_spacer());
438        assert!(!Cell::default().is_wide_spacer());
439    }
440}