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