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
//! 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 one cell position as touched.
///
/// Expands the row's existing touched span to include `pos.x`.
/// Out-of-bounds rows are ignored.
#[allow(dead_code)]
pub fn touch(&mut self, pos: impl Into<Position>) {
let pos = pos.into();
let y = pos.y as usize;
if y >= self.touched.len() {
return;
}
match &mut self.touched[y] {
Some(span) => {
span.first = span.first.min(pos.x);
span.last = span.last.max(pos.x);
}
None => {
self.touched[y] = Some(TouchedSpan {
first: pos.x,
last: pos.x,
});
}
}
}
/// 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 });
}
}
}
/// 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, no touched span is recorded. When
/// a wide cell is overwritten by a narrower cell, the touched span is
/// widened to cover stale continuation columns.
pub fn set_cell(&mut self, pos: impl Into<Position>, cell: &Cell) {
let pos = pos.into();
let existing = self.buffer.cell(pos);
let changed = existing.is_none_or(|e| e != cell);
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);
self.buffer.set(pos, cell);
// Touch the range of columns this cell occupies, extending
// to cover any wider cell being overwritten so the touched
// span includes the orphaned continuation column(s).
let end_col = pos.x + width - 1;
self.touch_line(pos.y, pos.x, end_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();
}
/// 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.
#[allow(dead_code)]
pub fn insert_cells(
&mut self,
pos: impl Into<Position>,
n: u16,
bounds_right: u16,
fill: &Cell,
) {
let pos = pos.into();
self.buffer.insert_cells(pos, n, bounds_right, fill);
self.touch_line(
pos.y,
pos.x,
bounds_right.min(self.width()).saturating_sub(1),
);
}
/// 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.
#[allow(dead_code)]
pub fn delete_cells(
&mut self,
pos: impl Into<Position>,
n: u16,
bounds_right: u16,
fill: &Cell,
) {
let pos = pos.into();
self.buffer.delete_cells(pos, n, bounds_right, fill);
self.touch_line(
pos.y,
pos.x,
bounds_right.min(self.width()).saturating_sub(1),
);
}
}
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;
}
self.buffer.fill_rect(rect, cell);
let last_col = clipped.right().saturating_sub(1);
for y in clipped.top()..clipped.bottom() {
self.touch_line(y, clipped.left(), last_col);
}
}
/// 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 {
use super::*;
#[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());
}
#[test]
fn test_set_cell_touches() {
let mut rb = RenderBuffer::new(10, 5);
rb.set_cell((3, 2), &Cell::narrow("X"));
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 test_touched_span_expansion() {
let mut rb = RenderBuffer::new(10, 5);
rb.set_cell((2, 0), &Cell::narrow("A"));
rb.set_cell((7, 0), &Cell::narrow("B"));
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::narrow("X"));
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 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::narrow("X"));
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::wide("漢");
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);
let wide = Cell::wide("漢");
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::narrow("X"));
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::wide("漢"));
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_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::wide("漢"));
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());
}
}