Skip to main content

justerm_core/
grid.rs

1//! The grid — the 2D array of cells representing the current screen.
2//!
3//! Rows are stored as separate `Vec`s (not one flat buffer) so the scrollback
4//! ring (a later slice) can move whole rows in/out cheaply.
5
6use crate::cell::Cell;
7use crate::color::Color;
8use std::collections::BTreeMap;
9use std::ops::{Deref, DerefMut};
10use std::sync::Arc;
11
12/// A row's combining clusters: column → the combining marks attached to that
13/// column's base glyph. Sparse (most rows have none) and **flag-gated** — an
14/// entry is only ever read when the cell at that column has its
15/// `COMBINED_PRESENT` bit set (xterm's `_combined` invariant, #45). Stale entries
16/// left by an overwrite/erase are therefore harmless; only live entries must be
17/// carried when cells move column (ICH/DCH/reflow).
18type Combining = BTreeMap<usize, Vec<char>>;
19
20/// A row's hyperlinks: column → the URI itself, shared (OSC 8). Same per-row,
21/// flag-gated sparse-map design as [`Combining`], gated by the cell's `LINK_PRESENT`
22/// bit instead (xterm's `_extendedAttrs` / `HAS_EXTENDED`, #46).
23///
24/// **The value is the URI, not an index into a buffer-wide pool (#628).** It was an
25/// index until then, and that pool was never reclaimed — which is the same defect the
26/// combining map had and lost when #45 deleted `grapheme_pool` for exactly this shape.
27/// Links kept the pool only because #46 mirrored xterm's `_dataByLinkId` registry and
28/// ported the `_nextId++` half without the delete half.
29///
30/// `Arc<str>` rather than `String` because cells genuinely share a URI: one OSC 8 open
31/// covering a thousand cells is a thousand map entries pointing at one allocation, which
32/// is the sharing the pool existed to provide. It dies with the last row that holds it —
33/// no release path, no refcount of our own, no sweep. Alacritty's `Arc<HyperlinkInner>`
34/// is the same choice; `Rc` is **not** an option, because `Engine` is `Send + Sync` and
35/// `Rc` would remove that silently.
36type Links = BTreeMap<usize, Arc<str>>;
37
38/// A row's non-default underline colours (SGR 58, #520): column → the underline
39/// `Color` reference. Same per-row, flag-gated sparse-map design as [`Links`],
40/// gated by the cell's `UCOLOR_PRESENT` bit. Only non-`Default` colours get an
41/// entry — a `Default` underline follows the fg and needs no storage.
42type UColors = BTreeMap<usize, Color>;
43
44/// Every **extended attribute** live at one column — the family that rides the
45/// row's flag-gated side maps rather than the 12-byte cell: the OSC 8 hyperlink
46/// (#46) and the SGR 58 underline colour (#520). Combining marks are deliberately
47/// *not* here: they are content, re-attached mark-by-mark through
48/// [`Row::push_combining`], not carried as an opaque value.
49///
50/// It exists so a path that *moves* or *grows* a cell carries the whole family in
51/// one step ([`Row::ext_attrs_at`] → [`Row::set_ext_attrs`]) instead of naming each
52/// rider — the same shape as xterm.js's `_copyCellMapsFrom`, which re-keys
53/// `_combined` and `_extendedAttrs` together for every cell `copyCellsFrom` moves.
54/// Adding a rider (an underline *style*, say) is a field here plus the two arms
55/// below; every carry site is covered by construction (#521).
56/// **Not `Copy` since #628.** The link rider went from a `NonZeroU32` pool index to a
57/// shared `Arc<str>`, so the family is `Clone` only; the carry sites clone it, which is
58/// a refcount bump and is what makes the reclamation automatic.
59#[derive(Clone, Debug, Default, PartialEq, Eq)]
60pub(crate) struct ExtAttrs {
61    link: Option<Arc<str>>,
62    ucolor: Option<Color>,
63}
64
65impl ExtAttrs {
66    /// The family as the *pen* currently holds it — the other source besides a cell
67    /// (`Row::ext_attrs_at`). Every print-path site that stamps a freshly built cell
68    /// goes through here, so the gating rules live in one place and a later rider is
69    /// added once (#521/#528).
70    pub(crate) fn from_pen(link: Option<Arc<str>>, ucolor: Option<Color>) -> ExtAttrs {
71        ExtAttrs { link, ucolor }
72    }
73}
74
75/// Re-key a sparse column map to follow a `copy_within(src, dst)` cell shift: the
76/// live entry for a moved cell travels to the cell's new column. Vacated source
77/// keys whose cell loses its gate bit are left stale — harmless under the
78/// flag-gate — so only the live carry is done. Generic over the value type so the
79/// combining and link maps share one implementation.
80fn move_map<V>(map: &mut BTreeMap<usize, V>, src: std::ops::Range<usize>, dst: usize) {
81    if map.is_empty() {
82        return;
83    }
84    let start = src.start;
85    let moved: Vec<(usize, V)> = src
86        .filter_map(|s| map.remove(&s).map(|v| (dst + (s - start), v)))
87        .collect();
88    for (col, v) in moved {
89        map.insert(col, v);
90    }
91}
92
93/// One row of cells **plus** its per-row, column-keyed combining, link, and
94/// underline-colour maps.
95///
96/// The maps ride with the row through scroll / scrollback / reflow for free (the
97/// row is the unit that moves), which is why combining (#45), hyperlinks (#46),
98/// and underline colours (#520) live here rather than in global per-cell indices —
99/// no leak, cleared on row reuse. `Row` derefs to `[Cell]`, so index/iterate/slice
100/// sites are unchanged; the maps are reached through the dedicated methods so the
101/// flag-gate (read iff the cell's `COMBINED_PRESENT` / `LINK_PRESENT` /
102/// `UCOLOR_PRESENT` bit is set) is never bypassed.
103///
104/// **No `#[non_exhaustive]` (#844), and the open question here is not the attribute.** A consumer
105/// can build one — the derived `Default` is a public constructor — but `Row` has no public method
106/// and appears in no public signature, so nothing can be done with the value. What wants deciding
107/// is why it is re-exported at all.
108#[derive(Clone, Debug, PartialEq, Eq, Default)]
109pub struct Row {
110    cells: Vec<Cell>,
111    combining: Combining,
112    links: Links,
113    ucolors: UColors,
114    /// Did this row soft-wrap (auto-wrap) into the next one?
115    ///
116    /// A property of the **row**, and stored on the row for a reason: it used to ride
117    /// `CellFlags::WRAPLINE` in the last cell, where every whole-cell write and clear destroyed it
118    /// — ordinary typing in the last column silently split the logical line (#538). Here no cell
119    /// operation can reach it. Both references keep it off the cell too, though the field is not
120    /// the same: ghostty's `Row.wrap` is this exact flag (the row wraps *into* the next), while
121    /// xterm.js's `BufferLine.isWrapped` is the opposite-polarity link (the row *continues* the
122    /// previous one) — ghostty's `wrap_continuation`, not its `wrap`. The distinction matters when
123    /// borrowing xterm.js's `clearWrap` values, which describe the *previous* row's link.
124    ///
125    /// It still crosses the wire as the last cell's `WRAPLINE` bit, derived at encode time, so the
126    /// format is unchanged.
127    wrapped: bool,
128}
129
130impl Row {
131    /// A row of `cols` blank cells.
132    pub(crate) fn blank(cols: usize) -> Row {
133        Row {
134            cells: vec![Cell::default(); cols],
135            combining: Combining::new(),
136            links: Links::new(),
137            ucolors: UColors::new(),
138            wrapped: false,
139        }
140    }
141
142    /// Wrap a cell vector as a row with no combining marks, links, or ucolors.
143    pub(crate) fn from_cells(cells: Vec<Cell>) -> Row {
144        Row {
145            cells,
146            combining: Combining::new(),
147            links: Links::new(),
148            ucolors: UColors::new(),
149            wrapped: false,
150        }
151    }
152
153    /// Build a row from cells and its maps (the reflow re-split path).
154    pub(crate) fn new(
155        cells: Vec<Cell>,
156        combining: Combining,
157        links: Links,
158        ucolors: UColors,
159    ) -> Row {
160        Row {
161            cells,
162            combining,
163            links,
164            ucolors,
165            wrapped: false,
166        }
167    }
168
169    /// Consume the row into its cells, combining map, link map, and ucolor map
170    /// (the reflow join path).
171    pub(crate) fn into_parts(self) -> (Vec<Cell>, Combining, Links, UColors) {
172        (self.cells, self.combining, self.links, self.ucolors)
173    }
174
175    /// Resize to `cols`, padding with blanks or truncating; map entries for
176    /// dropped columns are pruned (xterm's shrink-prune).
177    pub(crate) fn resize(&mut self, cols: usize) {
178        self.cells.resize(cols, Cell::default());
179        if self
180            .combining
181            .keys()
182            .next_back()
183            .is_some_and(|&m| m >= cols)
184        {
185            self.combining.retain(|&col, _| col < cols);
186        }
187        if self.links.keys().next_back().is_some_and(|&m| m >= cols) {
188            self.links.retain(|&col, _| col < cols);
189        }
190        if self.ucolors.keys().next_back().is_some_and(|&m| m >= cols) {
191            self.ucolors.retain(|&col, _| col < cols);
192        }
193    }
194
195    /// Empty the row, keeping the cell allocation — for recycling a row buffer
196    /// (`scroll_up_recycle`). Clears cells and both maps so a reused row never
197    /// surfaces a previous occupant's marks or links.
198    /// Every URI this row's map still **owns**, gate or no gate.
199    ///
200    /// Deliberately ungated, and that is the whole point: every public reader goes
201    /// through `LINK_PRESENT`, so an entry left behind by an in-place erase is invisible
202    /// to all of them. A test written against the gated reader therefore counts *linked
203    /// cells* and cannot fail for retention — measured, it read 0 while the URIs were
204    /// still allocated. Test-only observability for the one property the gate hides.
205    ///
206    /// Yields one item per **column**, not per distinct URI — a link over three cells is
207    /// three entries sharing one allocation, so a caller counting URIs must dedupe by
208    /// `Arc::as_ptr`. Stated because getting that wrong is a false failure, not a false
209    /// pass: it was measured at 9 for a four-line buffer holding four links.
210    #[cfg(test)]
211    pub(crate) fn owned_links(&self) -> impl Iterator<Item = &Arc<str>> {
212        self.links.values()
213    }
214
215    /// Drop every side-map entry in `cols`, without touching the cells.
216    ///
217    /// The companion to a blanking write. `Cell::reset` clears the presence bits, and
218    /// under [rule 3](../../docs/map/invariant/row-keyed-side-maps.md) that is all it
219    /// *owes* — a stale entry is unreadable through the gate, so purging is an
220    /// optimisation and never the correctness step. That stays true; what changed is the
221    /// price of skipping it. Since #628 the link map owns an `Arc<str>` and the combining
222    /// map owns a `Vec<char>`, so an entry left behind by an in-place erase retains a heap
223    /// allocation until the row is reused — invisible to every public reader, because they
224    /// are all gated on the bit the erase just cleared.
225    ///
226    /// All three references release here: alacritty's `Cell::reset` drops its
227    /// `Option<Arc<CellExtra>>` outright, ghostty's ref-counted set frees at zero, and
228    /// xterm.js's `_resetBufferLine` clears `_extendedAttrs` and disposes the line's
229    /// markers so `OscLinkService` deletes the entry.
230    pub(crate) fn purge_side_maps(&mut self, cols: core::ops::Range<usize>) {
231        for col in cols {
232            self.combining.remove(&col);
233            self.links.remove(&col);
234            self.ucolors.remove(&col);
235        }
236    }
237
238    pub(crate) fn clear(&mut self) {
239        self.cells.clear();
240        self.combining.clear();
241        self.links.clear();
242        self.ucolors.clear();
243        self.wrapped = false;
244    }
245
246    /// Blank this row **in place** — every cell reset, and every row-scoped property with them.
247    ///
248    /// The distinction from a cell loop is the whole point. Soft-wrap is a property of the row
249    /// (#538), so `for cell in row { cell.reset() }` leaves a blanked row still claiming to
250    /// continue into the next one — and because the row *struct* is what scroll rotates and what
251    /// the alt grid keeps, that stale claim outlives the content it described. Blanking is one
252    /// operation so a caller cannot blank half of a row's state; a future row-scoped field is
253    /// covered by construction, the same way `Row::clear` covers the side maps for a recycled
254    /// buffer.
255    ///
256    /// Keeps the cell allocation and the row's width — unlike [`Row::clear`], which empties the
257    /// `Vec` for a buffer about to be re-fitted.
258    pub(crate) fn blank_in_place(&mut self) {
259        for cell in self.cells.iter_mut() {
260            cell.reset();
261        }
262        self.wrapped = false;
263    }
264
265    /// Did this row soft-wrap into the next one? See [`Row::wrapped`] for why this is a row
266    /// property and not a cell flag (#538).
267    pub(crate) fn is_wrapped(&self) -> bool {
268        self.wrapped
269    }
270
271    /// Mark (or unmark) this row as soft-wrapped into the next.
272    ///
273    /// Unmarking is per-verb, not derivable from what was erased — see `Term::end_wrap`, which is
274    /// the only place that unmarks and carries the rule with its references. An *overwrite* of the
275    /// last column must leave it set (that was the whole point of #538: a cell write cannot decide
276    /// a row property), and so must a leftward erase.
277    pub(crate) fn set_wrapped(&mut self, wrapped: bool) {
278        self.wrapped = wrapped;
279    }
280
281    /// The combining marks at `col`, or `None`. Flag-gated: returns `Some` only
282    /// when the cell carries the `COMBINED_PRESENT` bit, so a stale map entry is
283    /// never surfaced.
284    pub(crate) fn combining_at(&self, col: usize) -> Option<&[char]> {
285        if self.cells[col].is_combined() {
286            self.combining.get(&col).map(Vec::as_slice)
287        } else {
288            None
289        }
290    }
291
292    /// The hyperlink URI at `col`, or `None`. Flag-gated by the cell's `LINK_PRESENT`
293    /// bit (mirror of [`Row::combining_at`]).
294    pub(crate) fn link_at(&self, col: usize) -> Option<&Arc<str>> {
295        if self.cells[col].is_linked() {
296            self.links.get(&col)
297        } else {
298            None
299        }
300    }
301
302    /// The non-default underline colour at `col`, or `None` (which the caller reads
303    /// as `Default` — follow the fg). Flag-gated by the cell's `UCOLOR_PRESENT` bit,
304    /// so a stale map entry an overwrite left behind is never surfaced (#520).
305    pub(crate) fn ucolor_at(&self, col: usize) -> Option<Color> {
306        if self.cells[col].is_ucolored() {
307            self.ucolors.get(&col).copied()
308        } else {
309            None
310        }
311    }
312
313    /// Attach a combining mark to `col`'s glyph. The first mark on a cell starts a
314    /// fresh cluster — dropping any stale entry an overwrite left behind (the bit
315    /// was clear) — and sets the presence bit; subsequent marks append. Mirrors
316    /// xterm's `addCodepointToCell`.
317    pub(crate) fn push_combining(&mut self, col: usize, mark: char) {
318        if self.cells[col].is_combined() {
319            self.combining.entry(col).or_default().push(mark);
320        } else {
321            self.cells[col].set_combined(true);
322            self.combining.insert(col, vec![mark]);
323        }
324    }
325
326    /// Stamp `col`'s glyph with a hyperlink URI, setting the presence bit (the print
327    /// path calls this on every cell written while a link is open). The `Arc` clone is
328    /// a refcount bump, so a link over N cells is one allocation and N pointers.
329    pub(crate) fn set_link(&mut self, col: usize, link: Arc<str>) {
330        self.cells[col].set_linked(true);
331        self.links.insert(col, link);
332    }
333
334    /// Stamp `col`'s glyph with a non-default underline colour, setting the presence
335    /// bit (the print path calls this on every cell written while the pen's underline
336    /// colour is non-default, #520). Mirror of [`Row::set_link`].
337    pub(crate) fn set_ucolor(&mut self, col: usize, color: Color) {
338        self.cells[col].set_ucolored(true);
339        self.ucolors.insert(col, color);
340    }
341
342    /// Every extended attribute live at `col`, as one value (#521). Flag-gated per
343    /// rider, so a stale entry an overwrite left behind is never picked up.
344    pub(crate) fn ext_attrs_at(&self, col: usize) -> ExtAttrs {
345        ExtAttrs {
346            link: self.link_at(col).cloned(),
347            ucolor: self.ucolor_at(col),
348        }
349    }
350
351    /// Make `col` carry **exactly** `attrs` — each rider's presence bit and map
352    /// entry set together, or *both cleared*. Clearing matters as much as setting:
353    /// the promotion paths write over a column that may still hold a live entry, and
354    /// they build the new cell by copying one that may still carry a presence bit,
355    /// so "set what is there" alone would leave either half of the gate dangling
356    /// (#521).
357    pub(crate) fn set_ext_attrs(&mut self, col: usize, attrs: ExtAttrs) {
358        match attrs.link {
359            Some(link) => self.set_link(col, link),
360            None => {
361                self.cells[col].set_linked(false);
362                self.links.remove(&col);
363            }
364        }
365        match attrs.ucolor {
366            Some(color) => self.set_ucolor(col, color),
367            None => {
368                self.cells[col].set_ucolored(false);
369                self.ucolors.remove(&col);
370            }
371        }
372    }
373
374    /// Re-key every map to follow a `copy_within(src, dst)` cell shift (ICH/DCH),
375    /// so a cluster, link, or underline colour stays attached to its glyph at the
376    /// new column.
377    pub(crate) fn move_maps(&mut self, src: std::ops::Range<usize>, dst: usize) {
378        move_map(&mut self.combining, src.clone(), dst);
379        move_map(&mut self.links, src.clone(), dst);
380        move_map(&mut self.ucolors, src, dst);
381    }
382}
383
384impl Deref for Row {
385    type Target = [Cell];
386    fn deref(&self) -> &[Cell] {
387        &self.cells
388    }
389}
390
391impl DerefMut for Row {
392    fn deref_mut(&mut self) -> &mut [Cell] {
393        &mut self.cells
394    }
395}
396
397/// Re-wrap physical `rows` to `new_cols`. Soft-wrapped rows are joined into logical lines, then
398/// each logical line is re-split at `new_cols` with the wrap flag set on every segment but the
399/// last. Trailing blank rows are absorbed (re-created by the caller's row-count fit). See #7.
400///
401/// The flag is read from and written to the **`Row`**, not the last cell: soft wrap is a row
402/// property (#538) and `WRAPLINE` survives only as a wire bit derived at encode time.
403///
404/// `points` are `(row, col)` coordinates to track through the reflow — the cursor, any selection
405/// anchors, **and every OSC-133 command mark** — and the returned `Vec` maps each to its new
406/// position, index-aligned with the input. That last group is why the mapping is a single pass
407/// rather than a test inside the re-split loop: `points` scales with the number of commands in the
408/// buffer, and the loop scales with rows.
409///
410/// **A returned point is a position in the logical line, not necessarily a cell.** Two of its
411/// components deliberately leave the grid (#562), because a point that sits *just after* the last
412/// cell is a real place and the caller — not this function — knows what that means for the kind of
413/// point it holds:
414///
415/// - `col` may equal `new_cols`. The cursor reads that as the next write position (the row after);
416///   an OSC-133 mark reads it as an **exclusive** bound meaning "all of this row"; a selection
417///   anchor is clamped. Answering `(row + 1, 0)` here picked the cursor's reading for all three.
418/// - `row` may be **past the last row emitted**, for a point on a trailing blank line the join
419///   absorbed. Nothing extra is emitted for it: the row is one the caller's fit will create
420///   (`Grid::set_screen` pads at the bottom), and bounding it against `out.len()` here would clamp
421///   away a row that is about to exist. The bound belongs at the seam, against the final geometry.
422///
423/// **A wide pair straddling the new boundary *is* special-cased** — the re-split emits a short row
424/// rather than splitting the pair, and marks the column it vacates as the wrap artefact (#533). An
425/// earlier version of this comment said the opposite long after the guard landed, and the mapping
426/// below was written against that sentence: it divided the offset by `new_cols`, which is only
427/// right if every row is full (#549).
428///
429/// Common-90%: trailing blanks on a hard-ended row are trimmed by *content*, so a BCE-coloured
430/// tail does not re-split into a phantom row (#530).
431pub(crate) fn reflow(
432    rows: Vec<Row>,
433    new_cols: usize,
434    points: &[(usize, usize)],
435) -> (Vec<Row>, Vec<(usize, usize)>) {
436    // 1. Join soft-wrapped rows into logical lines, recording each tracked
437    //    point's logical coordinate (line index + offset within the line). The
438    //    combining map is carried alongside: a row's entries are re-keyed by the
439    //    join offset so a cluster stays attached to its glyph across the wrap.
440    let mut logical: Vec<Vec<Cell>> = Vec::new();
441    let mut logical_comb: Vec<Combining> = Vec::new();
442    let mut logical_links: Vec<Links> = Vec::new();
443    let mut logical_ucolors: Vec<UColors> = Vec::new();
444    let mut current: Vec<Cell> = Vec::new();
445    let mut current_comb: Combining = Combining::new();
446    let mut current_links: Links = Links::new();
447    let mut current_ucolors: UColors = UColors::new();
448    // Per point: (logical line, offset, found-yet).
449    let mut tracked: Vec<(usize, usize, bool)> = vec![(0, 0, false); points.len()];
450    for (i, row) in rows.into_iter().enumerate() {
451        for (pi, &(pr, pc)) in points.iter().enumerate() {
452            if i == pr && !tracked[pi].2 {
453                tracked[pi] = (logical.len(), current.len() + pc, true);
454            }
455        }
456        let soft = row.is_wrapped();
457        let base = current.len();
458        let (cells, comb, links, ucolors) = row.into_parts();
459        // Carry live map entries, re-keyed to the logical-line offset (flag-gated:
460        // a stale entry whose cell lost its bit is dropped).
461        for (col, marks) in comb {
462            if cells[col].is_combined() {
463                current_comb.insert(base + col, marks);
464            }
465        }
466        for (col, link) in links {
467            if cells[col].is_linked() {
468                current_links.insert(base + col, link);
469            }
470        }
471        for (col, color) in ucolors {
472            if cells[col].is_ucolored() {
473                current_ucolors.insert(base + col, color);
474            }
475        }
476        if soft {
477            let mut cells = cells;
478            // A wide char that wrapped at the boundary (write_glyph / relocate_cluster_wide) left a
479            // leading-spacer placeholder in the vacated last column. It is a wrap artefact, not
480            // content — drop it on the join so the logical line (and re-split) never carries a
481            // phantom blank into accessible_text / search / copy (#303). The `soft` flag was already
482            // read from this cell above, so removing it now is safe.
483            if cells.last().is_some_and(Cell::is_leading_spacer) {
484                cells.pop();
485            }
486            current.extend(cells);
487        } else {
488            let mut cells = cells;
489            // Trim the hard-ended line's trailing blanks by **content**, not by full-cell
490            // equality. A cell the app never wrote and one it erased to a coloured background
491            // (BCE) are both "no content" — reflow is finding where the logical line *ends*, and a
492            // background is not content. Comparing against `Cell::default()` kept a BCE tail on the
493            // line, so a narrowing resize re-split it into an extra row of coloured blanks the app
494            // never typed (a phantom row that steals from scrollback on a short screen). Both
495            // references trim on content only: xterm.js `getTrimmedLength` tests `HAS_CONTENT_MASK`,
496            // alacritty `line_length` tests `c != ' '` — and xterm keeps the background-aware
497            // variant a *separate* function for the callers (the DOM renderer) that want it, which
498            // reflow is not. This does not erase a cell that survives on screen (#530): it decides
499            // a line's length, it does not blank anything.
500            while cells.last().is_some_and(Cell::is_blank) {
501                cells.pop();
502            }
503            current.extend(cells);
504            logical.push(std::mem::take(&mut current));
505            logical_comb.push(std::mem::take(&mut current_comb));
506            logical_links.push(std::mem::take(&mut current_links));
507            logical_ucolors.push(std::mem::take(&mut current_ucolors));
508        }
509    }
510    if !current.is_empty() {
511        logical.push(current);
512        logical_comb.push(current_comb);
513        logical_links.push(current_links);
514        logical_ucolors.push(current_ucolors);
515    }
516    // Trailing blank lines are absorbed, not preserved as rows (the maps are
517    // trimmed in lockstep so all four stay index-aligned).
518    while logical.last().is_some_and(|l| l.is_empty()) {
519        logical.pop();
520        logical_comb.pop();
521        logical_links.pop();
522        logical_ucolors.pop();
523    }
524
525    // 2. Re-split each logical line into `new_cols`-wide rows, mapping each
526    //    tracked point to its new (row, col).
527    let mut out: Vec<Row> = Vec::new();
528    let mut new_points = vec![(0usize, 0usize); points.len()];
529    // Where each emitted row of the current logical line actually starts and how many content
530    // cells it actually holds: `(first offset, cells, row index)`. The re-split loop is the owner
531    // of that extent — it is the thing that decides `take` — so the point mapping below reads it
532    // instead of recomputing the position as `off / new_cols`, which silently assumes every row is
533    // full. It is not: the anti-split guard emits a **short** row whenever one would end on a
534    // `WIDE_CHAR` lead, and each such row shifted every later point by one, accumulating until the
535    // point crossed into a neighbouring row (#549, an ADR-0025 D1 read-side violation — the same
536    // "don't re-derive what the owner already knows" clause the wrap flag lives under).
537    //
538    // All three references decide the position where the real extent is known, and none divides an
539    // offset by the new width:
540    //
541    // - **xterm.js precomputes exactly this array** — `reflowSmallerGetNewLineLengths`
542    //   (`common/buffer/BufferReflow.ts:179` @ `699f553`), whose doc names the reason: *"pre-compute
543    //   the wrapping points since wide characters may need to be wrapped onto the following line …
544    //   will only contain the values `newCols` … and `newCols - 1` (when the line does end with a
545    //   wide character), except for the last value"*. That is this `Vec`, in the reference.
546    // - **ghostty** moves a tracked pin by assignment from the write cursor's live position inside
547    //   its reflow loop (`terminal/PageList.zig:1650-1659` @ `e6e26e1`) — its `tracked_pins` is the
548    //   closest analogue of `points` (anchors *and* marks, not just the cursor).
549    // - **alacritty** re-anchors the cursor on the iteration that processes its own line, against
550    //   `num_wrapped` (`alacritty_terminal/src/grid/resize.rs:169-188` @ `852e971`).
551    //
552    // (xterm.js also skips the cursor's wrapped run in the *larger* path, but that is gated on its
553    // `reflowCursorLine` option — `BufferReflow.ts:45`, `Buffer.ts:337`/`:370`/`:391` — so it is a
554    // policy, not a refusal.)
555    //
556    // Held outside the loop and cleared per line, so this costs one allocation. Mapped in a single
557    // pass afterwards rather than tested per segment: `points` carries every OSC-133 command mark
558    // in the buffer, and the per-segment shape would be rows × points. Note what that does **not**
559    // claim — it is not faster than the arithmetic it replaces. That was `O(points)` per logical
560    // line and this is too (the `pl != li` filter below is the dominant term either way); measured
561    // on 8000 marks over 8000 lines, a narrow-then-widen resize is identical within noise.
562    let mut segments: Vec<(usize, usize, usize)> = Vec::new();
563    for (li, line) in logical.iter().enumerate() {
564        let comb = &logical_comb[li];
565        let links = &logical_links[li];
566        let ucolors = &logical_ucolors[li];
567        let start = out.len();
568        segments.clear();
569        if line.is_empty() {
570            out.push(Row::blank(new_cols));
571        } else {
572            let mut i = 0;
573            while i < line.len() {
574                let mut take = (line.len() - i).min(new_cols);
575                // Don't split a wide char from its spacer: if the row would end
576                // on a WIDE_CHAR lead, drop it to the next row (xterm's newCols-1).
577                let vacates_for_wide = i + take < line.len() && line[i + take - 1].is_wide();
578                if vacates_for_wide {
579                    take -= 1;
580                }
581                // `take == 0` is reachable only at `new_cols == 1`, and #547 made that width
582                // unreachable: `MIN_COLUMNS = 2` floors every entry into `Term::resize`, this
583                // function's only caller. The guard stays anyway, because what it prevents is a
584                // *hang*, not a wrong cell — at `take == 0` this loop never advances `i`.
585                // xterm.js documents the identical failure at the identical width
586                // ("Calling this with a `newCols` value of `1` will lock up.",
587                // `common/buffer/BufferReflow.ts:173`), so the cost of one `max` is well spent
588                // on the day someone adds a second caller. Valid as long as `MIN_COLUMNS >= 2`.
589                let take = take.max(1);
590                // Segment maps: entries in [i, i+take) re-keyed to col - i.
591                let seg_comb: Combining = comb
592                    .range(i..i + take)
593                    .map(|(&col, marks)| (col - i, marks.clone()))
594                    .collect();
595                let seg_links: Links = links
596                    .range(i..i + take)
597                    .map(|(&col, link)| (col - i, link.clone()))
598                    .collect();
599                let seg_ucolors: UColors = ucolors
600                    .range(i..i + take)
601                    .map(|(&col, &color)| (col - i, color))
602                    .collect();
603                let mut row =
604                    Row::new(line[i..i + take].to_vec(), seg_comb, seg_links, seg_ucolors);
605                row.resize(new_cols);
606                // Reflow is a *producer* of the wide-wrap artefact, so it owes the artefact's
607                // marker — the column just vacated is a blank the text extractors must skip, not
608                // a space the app typed. Without it a resize injects a phantom space into copy,
609                // search and accessible text (#533). alacritty marks the same cell at both of its
610                // equivalent sites (`grid/resize.rs:155-157` grow, `:293-297` shrink, the latter
611                // `mem::replace`-ing the last column with a `LEADING_WIDE_CHAR_SPACER`); ghostty
612                // sets `.wide = .spacer_head` (`PageList.zig:1767`). The cell stays a **default**
613                // blank: unlike the print path (#528), reflow has no pen — it is a re-split of
614                // rows that already exist — and all three references build it from defaults.
615                if vacates_for_wide && take < new_cols {
616                    row.cells[new_cols - 1].set_leading_spacer();
617                }
618                segments.push((i, take, out.len()));
619                i += take;
620                if i < line.len() {
621                    row.set_wrapped(true);
622                }
623                out.push(row);
624            }
625        }
626        for (pi, &(pl, poff, _)) in tracked.iter().enumerate() {
627            if pl != li {
628                continue;
629            }
630            let off = poff.min(line.len());
631            new_points[pi] = match segments.last() {
632                // The empty-line branch emits one blank row and runs no segment loop, so the only
633                // offset a point can have here is 0.
634                None => (start, 0),
635                Some(&(last_off, last_take, last_row)) if off >= last_off + last_take => {
636                    // `off == line.len()`: the point sits *after* the last cell, so no segment
637                    // contains it — parked past the content rather than on a glyph. The honest
638                    // answer is the column just after the last one, and when that row came out
639                    // **full** it is `new_cols` — a column the grid does not have.
640                    //
641                    // Returned anyway, because the three kinds of point want different things from
642                    // it and this function cannot know which it holds (#562): the cursor wants the
643                    // next *write* position (the row after), an OSC-133 mark wants an **exclusive**
644                    // bound meaning "all of this row" (`extract_lines` clips `[b, c)`), and a
645                    // selection anchor wants to be clamped inside the grid. Answering `(row + 1, 0)`
646                    // here picked the cursor's answer for all three, which put a mark on the first
647                    // row of the *next logical line* and made it swallow that line's newline.
648                    // `Term::resize` resolves it per kind at the seam.
649                    //
650                    // ghostty splits **two** of the three the same way inside its own reflow: a
651                    // non-cursor pin is clamped before it can widen anything, the cursor pin never
652                    // is (`terminal/PageList.zig:1576-1606` @ `e6e26e1`). The mark's reading has no
653                    // prior art there and is derived here — ghostty's clamp puts a pin strictly
654                    // *inside* the destination and then widens the row to include it, the opposite
655                    // of a bound sitting outside the grid, and it has no column-bearing semantic
656                    // mark to want one (`semantic_prompt` is a row property, `:1573`). The nearest
657                    // reference for "one past is representable" is xterm.js's `x === cols`, which
658                    // is its **cursor**. Derived, not ported: `extract_lines` clips `[b, c)`, so the
659                    // exclusive end is the only value that can mean "all of this row".
660                    (last_row, last_take)
661                }
662                Some(_) => {
663                    // Segments tile `[0, line.len())` in order, so the one holding `off` is the
664                    // last whose start is `<= off`.
665                    let k = segments.partition_point(|&(s, _, _)| s <= off) - 1;
666                    let (seg_off, _, seg_row) = segments[k];
667                    (seg_row, off - seg_off)
668                }
669            };
670        }
671    }
672    // A point whose logical line was a **trailing blank** keeps its distance from the content, in
673    // lines. The join absorbs those lines rather than emitting them, so the row named here is one
674    // this function never produced — and that is correct: `reflow` does not own the row count. Its
675    // caller's fit does (`Grid::set_screen` pads blank rows at the bottom), and the bound belongs
676    // there too, against the *final* geometry rather than against `out.len()`.
677    //
678    // Clamping it here instead collapsed the cursor onto the last content row, so the next byte
679    // overwrote the content it should have followed (#562 symptom 2). The earlier guard also
680    // clamped a point that was merely one row past — a row the fit was about to create — which is
681    // how a resize folded the cursor back onto the last glyph and destroyed it (symptom 3).
682    //
683    // Nothing is materialised for this, and ghostty is the precedent — but for a narrower reason
684    // than "a blank row is free". It **defers** the row (`if (!src_row.wrap_continuation)
685    // self.new_rows += 1; return;`, `terminal/PageList.zig:1610-1616` @ `e6e26e1`) and *pays the
686    // debt by scrolling* the moment a non-blank row follows (`while (self.new_rows > 0)
687    // cursorScrollOrNewPage(...)`, `:1634-1637`). What is free is specifically a blank row with
688    // nothing after it — its own comment: *"so that blank rows at the end of the page list are
689    // never written"*. That is exactly this case, because the join only absorbs **trailing** blank
690    // lines. A port that emitted a real row here instead would pay out of the active area, and on a
691    // pane with no scrollback to absorb the displaced one — the alt screen — that is content
692    // destruction. Measured: 22 alt lines became 21.
693    for (pi, &(pl, poff, _)) in tracked.iter().enumerate() {
694        if pl >= logical.len() {
695            // Clamped **below** `new_cols`, not to it. `col == new_cols` is the "just past a full
696            // row" signal the seam reads, and an absorbed line is blank — it has no full row for
697            // the cursor to be just past. Clamping to `new_cols` made the signal fall out of
698            // ordinary arithmetic: a cursor parked one column further left stayed on its row while
699            // one column further right jumped a whole row (measured at width 4, parked columns 3
700            // and 4). A value that carries meaning must not also be an upper bound.
701            new_points[pi] = (
702                out.len() + (pl - logical.len()),
703                poff.min(new_cols.saturating_sub(1)),
704            );
705        }
706    }
707
708    (out, new_points)
709}
710
711/// The current screen: `rows` × `cols` cells.
712///
713/// **No `#[non_exhaustive]` (#844): nothing outside this crate has a reason to build one.** No
714/// public function accepts it — the engine hands it out — and there are zero out-of-crate literal
715/// sites, so the attribute would bind nothing it does not already bind.
716#[derive(Clone, Debug)]
717pub struct Grid {
718    cols: usize,
719    rows: usize,
720    lines: Vec<Row>,
721}
722
723impl Grid {
724    /// A blank grid of the given size.
725    pub fn new(cols: usize, rows: usize) -> Self {
726        let lines = vec![Row::blank(cols); rows];
727        Grid { cols, rows, lines }
728    }
729
730    pub fn cols(&self) -> usize {
731        self.cols
732    }
733
734    pub fn rows(&self) -> usize {
735        self.rows
736    }
737
738    /// Did `row` soft-wrap (auto-wrap) into the next one — i.e. are the two rows one logical
739    /// line?
740    ///
741    /// Ask this, not the last cell's `WRAPLINE` flag: soft-wrap is a property of the row and is
742    /// stored there, so a cell never carries it on a live grid (#538). The flag still appears on
743    /// the *wire*, derived onto a span's last cell at encode time, which is a different layer —
744    /// see `docs/architecture.md` §Cell on the two things called "cell" here.
745    pub fn is_row_wrapped(&self, row: usize) -> bool {
746        self.lines[row].is_wrapped()
747    }
748
749    /// Read a cell. Panics on out-of-bounds (callers clamp to the grid).
750    pub fn cell(&self, row: usize, col: usize) -> &Cell {
751        &self.lines[row][col]
752    }
753
754    /// Mutable access to a cell.
755    pub fn cell_mut(&mut self, row: usize, col: usize) -> &mut Cell {
756        &mut self.lines[row][col]
757    }
758
759    /// Read a whole row.
760    pub fn row(&self, row: usize) -> &[Cell] {
761        &self.lines[row]
762    }
763
764    /// Read a whole row including its combining map — for combining-aware reads
765    /// (text extraction, serialization).
766    pub(crate) fn row_ref(&self, row: usize) -> &Row {
767        &self.lines[row]
768    }
769
770    /// Mutable access to a whole row (cells + combining map) — for in-row cell
771    /// shifts (ICH/DCH), which must re-key combining alongside the cell move.
772    pub(crate) fn row_mut(&mut self, row: usize) -> &mut Row {
773        &mut self.lines[row]
774    }
775
776    /// A clone of a whole row (cells + combining map) — for the sub-region scroll
777    /// eviction, which copies row 0 out to scrollback (the full-screen path moves
778    /// the row instead, via `scroll_up_recycle`).
779    pub(crate) fn row_owned(&self, row: usize) -> Row {
780        self.lines[row].clone()
781    }
782
783    /// Scroll the rows `[top..=bottom]` up by one line: the top line of the
784    /// region is dropped and a blank line appears at `bottom`. Rows outside the
785    /// region are untouched.
786    ///
787    /// `rotate_left` moves whole-row `Vec` *handles* (24 bytes each), not cell
788    /// data — cheap even at the screen's bounded row count, so the per-newline
789    /// scrollback cost lives in the *eviction*, not here (see `scroll_up_recycle`
790    /// and ADR-0009).
791    pub fn scroll_up_region(&mut self, top: usize, bottom: usize) {
792        // Rotate the region's top line to its bottom, then blank it: every line
793        // in the region shifts up one and the region's bottom becomes empty.
794        self.lines[top..=bottom].rotate_left(1);
795        self.lines[bottom].blank_in_place();
796    }
797
798    /// Full-screen scroll up that **moves** the evicted top row out instead of
799    /// copying it (`Term::linefeed`'s hot path): `rotate_left` puts logical row 0
800    /// in the bottom slot, then a recycled `blank` is swapped into that slot and
801    /// the evicted row returned by value (the caller pushes it into scrollback).
802    /// The grid clears + fits `blank` to `cols`, so the caller may hand it a
803    /// dirty recycled row — reusing its allocation, so a steady-state flood does
804    /// no per-line alloc/copy (ADR-0009). No ring: the win is recycling the row
805    /// buffer, not making the cheap handle-rotate O(1).
806    pub(crate) fn scroll_up_recycle(&mut self, mut blank: Row) -> Row {
807        blank.clear(); // drop any recycled content (keeps the allocation)
808        blank.resize(self.cols);
809        self.lines.rotate_left(1); // logical row 0 -> the bottom slot
810        let last = self.rows - 1;
811        std::mem::replace(&mut self.lines[last], blank)
812    }
813
814    /// Extract all rows, leaving the grid empty. Used by `Term::resize` to
815    /// reflow the screen together with scrollback as one stream.
816    pub(crate) fn take_lines(&mut self) -> Vec<Row> {
817        std::mem::take(&mut self.lines)
818    }
819
820    /// Replace the screen with `lines` at `cols` x `rows`: each row is fit to
821    /// `cols` and the screen is padded with blank rows / truncated to `rows`.
822    pub(crate) fn set_screen(&mut self, mut lines: Vec<Row>, cols: usize, rows: usize) {
823        for row in &mut lines {
824            row.resize(cols);
825        }
826        while lines.len() < rows {
827            lines.push(Row::blank(cols));
828        }
829        lines.truncate(rows);
830        self.lines = lines;
831        self.cols = cols;
832        self.rows = rows;
833    }
834
835    /// Reset every cell to a blank default. Used when switching to the alt
836    /// screen (which always starts cleared).
837    pub fn clear(&mut self) {
838        for row in &mut self.lines {
839            row.blank_in_place();
840        }
841    }
842
843    /// Scroll the rows `[top..=bottom]` down by one line: a blank line appears at
844    /// `top` and the bottom region line is dropped. Rows outside are untouched.
845    /// Used by RI (reverse index) at the top margin.
846    pub fn scroll_down_region(&mut self, top: usize, bottom: usize) {
847        // Rotate the region's bottom line to its top, then blank it: every line
848        // in the region shifts down one and the region's top becomes empty.
849        self.lines[top..=bottom].rotate_right(1);
850        self.lines[top].blank_in_place();
851    }
852}
853
854#[cfg(test)]
855mod tests {
856    use super::*;
857
858    /// A grid whose row `r` carries the char `'a' + r` in column 0 — a distinct
859    /// marker per logical row so a scroll's row mapping is observable.
860    fn stamped(cols: usize, rows: usize) -> Grid {
861        let mut g = Grid::new(cols, rows);
862        for r in 0..rows {
863            g.cell_mut(r, 0).set_c(char::from(b'a' + r as u8));
864        }
865        g
866    }
867
868    /// Column-0 chars read top-to-bottom in *logical* row order.
869    fn col0(g: &Grid) -> String {
870        (0..g.rows()).map(|r| g.cell(r, 0).c()).collect()
871    }
872
873    #[test]
874    fn full_screen_scroll_up_shifts_content_and_blanks_bottom() {
875        let mut g = stamped(2, 3); // logical col0 = "abc"
876        g.scroll_up_region(0, 2);
877        assert_eq!(col0(&g), "bc "); // shifted up, bottom blanked
878    }
879
880    #[test]
881    fn full_screen_scroll_down_shifts_content_and_blanks_top() {
882        // RI at the top margin: blank appears at the top, the bottom line is lost.
883        let mut g = stamped(2, 3); // "abc"
884        g.scroll_down_region(0, 2);
885        assert_eq!(col0(&g), " ab");
886    }
887
888    #[test]
889    fn sub_region_scroll_leaves_rows_outside_the_region_untouched() {
890        let mut g = stamped(2, 4); // "abcd"
891        g.scroll_up_region(0, 1); // sub-region [0..=1] only
892        // rows 0..=1 ("ab") scroll up → "b" then blank; rows 2,3 ("c","d") stay.
893        assert_eq!(col0(&g), "b cd");
894    }
895
896    #[test]
897    fn scroll_up_recycle_moves_out_row0_and_blanks_a_dirty_recycled_row() {
898        let mut g = stamped(2, 3); // "abc"
899        // Hand it a *dirty* recycled row (full width, stale content) — the new
900        // bottom must come out blank, not carrying the recycled row's text.
901        let mut x = Cell::default();
902        x.set_c('X');
903        let dirty = Row::from_cells(vec![x; 2]);
904        let evicted = g.scroll_up_recycle(dirty);
905        assert_eq!(evicted[0].c(), 'a'); // logical row 0 moved out, not copied
906        assert_eq!(col0(&g), "bc "); // shifted up; bottom blank, NOT "bcX"
907    }
908
909    #[test]
910    fn take_lines_returns_rows_in_logical_order_after_a_scroll() {
911        // `reflow` assumes logical row order; `take_lines` must deliver it.
912        let mut g = stamped(1, 3); // "abc"
913        g.scroll_up_region(0, 2); // "bc "
914        let lines = g.take_lines();
915        let got: String = lines.iter().map(|r| r[0].c()).collect();
916        assert_eq!(got, "bc ");
917    }
918
919    /// `set_ext_attrs` is "make this column carry **exactly** these attrs". The
920    /// clearing half is invisible through the public API — the flag-gate hides a
921    /// stale entry either way — so it is pinned here, at the primitive that owns
922    /// the guarantee: a caller handing it `None` must leave neither a set presence
923    /// bit nor a readable map entry behind (#521).
924    #[test]
925    fn set_ext_attrs_clears_both_halves_of_the_gate() {
926        let mut row = Row::blank(2);
927        let link: Arc<str> = Arc::from("https://example.com/a");
928        row.set_link(0, link.clone());
929        row.set_ucolor(0, Color::Indexed(3));
930        assert_eq!(row.ext_attrs_at(0).link, Some(link));
931        assert_eq!(row.ext_attrs_at(0).ucolor, Some(Color::Indexed(3)));
932
933        row.set_ext_attrs(0, ExtAttrs::default());
934        assert!(!row.cells[0].is_linked(), "presence bit cleared");
935        assert!(!row.cells[0].is_ucolored(), "presence bit cleared");
936        assert!(row.links.is_empty(), "and the map entry with it");
937        assert!(row.ucolors.is_empty());
938        // Re-arming the bit by hand must not resurrect anything.
939        row.cells[0].set_linked(true);
940        row.cells[0].set_ucolored(true);
941        assert_eq!(row.ext_attrs_at(0), ExtAttrs::default());
942    }
943
944    /// The carry itself: reading a column's family and stamping it onto another
945    /// column reproduces both riders together — the one step the promotion paths
946    /// rely on so a future rider needs no new call site (#521).
947    #[test]
948    fn ext_attrs_round_trip_from_one_column_to_another() {
949        let mut row = Row::blank(2);
950        let link: Arc<str> = Arc::from("https://example.com/b");
951        row.set_link(0, link.clone());
952        row.set_ucolor(0, Color::Rgb(1, 2, 3));
953        let carried = row.ext_attrs_at(0);
954        row.set_ext_attrs(1, carried.clone());
955        assert_eq!(row.link_at(1), Some(&link));
956        assert_eq!(row.ucolor_at(1), Some(Color::Rgb(1, 2, 3)));
957        assert_eq!(row.ext_attrs_at(1), carried);
958        // The carry shares the allocation rather than copying the URI — the property
959        // that makes a link over a thousand cells cost one string (#628).
960        assert!(
961            Arc::ptr_eq(row.link_at(0).unwrap(), row.link_at(1).unwrap()),
962            "both columns must point at the same allocation, not equal copies",
963        );
964    }
965}