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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
//! Touch-tracked render buffer.
//!
//! [`RenderBuffer`] wraps the core cell [`Buffer`] with per-row touched
//! spans. Drawing code can update only the cells it changed, while the
//! renderer later visits touched spans and compares them against tracked
//! terminal state before emitting bytes.

use crate::cell::Cell;
use crate::layout::{Position, Rect};

use crate::buffer::{Bounded, Buffer, Surface, SurfaceMut};

/// Inclusive modified-column span for one row.
///
/// `first` and `last` are zero-based columns. A row with no touches is
/// represented by `None` in the owning [`RenderBuffer`], not by an empty
/// span.
#[derive(Debug, Clone, Copy)]
pub struct TouchedSpan {
    /// Leftmost touched column.
    pub first: u16,
    /// Rightmost touched column.
    pub last: u16,
}

/// Cell buffer with per-line dirty tracking for renderer diffs.
///
/// # Model
///
/// The embedded [`Buffer`] holds cell contents. The `touched` table says
/// which inclusive column span changed on each row since the last clear.
/// Mutating helpers update both structures; read-only helpers leave touch
/// state unchanged.
#[derive(Debug, Clone)]
pub struct RenderBuffer {
    /// Backing cell buffer.
    pub buffer: Buffer,
    touched: Vec<Option<TouchedSpan>>,
}

impl RenderBuffer {
    /// Create a render buffer with all lines initially untouched.
    ///
    /// # Parameters
    ///
    /// - `width`: number of columns.
    /// - `height`: number of rows.
    ///
    /// # Returns
    ///
    /// A blank buffer of the requested size with no touched rows.
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            buffer: Buffer::new(width, height),
            touched: vec![None; height as usize],
        }
    }

    /// Return the buffer width in cells.
    pub fn width(&self) -> u16 {
        self.buffer.width()
    }

    /// Return the buffer height in cells.
    pub fn height(&self) -> u16 {
        self.buffer.height()
    }

    /// Mark an inclusive column range on one row as touched.
    ///
    /// # Parameters
    ///
    /// - `y`: zero-based row.
    /// - `first`: leftmost touched column.
    /// - `last`: rightmost touched column.
    ///
    /// Out-of-bounds rows are ignored. The range is recorded as supplied;
    /// callers should pass in-bounds columns for precise later diffs.
    pub fn touch_line(&mut self, y: u16, first: u16, last: u16) {
        let y = y as usize;
        if y >= self.touched.len() {
            return;
        }
        match &mut self.touched[y] {
            Some(span) => {
                span.first = span.first.min(first);
                span.last = span.last.max(last);
            }
            None => {
                self.touched[y] = Some(TouchedSpan { first, last });
            }
        }
    }

    /// The column a write at `pos` starts changing.
    ///
    /// That is `pos.x` itself, unless `pos` holds a continuation whose owning
    /// primary lies further left: writing over a continuation blanks that
    /// primary, so the damage starts there. `Buffer::set` walks back over
    /// chained continuations to find it, and the damage has to walk with it,
    /// since stopping one column short leaves the diff blind to half of what
    /// the write changed.
    ///
    /// `Buffer::set` blanks a primary only when the walk lands on a cell wide
    /// enough to own the column, so the damage may only reach back that far
    /// under the same condition. A continuation beside a narrow cell, or one
    /// that reaches the edge, leaves its neighbour untouched and blanks at
    /// most the column written.
    ///
    /// This reads the row as it stands, so callers ask before writing: the
    /// write is what destroys the evidence.
    fn owned_from(&self, pos: Position) -> u16 {
        if pos.x == 0 {
            return pos.x;
        }
        if !self.buffer.cell(pos).is_some_and(Cell::is_continuation) {
            return pos.x;
        }
        let Some(line) = self.buffer.line(pos.y) else {
            return pos.x;
        };
        let mut pc = pos.x - 1;
        while pc > 0 && line[pc as usize].is_continuation() {
            pc -= 1;
        }
        if line[pc as usize].is_wide() {
            pc
        } else {
            pos.x
        }
    }

    /// The last column a write reaching `pos` changes.
    ///
    /// That is `pos.x` itself, unless `pos` holds a wide primary whose
    /// continuation lies past it: writing over a primary blanks the whole
    /// cluster it owns, so a column outside the write changes with it. The
    /// mirror of [`RenderBuffer::owned_from`], answering the same question on
    /// the other side, and needed for the same reason: a span that stops one
    /// column short leaves the diff blind to a cell the write rewrote, and the
    /// stale half of the cluster survives the frame.
    ///
    /// The reach is clamped to the row, because `Buffer::set` truncates a cell
    /// that does not fit and the span may not claim a column the row does not
    /// have. A narrow cell reaches no further than itself.
    ///
    /// A continuation reaches as far as the primary that owns it, because a
    /// write landing on one breaks the whole cluster and the buffer blanks
    /// every column the primary held.
    ///
    /// This reads the row as it stands, so callers ask before writing: the
    /// write is what destroys the evidence.
    fn owned_through(&self, pos: Position) -> u16 {
        let Some(line) = self.buffer.line(pos.y) else {
            return pos.x;
        };
        let Some(cell) = line.get(pos.x as usize) else {
            return pos.x;
        };
        let owner = if cell.is_continuation() && pos.x > 0 {
            let mut pc = pos.x - 1;
            while pc > 0 && line[pc as usize].is_continuation() {
                pc -= 1;
            }
            pc
        } else {
            pos.x
        };
        if !line[owner as usize].is_wide() {
            return pos.x;
        }
        owner
            .saturating_add(line[owner as usize].width() as u16 - 1)
            .min(self.width().saturating_sub(1))
            .max(pos.x)
    }

    /// The columns a row shift bounded by `bounds_right` actually changes.
    ///
    /// Both ends close over the clusters the shift breaks, the way a write's
    /// do: the shift can pull a continuation out from beside the primary that
    /// owns it, or carry a primary off and leave its continuation behind, and
    /// either half left standing has to be repainted even though it sits
    /// outside the region asked for. The row is read before the shift, which
    /// is what destroys the evidence.
    fn shift_span(&self, pos: Position, bounds_right: u16) -> (u16, u16) {
        let last = bounds_right.min(self.width()).saturating_sub(1);
        (
            self.owned_from(pos),
            self.owned_through(Position::new(last, pos.y)),
        )
    }

    /// Mark an entire line as touched. `y` is the 0-based row.
    pub fn touch_full_line(&mut self, y: u16) {
        if self.width() > 0 {
            self.touch_line(y, 0, self.width() - 1);
        }
    }

    /// Set a cell and mark its occupied columns touched if changed.
    ///
    /// # Parameters
    ///
    /// - `pos`: zero-based destination coordinate.
    /// - `cell`: cell to clone into the buffer.
    ///
    /// # Behavior
    ///
    /// Delegates wide-cell accounting to [`Buffer::set`]. If the new
    /// value equals the existing cell and fits the row, no touched span is
    /// recorded. An equal cell clipped by resize still passes through
    /// [`Buffer::set`] for normalization. When
    /// a wide cell is overwritten by a narrower cell, the touched span
    /// covers the whole cluster being broken, both the column written and
    /// the primary to its left that the write blanks. A write that lands on
    /// a wide neighbour reaches the continuation that neighbour owns, which
    /// can sit past the columns the write itself covers.
    ///
    /// A continuation is placed by the cell that owns it, so [`Buffer::set`]
    /// ignores one arriving on its own and this records no damage for it.
    pub fn set_cell(&mut self, pos: impl Into<Position>, cell: &Cell) {
        let pos = pos.into();
        // The buffer leaves the column as its owner wrote it, so there is
        // nothing for the diff to look at.
        if cell.is_continuation() {
            return;
        }
        // `Buffer::set` ignores a write outside the grid, and the damage has
        // to agree with it. `cell` answers `None` there, which reads as
        // changed, so the span would be recorded for a row nothing wrote to,
        // and `pos.x + width` can overflow reaching it.
        if pos.y >= self.height() || pos.x >= self.width() {
            return;
        }

        let existing = self.buffer.cell(pos);
        // Resize can preserve an equal primary whose columns no longer fit.
        let changed =
            existing.is_none_or(|e| e != cell) || u16::from(cell.width()) > self.width() - pos.x;

        if changed {
            let new_width = cell.width().max(1) as u16;
            let prev_width = existing.map(|e| e.width()).unwrap_or(0).max(1) as u16;
            let width = new_width.max(prev_width);
            let first_col = self.owned_from(pos);
            // `Buffer::set` truncates a cell that does not fit, so the span
            // must not claim a column the row does not have either. The
            // saturating add is what keeps a row as wide as `u16` can
            // describe from overflowing on the way to that clamp.
            let end_col = pos
                .x
                .saturating_add(width - 1)
                .min(self.width().saturating_sub(1));
            let last_col = self.owned_through(Position::new(end_col, pos.y));
            self.buffer.set(pos, cell);
            self.touch_line(pos.y, first_col, last_col);
        }
    }

    /// Get the touched span for one row.
    ///
    /// # Parameters
    ///
    /// - `y`: zero-based row.
    ///
    /// # Returns
    ///
    /// `Some(TouchedSpan)` when the row has touched columns, otherwise
    /// `None`.
    pub fn touched(&self, y: u16) -> Option<TouchedSpan> {
        self.touched.get(y as usize).copied().flatten()
    }

    /// Return whether any row has touched columns.
    pub fn has_changes(&self) -> bool {
        self.touched.iter().any(|t| t.is_some())
    }

    /// Count the number of touched lines.
    #[allow(dead_code)]
    pub fn touched_line_count(&self) -> usize {
        self.touched.iter().filter(|t| t.is_some()).count()
    }

    /// Clear all touched flags without changing cell contents.
    pub fn clear_touched(&mut self) {
        self.touched.fill(None);
    }

    /// Mark every row as touched across the full width.
    ///
    /// Rows in a zero-width buffer remain untouched because there are no
    /// columns to render.
    pub fn touch_all(&mut self) {
        let width = self.width();
        for y in 0..self.height() {
            self.touch_line(y, 0, width.saturating_sub(1));
        }
    }

    /// Resize the buffer and mark all rows touched.
    ///
    /// # Parameters
    ///
    /// - `width`: new column count.
    /// - `height`: new row count.
    ///
    /// Existing contents are preserved according to [`Buffer::resize`];
    /// newly exposed cells are blank.
    pub fn resize(&mut self, width: u16, height: u16) {
        self.buffer.resize(width, height);
        self.touched.resize(height as usize, None);
        self.touch_all();
    }

    /// Change height, retaining dirty spans and marking only added rows.
    pub(crate) fn resize_height(&mut self, height: u16) {
        let old_height = self.height();
        self.buffer.resize(self.width(), height);
        self.touched.resize(height as usize, None);
        for y in old_height..height {
            self.touch_line(y, 0, self.width().saturating_sub(1));
        }
    }

    /// Borrow one row.
    ///
    /// # Returns
    ///
    /// `Some(&[Cell])` for an in-bounds row, otherwise `None`.
    pub fn line(&self, y: u16) -> Option<&[Cell]> {
        self.buffer.line(y)
    }

    /// Get mutable line reference. `y` is the 0-based row. Does **not**
    /// mark the line as touched; the caller is responsible for tracking
    /// touch state when bypassing [`Self::set_cell`].
    pub fn line_mut(&mut self, y: u16) -> Option<&mut [Cell]> {
        self.buffer.line_mut(y)
    }

    /// Mutable reference to the cell at `pos`, marking the column
    /// span covered by the cell as touched. Returns `None` for
    /// out-of-bounds positions.
    ///
    /// Unlike [`Self::set_cell`], this does not compare against the
    /// existing cell — the touched span is extended unconditionally and
    /// `Renderer::sync_front` filters unchanged
    /// cells by value equality. Callers must not change
    /// [`Cell::width`] through this handle; use [`Self::set_cell`] for
    /// wide-cell writes that need continuation-column accounting.
    pub fn cell_mut(&mut self, pos: impl Into<Position>) -> Option<&mut Cell> {
        let pos = pos.into();
        if pos.y >= self.buffer.height() || pos.x >= self.buffer.width() {
            return None;
        }
        // Inspect width before the mutable borrow so wide cells touch
        // their continuation column. Width stays stable under the
        // documented contract (no width changes via this handle).
        let width = self
            .buffer
            .cell(pos)
            .map(|c| c.width().max(1) as u16)
            .unwrap_or(1);
        let end_col = pos.x + width - 1;
        self.touch_line(pos.y, pos.x, end_col);
        self.buffer.cell_mut(pos)
    }

    /// Iterate over touched lines with their row indices (0-based).
    #[allow(dead_code)]
    pub fn touched_lines(&self) -> impl Iterator<Item = (u16, TouchedSpan)> + '_ {
        self.touched
            .iter()
            .enumerate()
            .filter_map(|(i, t)| t.map(|span| (i as u16, span)))
    }

    /// Insert `n` lines at `y` within a bounded region.
    ///
    /// Freed rows are filled with `fill`. All rows in
    /// `[y, bounds_bottom)` are marked touched across the full width so
    /// the renderer redraws the affected region on the next frame.
    pub fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
        self.buffer.insert_lines(y, n, bounds_bottom, fill);
        let bottom = bounds_bottom.min(self.height());
        if y >= bottom || self.width() == 0 {
            return;
        }
        let last_col = self.width() - 1;
        for row in y..bottom {
            self.touch_line(row, 0, last_col);
        }
    }

    /// Delete `n` lines at `y` within a bounded region.
    ///
    /// Freed rows are filled with `fill`. All rows in
    /// `[y, bounds_bottom)` are marked touched across the full width so
    /// the renderer redraws the affected region on the next frame.
    pub fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
        self.buffer.delete_lines(y, n, bounds_bottom, fill);
        let bottom = bounds_bottom.min(self.height());
        if y >= bottom || self.width() == 0 {
            return;
        }
        let last_col = self.width() - 1;
        for row in y..bottom {
            self.touch_line(row, 0, last_col);
        }
    }

    /// Insert `n` cells at `pos` within a row-bounded region.
    ///
    /// Delegates to [`Buffer::insert_cells`], fills freed cells with
    /// `fill`, and marks the affected row span touched. The span closes over
    /// the clusters the shift breaks at either end, which
    /// [`RenderBuffer::shift_span`] works out from the row as it stands.
    #[allow(dead_code)]
    pub fn insert_cells(
        &mut self,
        pos: impl Into<Position>,
        n: u16,
        bounds_right: u16,
        fill: &Cell,
    ) {
        let pos = pos.into();
        let (first_col, last_col) = self.shift_span(pos, bounds_right);
        self.buffer.insert_cells(pos, n, bounds_right, fill);
        self.touch_line(pos.y, first_col, last_col);
    }

    /// Delete `n` cells at `pos` within a row-bounded region.
    ///
    /// Delegates to [`Buffer::delete_cells`], fills freed right-edge
    /// cells with `fill`, and marks the affected row span touched. The span
    /// closes over the clusters the shift breaks at either end, the way
    /// [`RenderBuffer::insert_cells`] does.
    #[allow(dead_code)]
    pub fn delete_cells(
        &mut self,
        pos: impl Into<Position>,
        n: u16,
        bounds_right: u16,
        fill: &Cell,
    ) {
        let pos = pos.into();
        let (first_col, last_col) = self.shift_span(pos, bounds_right);
        self.buffer.delete_cells(pos, n, bounds_right, fill);
        self.touch_line(pos.y, first_col, last_col);
    }
}

impl Bounded for RenderBuffer {
    fn bounds(&self) -> Rect {
        Rect::new(0, 0, self.width(), self.height())
    }
}

impl Surface for RenderBuffer {
    fn cell(&self, pos: Position) -> Option<&Cell> {
        self.buffer.cell(pos)
    }
}

impl SurfaceMut for RenderBuffer {
    fn set_cell(&mut self, pos: Position, cell: &Cell) {
        RenderBuffer::set_cell(self, pos, cell);
    }

    fn cell_mut(&mut self, pos: Position) -> Option<&mut Cell> {
        RenderBuffer::cell_mut(self, pos)
    }

    /// Bulk-fill via the underlying [`Buffer::fill_rect`] override and
    /// then touch every affected row once. This skips the per-cell
    /// `set_cell` dispatch and the per-cell touched-span widen the
    /// trait default would perform — at the cost of marking unchanged
    /// rows touched on no-op fills, which only causes the transform
    /// pass to re-check rows that didn't change.
    fn fill_rect(&mut self, rect: Rect, cell: &Cell) {
        let clipped = self.bounds().intersection(rect);
        if clipped.is_empty() {
            return;
        }
        // A fill starting on a continuation makes the buffer walk back to the
        // primary that owns it and blank from there, and a fill ending on a
        // primary blanks the continuation that primary owns, which lies past
        // the rect. Ask each row where its damage really begins and ends
        // before the fill rewrites the evidence.
        // `touch_line` only merges a span into the row's record, so it does not
        // care that the fill has not run yet.
        let last_col = clipped.right().saturating_sub(1);
        for y in clipped.top()..clipped.bottom() {
            let first = self.owned_from(Position::new(clipped.left(), y));
            let last = self.owned_through(Position::new(last_col, y));
            self.touch_line(y, first, last);
        }
        self.buffer.fill_rect(rect, cell);
    }

    /// Delegate to [`RenderBuffer::insert_lines`] for the row-swap fast
    /// path and the precise touched-span bookkeeping it performs.
    fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
        RenderBuffer::insert_lines(self, y, n, bounds_bottom, fill);
    }

    /// Delegate to [`RenderBuffer::delete_lines`] for the row-swap fast
    /// path and the precise touched-span bookkeeping it performs.
    fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
        RenderBuffer::delete_lines(self, y, n, bounds_bottom, fill);
    }

    /// Delegate to [`RenderBuffer::insert_cells`] for the in-place
    /// rotate fast path and the row-touch bookkeeping it performs.
    fn insert_cells(&mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell) {
        RenderBuffer::insert_cells(self, pos, n, bounds_right, fill);
    }

    /// Delegate to [`RenderBuffer::delete_cells`] for the in-place
    /// rotate fast path and the row-touch bookkeeping it performs.
    fn delete_cells(&mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell) {
        RenderBuffer::delete_cells(self, pos, n, bounds_right, fill);
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn resize_height_preserves_marks_and_reuses_storage() {
        let mut buffer = super::RenderBuffer::new(4, 5);
        buffer.set_cell((1, 1), &crate::cell::Cell::new("a", 1));
        let storage = buffer.touched.as_ptr();
        buffer.resize_height(3);
        buffer.resize_height(5);
        assert_eq!(buffer.touched.as_ptr(), storage);
        for (row, touched) in [false, true, false, true, true].into_iter().enumerate() {
            assert_eq!(buffer.touched(row as u16).is_some(), touched);
        }
        let span = buffer.touched(1).unwrap();
        assert_eq!((span.first, span.last), (1, 1));
        buffer.resize_height(2);
        buffer.resize_height(3);
        assert!(buffer.touched(2).is_some());
        assert_eq!(buffer.line(2).unwrap(), &[crate::cell::Cell::BLANK; 4]);
    }

    use super::*;

    /// Assert that every column the operation rewrote lies inside `span`.
    ///
    /// The row is compared cell by cell against how it stood beforehand, so
    /// the test does not depend on having guessed which columns move: a span
    /// that misses one the buffer changed leaves that column stale on screen,
    /// and this is what says so.
    fn assert_span_covers_changes(span: TouchedSpan, before: &[Cell], after: &[Cell], what: &str) {
        for (x, (b, a)) in before.iter().zip(after).enumerate() {
            if b == a {
                continue;
            }
            let x = x as u16;
            assert!(
                (span.first..=span.last).contains(&x),
                "{what}: column {x} changed but the recorded span is {}..={}",
                span.first,
                span.last,
            );
        }
    }

    #[test]
    fn test_new_render_buffer() {
        let rb = RenderBuffer::new(80, 24);
        assert_eq!(rb.width(), 80);
        assert_eq!(rb.height(), 24);
        assert!(!rb.has_changes());
    }

    /// A wide primary can sit two or more columns from the continuation
    /// being written over, which `Buffer::resize` and the row shifts both
    /// leave behind. `Buffer::set` walks back to it and blanks from there,
    /// so the damage span has to reach it too.
    #[test]
    fn damage_reaches_a_primary_blanked_through_chained_continuations() {
        let mut rb = RenderBuffer::new(6, 1);
        rb.set_cell((0, 0), &Cell::new("\u{4e16}", 2));
        rb.buffer.line_mut(0).unwrap()[2] = Cell::CONTINUATION;
        rb.clear_touched();

        let before = rb.buffer.cell(Position::new(0, 0)).unwrap().clone();
        rb.set_cell((2, 0), &Cell::new("A", 1));
        let after = rb.buffer.cell(Position::new(0, 0)).unwrap().clone();
        let span = rb.touched(0).expect("row 0 touched");

        assert_ne!(before, after, "precondition: the primary was blanked");
        assert_eq!(span.first, 0, "damage must reach the primary it blanked");
    }

    /// `Buffer::set` ignores a write outside the grid, so the damage must
    /// too. Without the bounds check the span is recorded for a row nothing
    /// wrote to, and a column near `u16::MAX` overflows computing `end_col`.
    #[test]
    fn a_write_outside_the_grid_records_no_damage() {
        let mut rb = RenderBuffer::new(6, 1);
        // Fill the row with continuations so the back-walk would run if the
        // guard above it ever let an out-of-bounds position through.
        rb.set_cell((0, 0), &Cell::new("\u{4e16}", 2));
        for x in 2..6 {
            rb.buffer.line_mut(0).unwrap()[x] = Cell::CONTINUATION;
        }
        rb.clear_touched();

        // x past the right edge, y in bounds: the exact shape the review names.
        rb.set_cell((6, 0), &Cell::new("A", 1));
        rb.set_cell((100, 0), &Cell::new("A", 1));
        rb.set_cell((u16::MAX, 0), &Cell::new("A", 1));
        assert!(
            !rb.has_changes(),
            "an out-of-bounds write must change nothing"
        );

        // y past the bottom edge too.
        rb.set_cell((0, 9), &Cell::new("A", 1));
        assert!(!rb.has_changes());
    }

    /// `Buffer::set` truncates a wide cell that does not fit, so the damage
    /// span must not claim the column past it, and the arithmetic reaching
    /// that clamp must survive the widest row a `u16` can describe.
    #[test]
    fn a_span_stops_at_the_last_column_of_the_row() {
        let mut rb = RenderBuffer::new(6, 1);
        rb.set_cell((5, 0), &Cell::new("\u{4e16}", 2));
        let span = rb.touched(0).expect("row 0 touched");
        assert_eq!(span.last, 5, "the span must stop at the last column");

        let mut rb = RenderBuffer::new(u16::MAX, 1);
        rb.set_cell((u16::MAX - 1, 0), &Cell::new("\u{4e16}", 2));
        let span = rb.touched(0).expect("row 0 touched");
        assert_eq!(span.last, u16::MAX - 1);
    }

    /// The walk back to a primary follows `Buffer::set`'s ownership rule,
    /// which acts only on a wide cell. A continuation nothing owns leaves its
    /// neighbour alone, so the damage may not claim it.
    #[test]
    fn damage_reaches_back_only_to_a_primary_that_owns_the_column() {
        let cases: [(&str, Vec<Cell>, u16); 2] = [
            (
                "a narrow neighbour is left alone",
                vec![Cell::new("a", 1), Cell::CONTINUATION, Cell::CONTINUATION],
                2,
            ),
            (
                "a continuation reaching the edge owns nothing",
                vec![Cell::CONTINUATION, Cell::CONTINUATION, Cell::CONTINUATION],
                2,
            ),
        ];
        for (name, cells, want_first) in cases {
            let mut rb = RenderBuffer::new(6, 1);
            for (x, c) in cells.iter().enumerate() {
                rb.buffer.line_mut(0).unwrap()[x] = c.clone();
            }
            rb.clear_touched();
            rb.set_cell((2, 0), &Cell::new("A", 1));
            let span = rb.touched(0).expect("row 0 touched");
            assert_eq!(span.first, want_first, "{name}");
        }
    }

    /// A wide write covers the column to its right, and a wide primary
    /// standing there owns one further out still. `Buffer::set` blanks that
    /// whole cluster, so the damage has to reach past the columns the write
    /// itself covers or the orphaned continuation survives the frame.
    #[test]
    fn damage_reaches_the_continuation_a_wide_neighbour_owns() {
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((3, 0), &Cell::new("\u{6f22}", 2));
        rb.clear_touched();

        let before = rb.line(0).expect("row 0").to_vec();
        rb.set_cell((2, 0), &Cell::new("\u{4e16}", 2));
        let after = rb.line(0).expect("row 0").to_vec();
        let span = rb.touched(0).expect("row 0 must be dirty");

        assert!(
            before[4].is_continuation(),
            "precondition: the neighbour owned column 4",
        );
        assert!(
            !after[4].is_continuation(),
            "precondition: the write must have blanked it",
        );
        assert_span_covers_changes(span, &before, &after, "a wide write over a wide neighbour");
        assert_eq!(
            (span.first, span.last),
            (2, 4),
            "damage must reach the neighbour's continuation at column 4",
        );
    }

    #[test]
    fn test_set_cell_touches() {
        let mut rb = RenderBuffer::new(10, 5);
        rb.set_cell((3, 2), &Cell::new("X", 1));
        assert!(rb.has_changes());
        assert!(rb.touched(2).is_some());
        assert!(rb.touched(0).is_none());
    }

    #[test]
    fn test_same_cell_no_touch() {
        let mut rb = RenderBuffer::new(10, 5);
        // Set same as blank — should not touch
        rb.set_cell((0, 0), &Cell::BLANK);
        assert!(!rb.has_changes());
    }

    #[test]
    fn equal_wide_cell_after_shrink_is_normalized() {
        use crate::{color::Color, style::Style};

        for (text, width) in [("中", 2), ("👩‍👩‍👧‍👦", 8)] {
            for x in [0, 3] {
                for remaining in 1..u16::from(width) {
                    for style in [Style::default(), Style::EMPTY.bg(Color::Red).bold()] {
                        let cell = Cell::new(text, width).style(style);
                        let mut rb = RenderBuffer::new(x + u16::from(width) + 1, 1);
                        rb.set_cell((x, 0), &cell);
                        rb.resize(x + remaining, 1);
                        rb.clear_touched();

                        let before = rb.line(0).unwrap().to_vec();
                        assert_eq!(before[x as usize], cell);
                        let mut expected = rb.buffer.clone();
                        expected.set((x, 0), &cell);

                        rb.set_cell((x, 0), &cell);

                        assert_eq!(rb.line(0), expected.line(0));
                        let blank = Cell::BLANK.style(cell.style.clone());
                        for col in x..rb.width() {
                            assert_eq!(rb.buffer.cell(Position::new(col, 0)), Some(&blank));
                        }
                        let span = rb.touched(0).expect("normalization must record damage");
                        assert_eq!((span.first, span.last), (x, x + remaining - 1));
                        assert_span_covers_changes(
                            span,
                            &before,
                            rb.line(0).unwrap(),
                            "an equal wide cell after shrink",
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn equal_wide_cell_that_still_fits_after_shrink_is_untouched() {
        for width in [1, 2, 8] {
            for x in [0, 3] {
                let cell = Cell::new("W", width);
                let mut rb = RenderBuffer::new(x + u16::from(width) + 1, 1);
                rb.set_cell((x, 0), &cell);
                rb.resize(x + u16::from(width), 1);
                rb.clear_touched();
                let before = rb.line(0).unwrap().to_vec();

                rb.set_cell((x, 0), &cell);

                assert_eq!(rb.line(0).unwrap(), before);
                assert!(!rb.has_changes());
            }
        }
    }

    #[test]
    fn test_touched_span_expansion() {
        let mut rb = RenderBuffer::new(10, 5);
        rb.set_cell((2, 0), &Cell::new("A", 1));
        rb.set_cell((7, 0), &Cell::new("B", 1));
        let span = rb.touched(0).unwrap();
        assert_eq!(span.first, 2);
        assert_eq!(span.last, 7);
    }

    #[test]
    fn test_clear_touched() {
        let mut rb = RenderBuffer::new(10, 5);
        rb.set_cell((0, 0), &Cell::new("X", 1));
        assert!(rb.has_changes());
        rb.clear_touched();
        assert!(!rb.has_changes());
    }

    #[test]
    fn test_touch_all() {
        let mut rb = RenderBuffer::new(10, 5);
        rb.touch_all();
        assert_eq!(rb.touched_line_count(), 5);
    }

    #[test]
    fn a_fill_inside_a_wide_cell_damages_every_column_it_blanks() {
        // The fill lands wholly inside an eight-column cell. Blanking the
        // primary takes all eight columns with it, so the damage span has
        // to cover all eight. The right end is read at the fill's last
        // column, which is a continuation, so it has to reach back to the
        // primary to learn how far the blanking goes.
        let fam = "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}\u{200d}\u{1f466}";
        let mut rb = RenderBuffer::new(12, 1);
        rb.set_cell((0, 0), &Cell::new(fam, 8));
        rb.clear_touched();

        rb.fill_rect(Rect::new(2, 0, 2, 1), &Cell::new("x", 1));
        let (_, span) = rb.touched_lines().next().expect("the row changed");
        assert_eq!(span.first, 0, "the primary blanked with the fill");
        assert_eq!(span.last, 7, "so did every column it owned");
    }

    #[test]
    fn fill_rect_empty_rect_does_not_touch_lines() {
        let mut rb = RenderBuffer::new(10, 5);
        rb.fill_rect(Rect::new(2, 1, 0, 3), &Cell::BLANK);
        assert!(!rb.has_changes());
        rb.fill_rect(Rect::new(2, 1, 3, 0), &Cell::BLANK);
        assert!(!rb.has_changes());
    }

    #[test]
    fn fill_rect_clipped_to_zero_width_does_not_touch_lines() {
        // Rect starts at/beyond buffer width — after clipping right ==
        // left, so there's nothing to mark dirty.
        let mut rb = RenderBuffer::new(10, 5);
        rb.fill_rect(Rect::new(10, 0, 3, 2), &Cell::BLANK);
        assert!(!rb.has_changes());
    }

    #[test]
    fn fill_rect_via_trait_default_touches_every_affected_row() {
        // `fill_rect` is the default impl on `SurfaceMut`. It must
        // route every write through `set_cell` so dirty tracking sees
        // the change.
        let mut rb = RenderBuffer::new(10, 5);
        rb.fill_rect(Rect::new(2, 1, 3, 2), &Cell::new("X", 1));
        assert!(rb.touched(0).is_none());
        let r1 = rb.touched(1).expect("row 1 should be dirty");
        assert_eq!((r1.first, r1.last), (2, 4));
        let r2 = rb.touched(2).expect("row 2 should be dirty");
        assert_eq!((r2.first, r2.last), (2, 4));
        assert!(rb.touched(3).is_none());
    }

    #[test]
    fn fill_rect_via_trait_default_lays_wide_pairs_without_clobbering() {
        // Default `fill_rect` must step by `cell.width()` — otherwise a
        // wide fill cascades: each set_cell sees the previous cell's
        // continuation marker, walks back, blanks the just-written
        // primary, then writes its own primary.
        let mut rb = RenderBuffer::new(8, 1);
        let wide = Cell::new("漢", 2);
        rb.fill_rect(Rect::new(0, 0, 6, 1), &wide);
        for x in (0..6).step_by(2) {
            let p = rb.buffer.cell(Position::new(x, 0)).unwrap();
            assert_eq!(p.content(), "漢", "primary at x={x}");
            assert_eq!(p.width(), 2);
            let c = rb.buffer.cell(Position::new(x + 1, 0)).unwrap();
            assert!(c.is_continuation(), "continuation at x={}", x + 1);
        }
    }

    #[test]
    fn fill_rect_via_trait_default_blanks_trailing_partial_slot() {
        // Odd width with a 2-wide fill leaves one trailing slot that
        // can't hold a primary; it must become a blank, not garbage.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((4, 0), &Cell::new("X", 1));
        let wide = Cell::new("漢", 2);
        rb.fill_rect(Rect::new(0, 0, 5, 1), &wide);
        assert_eq!(rb.buffer.cell(Position::new(0, 0)).unwrap().width(), 2);
        assert_eq!(rb.buffer.cell(Position::new(2, 0)).unwrap().width(), 2);
        let trailing = rb.buffer.cell(Position::new(4, 0)).unwrap();
        assert!(trailing.is_blank(), "trailing slot must be blank");
        assert_eq!(trailing.width(), 1);
    }

    #[test]
    fn clear_via_trait_default_marks_every_row() {
        let mut rb = RenderBuffer::new(4, 3);
        // Stage a non-blank background so clear() has real work to do.
        rb.fill_rect(Rect::new(0, 0, 4, 3), &Cell::new("X", 1));
        rb.clear_touched();
        rb.clear();
        for y in 0..3 {
            let span = rb.touched(y).expect("every row should be dirty");
            assert_eq!((span.first, span.last), (0, 3));
        }
    }

    #[test]
    fn fill_rect_width1_clears_orphan_primary_at_left_edge() {
        // A wide cell straddling `lo`: primary at col 2, continuation
        // at col 3. The fill region `[3, 6)` overwrites the
        // continuation but the bulk-fill path must also blank the
        // primary at col 2 so no half-wide cell is left behind.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((2, 0), &Cell::new("漢", 2));
        assert!(
            rb.buffer
                .cell(Position::new(3, 0))
                .unwrap()
                .is_continuation()
        );
        rb.fill_rect(Rect::new(3, 0, 3, 1), &Cell::BLANK);
        assert!(
            rb.buffer.cell(Position::new(2, 0)).unwrap().is_blank(),
            "left-straddle primary must be blanked",
        );
        for x in 3..6 {
            assert!(rb.buffer.cell(Position::new(x, 0)).unwrap().is_blank());
        }
    }

    #[test]
    fn fill_rect_damage_reaches_the_primary_it_blanks() {
        // The bulk fill blanks the primary that owns a continuation it
        // overwrites, and that primary can sit left of the rect. The damage
        // has to reach it: recording only the rect leaves the diff blind to a
        // column the write changed, and the stale glyph survives the frame.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((2, 0), &Cell::new("漢", 2));
        rb.clear_touched();

        rb.fill_rect(Rect::new(3, 0, 3, 1), &Cell::BLANK);

        assert!(
            rb.buffer.cell(Position::new(2, 0)).unwrap().is_blank(),
            "precondition: the fill must have blanked the primary",
        );
        let span = rb.touched(0).expect("row 0 must be dirty");
        assert_eq!(
            (span.first, span.last),
            (2, 5),
            "damage must reach the blanked primary at column 2",
        );
    }

    #[test]
    fn fill_rect_damage_reaches_the_continuation_it_blanks() {
        // The bulk fill blanks the continuation owned by a primary it
        // overwrites, and that continuation sits one column past the rect.
        // The rect is not the damage: recording it alone leaves the diff
        // blind to a column the fill rewrote, and the stale half of the
        // cluster survives the frame.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((5, 0), &Cell::new("\u{6f22}", 2));
        rb.clear_touched();

        let before = rb.line(0).expect("row 0").to_vec();
        rb.fill_rect(Rect::new(3, 0, 3, 1), &Cell::BLANK);
        let after = rb.line(0).expect("row 0").to_vec();
        let span = rb.touched(0).expect("row 0 must be dirty");

        assert!(
            before[6].is_continuation(),
            "precondition: the primary at the rect's edge owned column 6",
        );
        assert!(
            !after[6].is_continuation(),
            "precondition: the fill must have blanked it",
        );
        assert_span_covers_changes(span, &before, &after, "a fill ending on a wide primary");
        assert_eq!(
            (span.first, span.last),
            (3, 6),
            "damage must reach the blanked continuation at column 6",
        );
    }

    #[test]
    fn fill_rect_width1_clears_orphan_continuation_at_right_edge() {
        // A wide cell straddling `hi - 1`: primary at col 5,
        // continuation at col 6. The fill region `[3, 6)` overwrites
        // the primary but the bulk-fill path must also blank the
        // continuation at col 6 sitting just past `hi`.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((5, 0), &Cell::new("漢", 2));
        assert!(
            rb.buffer
                .cell(Position::new(6, 0))
                .unwrap()
                .is_continuation()
        );
        rb.fill_rect(Rect::new(3, 0, 3, 1), &Cell::BLANK);
        for x in 3..6 {
            assert!(rb.buffer.cell(Position::new(x, 0)).unwrap().is_blank());
        }
        assert!(
            !rb.buffer
                .cell(Position::new(6, 0))
                .unwrap()
                .is_continuation(),
            "right-straddle orphan continuation must be cleared",
        );
        assert!(rb.buffer.cell(Position::new(6, 0)).unwrap().is_blank());
    }

    #[test]
    fn insert_cells_damage_reaches_the_primary_it_orphans() {
        // Starting the shift on a continuation drags it out from beside the
        // primary that owns it, leaving a wide cell claiming a column that
        // now holds something else. The primary sits left of the shift and
        // its own bytes never change, so only the damage can tell the diff to
        // repaint it.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((2, 0), &Cell::new("\u{6f22}", 2));
        rb.clear_touched();

        let before = rb.line(0).expect("row 0").to_vec();
        rb.insert_cells((3, 0), 1, 8, &Cell::BLANK);
        let after = rb.line(0).expect("row 0").to_vec();
        let span = rb.touched(0).expect("row 0 must be dirty");

        assert!(
            after[2].is_wide(),
            "precondition: the primary outlives the shift",
        );
        assert!(
            !after[3].is_continuation(),
            "precondition: the shift took its continuation away",
        );
        assert_span_covers_changes(
            span,
            &before,
            &after,
            "an insert starting on a continuation",
        );
        assert_eq!(
            (span.first, span.last),
            (2, 7),
            "damage must reach the orphaned primary at column 2",
        );
    }

    #[test]
    fn insert_cells_damage_reaches_the_continuation_it_orphans() {
        // The far end breaks the same way. A primary at the last column of
        // the region is carried off by the shift while the continuation it
        // owned stays put outside the region, so the damage has to reach one
        // column past `bounds_right`.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((5, 0), &Cell::new("\u{6f22}", 2));
        rb.clear_touched();

        let before = rb.line(0).expect("row 0").to_vec();
        rb.insert_cells((0, 0), 1, 6, &Cell::BLANK);
        let after = rb.line(0).expect("row 0").to_vec();
        let span = rb.touched(0).expect("row 0 must be dirty");

        assert!(
            !after[5].is_wide(),
            "precondition: the shift carried the primary off",
        );
        assert!(
            after[6].is_continuation(),
            "precondition: its continuation stayed behind",
        );
        assert_span_covers_changes(span, &before, &after, "an insert ending on a wide primary");
        assert_eq!(
            (span.first, span.last),
            (0, 6),
            "damage must reach the orphaned continuation at column 6",
        );
    }

    #[test]
    fn delete_cells_damage_reaches_the_primary_it_orphans() {
        // A left shift breaks a cluster at its start exactly as a right one
        // does, and the primary it strands is just as invisible to a diff
        // that only reads the region asked for.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((2, 0), &Cell::new("\u{6f22}", 2));
        rb.clear_touched();

        let before = rb.line(0).expect("row 0").to_vec();
        rb.delete_cells((3, 0), 1, 8, &Cell::BLANK);
        let after = rb.line(0).expect("row 0").to_vec();
        let span = rb.touched(0).expect("row 0 must be dirty");

        assert!(
            after[2].is_wide(),
            "precondition: the primary outlives the shift",
        );
        assert!(
            !after[3].is_continuation(),
            "precondition: the shift took its continuation away",
        );
        assert_span_covers_changes(span, &before, &after, "a delete starting on a continuation");
        assert_eq!(
            (span.first, span.last),
            (2, 7),
            "damage must reach the orphaned primary at column 2",
        );
    }

    #[test]
    fn delete_cells_damage_reaches_the_continuation_it_orphans() {
        // A primary at the last column of the region is pulled left, and the
        // continuation it owned sits outside the region where the shift
        // cannot reach it. Nothing but the damage says that column now holds
        // half of a cluster whose other half moved.
        let mut rb = RenderBuffer::new(8, 1);
        rb.set_cell((5, 0), &Cell::new("\u{6f22}", 2));
        rb.clear_touched();

        let before = rb.line(0).expect("row 0").to_vec();
        rb.delete_cells((0, 0), 1, 6, &Cell::BLANK);
        let after = rb.line(0).expect("row 0").to_vec();
        let span = rb.touched(0).expect("row 0 must be dirty");

        assert!(
            after[4].is_wide(),
            "precondition: the shift pulled the primary left",
        );
        assert!(
            !after[5].is_continuation(),
            "precondition: it landed without its continuation",
        );
        assert!(
            after[6].is_continuation(),
            "precondition: the continuation stayed behind",
        );
        assert_span_covers_changes(span, &before, &after, "a delete ending on a wide primary");
        assert_eq!(
            (span.first, span.last),
            (0, 6),
            "damage must reach the orphaned continuation at column 6",
        );
    }
}