uncurses 0.0.8

A Rust terminal rendering library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Per-line transform algorithm — the diff loop and its core helpers
//! (`transform_line`, `transform_line_taken`, `transform_line_inner`).

use std::io::{self, Write};

use super::predicates::can_clear_with;
use crate::ansi;
use crate::cell::Cell;
use crate::renderer::caps::Optimizations;
use crate::renderer::{RenderBuffer, Renderer};

impl Renderer {
    /// Transform a single line — the core diff algorithm.
    ///
    /// Compares old vs new cells and writes the minimal escape sequence to update.
    /// `y` is the 0-based row; `first_x`/`last_x` are 0-based column bounds (inclusive).
    pub(crate) fn transform_line(
        &mut self,
        out: &mut Vec<u8>,
        new_buf: &RenderBuffer,
        y: u16,
        first_x: u16,
        last_x: u16,
    ) -> io::Result<()> {
        // Pull cur_buf out of self so the inner pass can borrow a row
        // mutably while still calling &mut self methods on the renderer.
        // None of the methods reached from transform_line_inner touch
        // self.cur_buf, so this is safe.
        let mut cur_buf = self.cur_buf.take();
        let result = self.transform_line_taken(out, new_buf, &mut cur_buf, y, first_x, last_x);
        self.cur_buf = cur_buf;
        result
    }

    fn transform_line_taken(
        &mut self,
        out: &mut Vec<u8>,
        new_buf: &RenderBuffer,
        cur_buf: &mut Option<RenderBuffer>,
        y: u16,
        first_x: u16,
        last_x: u16,
    ) -> io::Result<()> {
        let cur_line = cur_buf.as_mut().and_then(|cb| cb.line_mut(y));
        let first_diff = self.transform_line_inner(out, new_buf, cur_line, y, first_x, last_x)?;
        if let Some(first_diff) = first_diff
            && let Some(cur) = cur_buf.as_mut()
            && let (Some(new_line), Some(cur_line)) = (new_buf.line(y), cur.line_mut(y))
        {
            let len = cur_line.len().min(new_line.len());
            if first_diff < cur_line.len() && first_diff < new_line.len() {
                cur_line[first_diff..len].clone_from_slice(&new_line[first_diff..len]);
            } else if len > 0 {
                // Reference falls back to refreshing the entire short
                // line from index 0 when first_diff is past one of the
                // lines, so cur_buf stays consistent with the screen
                // when a resize leaves cur_line shorter than the new
                // line and the emission already covered the leading
                // columns.
                cur_line[..len].clone_from_slice(&new_line[..len]);
            }
        }
        Ok(())
    }

    /// Transform a single row, mirroring the canonical algorithm's
    /// structure: anchor on first/last non-blank columns of old and new
    /// lines, then dispatch to one of five branches based on what kind
    /// of change pattern the row exhibits.
    ///
    /// Returns the first column of the suffix the wrapper must copy into
    /// `cur_buf`. Returns `None` when the row is unchanged or this method
    /// already copied the changed interval.
    fn transform_line_inner(
        &mut self,
        out: &mut Vec<u8>,
        new_buf: &RenderBuffer,
        mut cur_line: Option<&mut [Cell]>,
        y: u16,
        _first_x: u16,
        _last_x: u16,
    ) -> io::Result<Option<usize>> {
        let width = new_buf.width() as usize;
        let new_line = match new_buf.line(y) {
            Some(l) => l,
            None => return Ok(None),
        };
        if width == 0 || new_line.is_empty() {
            return Ok(None);
        }

        // Scroll operations can change the old buffer outside the touched
        // span, so compare the full row before reusing its equal prefix.
        let mut first_diff = 0;
        while first_diff < width {
            let new_cell = &new_line[first_diff];
            let old_cell = cur_line.as_deref().and_then(|line| line.get(first_diff));
            if old_cell.is_none_or(|old| old != new_cell) {
                break;
            }
            first_diff += 1;
        }
        if first_diff == width {
            return Ok(None);
        }

        // === Step 0: give up on the diff at a cluster of uncertain width ===
        //
        // Every step below plans from how many columns it believes each cell
        // takes, and a cluster the terminal draws narrower than the sum of
        // its parts breaks that belief for itself and for every column to
        // its right. The row is repainted from there instead.
        //
        // This comes first because the leading-blank branch of step 1 emits
        // an erase of its own. That erase reaches the columns a ligated
        // cluster was drawn into, and no later step would redraw what it
        // wiped.
        if let Some(bail) = self.uncertain_bail(new_line, cur_line.as_deref(), first_diff) {
            let cur_slice = cur_line.as_deref();
            // Columns left of the cluster are measured the way the terminal
            // draws them, so an ordinary comparison still holds there.
            let first = first_diff.min(bail);
            let first = super::emit::cluster_start(new_line, first);
            if first < bail {
                self.move_to(out, new_buf, y, first as u16)?;
                self.put_range(out, new_buf, cur_slice, new_line, y, first, bail - 1)?;
            }
            self.repaint_tail(out, new_buf, new_line, y, bail)?;
            return Ok(Some(first));
        }

        // === Step 1: find firstCell ===
        // When the new row begins with cells that the terminal can
        // reproduce by erasing (default-style blanks), we may be able
        // to use EL-1 to wipe a leading run. Otherwise reuse the first
        // differing column found above.
        let leading_blank: &Cell = &new_line[0];
        let mut first_cell;
        // `copy_from` is the leftmost column where the post-emission
        // screen matches `new_line`. The wrapper uses it to copy
        // new_line[copy_from..] into cur_buf. For most paths it equals
        // first_cell, but the EL-1 emission also clears columns to the
        // left of first_cell, so copy_from stays at the pre-erase
        // first_cell in that case.
        let mut copy_from;
        if can_clear_with(leading_blank, self.opts.contains(Optimizations::BCE)) {
            let mut o_first = 0usize;
            if let Some(cur) = cur_line.as_deref() {
                while o_first < cur.len() && &cur[o_first] == leading_blank {
                    o_first += 1;
                }
            }
            let mut n_first = 0usize;
            while n_first < new_line.len() && &new_line[n_first] == leading_blank {
                n_first += 1;
            }

            if n_first == o_first {
                // Equal leading blanks leave the first difference unchanged.
                first_cell = first_diff;
                copy_from = first_cell;
            } else if o_first > n_first {
                // Old had more leading blanks; nothing to clear.
                first_cell = n_first;
                copy_from = first_cell;
            } else {
                // o_first < n_first: new row has a wider leading-blank
                // run than old. Consider EL-1 (or EL-0 if it covers
                // the whole row); fall through to the trailing logic
                // either way.
                first_cell = o_first;
                copy_from = first_cell;
                let leading = n_first - o_first;
                const EL1_COST: usize = 4; // ESC [ 1 K
                if EL1_COST < leading {
                    if n_first >= width {
                        self.move_to(out, new_buf, y, 0)?;
                        self.update_pen(out, Some(leading_blank))?;
                        ansi::screen::write_erase_to_eol(out)?;
                    } else {
                        self.move_to(out, new_buf, y, (n_first - 1) as u16)?;
                        self.update_pen(out, Some(leading_blank))?;
                        out.write_all(ansi::screen::ERASE_LINE_LEFT)?;
                    }
                    // Mirror the cleared region into the borrowed old
                    // line so subsequent comparisons (notably the
                    // walk-back in step 4c) see the same screen state
                    // the terminal has after the erase. BCE paints
                    // bg-only into the cleared cells, so model the
                    // fill that way rather than copying leading_blank
                    // verbatim (which would carry fg / attrs the wire
                    // never received). Split-borrow on `self.cur` is
                    // disjoint from `cur_line` (already split off
                    // above), so the bce_blank ref lives directly.
                    let bce = self.opts.contains(Optimizations::BCE);
                    let bce_fill: &Cell = self.cur.bce_blank(bce);
                    if let Some(cur) = cur_line.as_deref_mut() {
                        let end = n_first.min(cur.len());
                        if first_cell < end {
                            crate::buffer::fill_line_into(&mut cur[first_cell..end], bce_fill);
                        }
                    }
                    // After the erase the cleared columns match `new`;
                    // advance firstCell past them so the trailing logic
                    // can focus on the rest of the row. copy_from stays
                    // at the pre-erase column so the wrapper also
                    // refreshes cur_buf for the cleared region.
                    first_cell = n_first;
                }
            }
        } else {
            first_cell = first_diff;
            copy_from = first_cell;
        }

        if first_cell >= width {
            return Ok(None);
        }

        // The scan compares columns, so it can stop on the second half of a
        // cluster: a continuation whose lead matched. Emission begins by
        // placing the cursor at this column, and a continuation writes no
        // bytes and moves no cursor, so the next glyph would go out a column
        // to the left of where it belongs. Backing up to the column that
        // owns the cell re-emits the whole cluster instead.
        //
        // `copy_from` follows, because the wrapper mirrors
        // `new_line[copy_from..]` into the back buffer. Left where it was,
        // the re-emitted lead would never be recorded and the same
        // difference would be found again on every later frame.
        let owner = super::emit::cluster_start(new_line, first_cell);
        if owner != first_cell {
            first_cell = owner;
            copy_from = copy_from.min(owner);
        }

        // After the correction above, the leftmost column emission starts
        // from owns the cell it holds. Every branch below places a cursor
        // there, and a continuation writes no bytes and moves no cursor.
        debug_assert!(
            !new_line[first_cell].is_continuation()
                || first_cell == 0
                || !new_line[first_cell - 1].is_wide(),
            "first_cell {first_cell} on row {y} points at a continuation a cell owns"
        );

        let cur_slice = cur_line.as_deref();

        // === Step 2: trailing-uncolorable fast path ===
        //
        // When the rightmost cell of the new row is non-default
        // (colored / styled / non-space), the terminal can't safely
        // wipe a trailing region with EL or DCH — those fill with the
        // current pen, which would visibly alter the right margin. In
        // that case just walk backward to find the last differing
        // column and overwrite the changed range.
        let trailing = &new_line[width - 1];
        if !can_clear_with(trailing, self.opts.contains(Optimizations::BCE)) {
            let mut n_last = width - 1;
            while n_last > first_cell {
                let new_c = &new_line[n_last];
                let old_c = cur_slice.and_then(|c| c.get(n_last));
                if old_c.is_some_and(|o| o == new_c) {
                    n_last -= 1;
                } else {
                    break;
                }
            }
            // Close the walk-back over its own cluster. Stopping on the
            // continuation of a lead that matched leaves the diff blind
            // to the lead itself: the equal-run scan in `put_range`
            // starts one column before the continuation, sees a
            // continuation on both sides, and skips a lead that may
            // have moved between frames.
            n_last = super::emit::cluster_end(new_line, n_last);
            if n_last >= first_cell {
                self.move_to(out, new_buf, y, first_cell as u16)?;
                self.put_range(out, new_buf, cur_slice, new_line, y, first_cell, n_last)?;
            }
            // The backward scan already proved the suffix equal. Copy only
            // this interval; a shorter old row uses the wrapper's fallback.
            if let Some(cur) = cur_line
                && let Some(changed) = cur.get_mut(copy_from..=n_last)
            {
                changed.clone_from_slice(&new_line[copy_from..=n_last]);
                return Ok(None);
            }
            return Ok(Some(copy_from));
        }

        // === Step 3: locate last non-blank cells on each side ===
        let blank: &Cell = trailing;
        let mut o_last = width.saturating_sub(1);
        while o_last > first_cell
            && cur_slice
                .and_then(|c| c.get(o_last))
                .is_some_and(|c| c == blank)
        {
            o_last -= 1;
        }
        let mut n_last = width - 1;
        while n_last > first_cell && &new_line[n_last] == blank {
            n_last -= 1;
        }

        // Close the walk-back over the cluster that owns its stopping
        // point. If the last non-blank column happens to be a lead, its
        // continuation lies past `n_last`, and every branch below that
        // treats `n_last + 1` as the cursor's resting column after the
        // range would name a spot inside the cluster the lead spans.
        // The mirror of the DCH branch's cursor-off-by-one, at the outer
        // range boundary.
        n_last = super::emit::cluster_end(new_line, n_last);
        if let Some(cur) = cur_slice {
            o_last = super::emit::cluster_end(cur, o_last);
        }

        let el0 = self.el0_cost();

        if n_last == first_cell && el0 < o_last.saturating_sub(n_last) {
            // === Step 4a: single non-blank cell + erase the rest ===
            self.move_to(out, new_buf, y, first_cell as u16)?;
            if &new_line[first_cell] != blank {
                // The single non-blank column may be the lead of a wide
                // cluster whose continuation sits at `first_cell + 1`.
                // Close the range so the emitter sees the whole glyph
                // and its cursor lands past it, not on the second half.
                let last = super::emit::cluster_end(new_line, first_cell);
                self.emit_range(out, new_buf, new_line, first_cell, last, false)?;
            }
            self.clear_to_end(out, cur_slice, blank, width, false)?;
        } else if n_last != o_last && new_line.get(n_last) != cur_slice.and_then(|c| c.get(o_last))
        {
            // === Step 4b: last non-blank cells differ in both
            // position and value — overwrite a range, optionally
            // followed by EL when the old row is meaningfully longer.
            self.move_to(out, new_buf, y, first_cell as u16)?;
            if o_last > n_last && o_last - n_last > el0 {
                let eoi =
                    self.put_range(out, new_buf, cur_slice, new_line, y, first_cell, n_last)?;
                if eoi {
                    self.move_to(out, new_buf, y, (n_last + 1) as u16)?;
                }
                self.clear_to_end(out, cur_slice, blank, width, false)?;
            } else {
                let n = n_last.max(o_last);
                self.put_range(out, new_buf, cur_slice, new_line, y, first_cell, n)?;
            }
        } else {
            // === Step 4c: tail walk-back + ICH/DCH ===
            //
            // The last non-blank cells match either in position or in
            // value. Walk back over matching cells (in lockstep) to
            // find where the lines really diverge, then either insert
            // or delete the difference in line length with ICH/DCH.
            let n_last_nonblank = n_last;
            let o_last_nonblank = o_last;
            let mut n_lc = n_last as isize;
            let mut o_lc = o_last as isize;
            fn cell_at_isize(line: &[Cell], i: isize) -> Option<&Cell> {
                if i < 0 { None } else { line.get(i as usize) }
            }
            let cur_at_isize =
                |i: isize| -> Option<&Cell> { cur_slice.and_then(|c| cell_at_isize(c, i)) };
            // Treat out-of-bounds slots as unequal so the walk-back
            // stops at the line boundary rather than stepping past it.
            // Returning false when either side is absent makes the loop
            // break at At(-1) instead of decrementing both indices to
            // -1.
            fn cells_eq(a: Option<&Cell>, b: Option<&Cell>) -> bool {
                matches!((a, b), (Some(x), Some(y)) if x == y)
            }
            while cells_eq(cell_at_isize(new_line, n_lc), cur_at_isize(o_lc)) {
                if !cells_eq(cell_at_isize(new_line, n_lc - 1), cur_at_isize(o_lc - 1)) {
                    break;
                }
                n_lc -= 1;
                o_lc -= 1;
                if n_lc == -1 || o_lc == -1 {
                    break;
                }
            }

            let n = o_lc.min(n_lc);
            let insert_at = (n + 1) as usize;
            if o_lc < n_lc && super::emit::cluster_start(new_line, insert_at) < insert_at {
                // Painting through this cluster would overwrite the suffix
                // before insertion shifts it. Repaint instead; an optimized
                // insert needs a shared cluster boundary for both operations.
                self.move_to(out, new_buf, y, first_cell as u16)?;
                self.put_range(
                    out,
                    new_buf,
                    cur_slice,
                    new_line,
                    y,
                    first_cell,
                    n_last_nonblank.max(o_last_nonblank),
                )?;
                return Ok(Some(copy_from));
            }
            if n >= first_cell as isize {
                // Emit the whole cluster at the range's end. The deletion
                // branch below uses the same boundary for its cursor move.
                let last = super::emit::cluster_end(new_line, n as usize);
                self.move_to(out, new_buf, y, first_cell as u16)?;
                self.put_range(out, new_buf, cur_slice, new_line, y, first_cell, last)?;
            }

            if o_lc < n_lc {
                // Insertion: new row has more non-blank cells than old.
                let m = n_last_nonblank.max(o_last_nonblank);
                // Wide-cell adjustment: ICH must land on a cell
                // boundary, never in the middle of a wide character.
                // If `n+1` would point at a continuation cell, shift
                // `n` so the cursor lands past the wide cell (advance)
                // when we're already at the leftmost column, or back
                // up before it otherwise.
                let mut n = n;
                let mut o_lc = o_lc;
                if n != 0 {
                    while n > 0 {
                        match cell_at_isize(new_line, n + 1) {
                            Some(w) if w.is_continuation() => {
                                n -= 1;
                                o_lc -= 1;
                            }
                            _ => break,
                        }
                    }
                }
                // Whichever way the walk went, the insert begins at `n + 1`,
                // and that column has to own the cell it holds. The
                // backward walk stops when it reaches column zero, and the
                // forward walk only ran for a lead, so either can leave the
                // insert pointing inside a glyph. Stepping past the cluster
                // moves `o_lc` with it, keeping the two rows in lockstep.
                while cell_at_isize(new_line, n + 1).is_some_and(|c| c.is_continuation()) {
                    n += 1;
                    o_lc += 1;
                }
                debug_assert!(
                    (n + 1) as usize >= new_line.len()
                        || !new_line[(n + 1) as usize].is_continuation(),
                    "ICH move_to({}) on row {y} lands inside a cluster",
                    n + 1
                );
                self.move_to(out, new_buf, y, (n + 1) as u16)?;
                let ich_count = (n_lc - o_lc) as usize;
                let ich_cost = ansi::cost::ich_cost(ich_count as u16);
                let span = (m as isize - n) as usize;
                // Prefer plain overwrite when ICH is available but the
                // walk-back ended before the last non-blank cell — there
                // is content past n_lc that ICH wouldn't cover and we'd
                // have to write anyway — or the ICH sequence costs more
                // than just emitting the m-n cells. Otherwise insert via
                // ICH or IRM depending on terminal support.
                if self.opts.contains(Optimizations::ICH)
                    && ((n_lc as usize) < n_last_nonblank || ich_cost > span)
                {
                    self.put_range(out, new_buf, cur_slice, new_line, y, (n + 1) as usize, m)?;
                } else {
                    self.insert_cells_op(out, &new_line[(n + 1) as usize..], ich_count)?;
                }
            } else if o_lc > n_lc {
                // Deletion: new row has fewer non-blank cells than old.
                //
                // Wide-cell adjustment: put_range emitted through `n`
                // in new, and if `new[n]` is a lead, its continuation
                // was painted too and the cursor rests past it, not at
                // `n + 1`. Moving to `n + 1` on a lead would step back
                // into the continuation of a glyph we just wrote, and
                // a DCH there would cut the glyph in half. Slide the
                // cursor to the column past the cluster, where the
                // wide-glyph emission already left it. The mirror of
                // the ICH branch's back-shift above, taken forward
                // because deletion pulls content leftward while
                // insertion pushes it right.
                let n_effective = if n >= 0 {
                    super::emit::cluster_end(new_line, n as usize) as isize
                } else {
                    n
                };
                debug_assert!(
                    (n_effective + 1) as usize >= new_line.len()
                        || !new_line[(n_effective + 1) as usize].is_continuation(),
                    "DCH move_to({}) on row {y} lands inside a cluster",
                    n_effective + 1
                );
                self.move_to(out, new_buf, y, (n_effective + 1) as u16)?;
                let dch_count = (o_lc - n_lc) as usize;
                let dch_cost = ansi::cost::dch_cost(dch_count as u16) as isize;
                let el_cost = ansi::cost::el_cost(0) as isize;
                let tail = n_last_nonblank as isize - (n_effective + 1);
                if !self.opts.contains(Optimizations::DCH) || dch_cost > el_cost + tail {
                    // (n_effective+1) may exceed n_last_nonblank when
                    // the cluster covers the last non-blank column;
                    // signed arithmetic keeps put_range a no-op for an
                    // inverted span, mirroring the pre-adjustment path.
                    if (n_effective + 1) <= n_last_nonblank as isize {
                        let eoi = self.put_range(
                            out,
                            new_buf,
                            cur_slice,
                            new_line,
                            y,
                            (n_effective + 1) as usize,
                            n_last_nonblank,
                        )?;
                        if eoi {
                            self.move_to(out, new_buf, y, (n_last_nonblank + 1) as u16)?;
                        }
                    }
                    self.clear_to_end(out, cur_slice, blank, width, false)?;
                } else {
                    self.update_pen(out, Some(blank))?;
                    ansi::screen::write_dch(out, dch_count as u16)?;
                }
            }
        }

        Ok(Some(copy_from))
    }
}