uncurses/buffer/mod.rs
1//! Cell-grid storage and surface traits.
2//!
3//! This module provides [`Buffer`], row helpers, rectangular views, and
4//! owned [`Window`]s for working with terminal cells. Use it when code
5//! needs an off-screen grid, a clipped view of an existing grid, or a
6//! common [`Surface`] / [`SurfaceMut`] API that drawing code can target.
7//!
8//! ## Surface trait family
9//!
10//! The surface traits describe terminal-cell grids in layers:
11//!
12//! - [`Bounded`] exposes the rectangular extent of a grid.
13//! - [`Surface`] adds read-only cell access and the default
14//! [`draw`](Surface::draw) blit operation.
15//! - [`SurfaceMut`] adds mutation, clearing, rectangular fills, and
16//! terminal-style insert/delete operations.
17//!
18//! Code written against [`Surface`] or [`SurfaceMut`] can operate on a
19//! [`Buffer`], [`TextBuffer`], [`Window`], [`View`], or
20//! [`Screen`](crate::screen::Screen) without caring where the cells are
21//! stored. The shared contract is a rectangular coordinate space of
22//! [`Cell`] values addressed by
23//! [`Position`].
24//!
25//! ```rust,ignore
26//! use uncurses::buffer::{Buffer, Surface, SurfaceMut};
27//! use uncurses::cell::Cell;
28//! use uncurses::layout::Position;
29//!
30//! let mut buf = Buffer::new(4, 2);
31//! buf.set_cell(Position::new(0, 0), &Cell::new("x", 1));
32//! assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "x");
33//! ```
34//!
35//! ## Buffer storage
36//!
37//! [`Buffer`] stores a fixed-size grid in one row-major `Vec<Cell>`.
38//! Column `x` and row `y` map to `cells[y * width + x]`, so each row is a
39//! contiguous slice and cloning the buffer requires only one allocation.
40//! Resizing allocates a new row-major backing store, copies the
41//! intersection of the old and new extents, and fills new slots with
42//! [`Cell::BLANK`](crate::cell::Cell::BLANK).
43//!
44//! ```text
45//! col: 0 1 2 3
46//! ┌───┬───┬───────┬───┐
47//! row 0 (y=0) │ H │ i │ 日 │ ! │
48//! └───┴───┴───────┴───┘
49//! ▲
50//! └─ wide primary at x=2 covers columns 2 and 3
51//! ```
52//!
53//! ## Wide cells and drawing
54//!
55//! A two-column grapheme is represented by a wide primary cell followed by
56//! a continuation placeholder in the next column. [`Buffer::set`] writes
57//! that placeholder automatically, blanks stale halves when overwriting an
58//! existing wide cell, and replaces a wide cell with a blank when it would
59//! not fit at the end of a row. The default [`Surface::draw`] implementation
60//! preserves the same invariant when blitting between surfaces: orphan
61//! continuations and clipped wide primaries are emitted as blanks.
62
63mod ops;
64mod surface;
65mod view;
66mod window;
67
68mod line;
69#[cfg(test)]
70mod tests;
71mod text_buffer;
72
73pub(crate) use line::fill_line_into;
74pub use line::{Line, blank_line, fill_line};
75pub use surface::{Bounded, Surface, SurfaceMut};
76pub use text_buffer::TextBuffer;
77pub use view::View;
78pub use window::Window;
79
80use crate::cell::Cell;
81use crate::layout::{Position, Rect, overruns};
82
83/// Off-screen storage for a rectangular grid of terminal cells.
84///
85/// A `Buffer` owns `width * height` [`Cell`] values in row-major order.
86/// Row `y` lives at `cells[y * width..(y + 1) * width]`, and column `x`
87/// within that row is addressed by [`Position::new`](crate::layout::Position::new)
88/// through the [`Surface`] and [`SurfaceMut`] APIs.
89///
90/// Use a buffer when rendering into memory before presenting the result to
91/// another surface, when composing with [`Window`]s, or when tests need a
92/// deterministic cell grid. Writes outside the buffer bounds are ignored;
93/// reads outside the bounds return `None`.
94///
95/// A wide primary [`Cell`] owns one continuation cell per additional column,
96/// for a total of `width - 1` continuations. Prefer [`Buffer::set`] or
97/// [`SurfaceMut::set_cell`] for writes so that continuation slots are kept
98/// consistent.
99#[derive(Debug, Clone)]
100pub struct Buffer {
101 cells: Vec<Cell>,
102 width: u16,
103 height: u16,
104}
105
106impl Buffer {
107 /// Create a new blank buffer.
108 ///
109 /// The returned buffer has `width * height` cells, all initialized to
110 /// [`Cell::BLANK`]. `width` and `height` are measured in terminal cell
111 /// columns and rows, not bytes or grapheme clusters.
112 ///
113 /// # Parameters
114 ///
115 /// - `width`: number of columns in each row.
116 /// - `height`: number of rows.
117 ///
118 /// # Returns
119 ///
120 /// A row-major [`Buffer`] with bounds `Rect::new(0, 0, width, height)`.
121 ///
122 /// # Panics
123 ///
124 /// Panics if `width * height` cannot be allocated.
125 ///
126 /// # Usage notes
127 ///
128 /// Zero-sized dimensions are valid. Accessors will then return empty
129 /// rows or `None` according to the resulting bounds.
130 pub fn new(width: u16, height: u16) -> Self {
131 let total = (width as usize) * (height as usize);
132 let cells = vec![Cell::BLANK; total];
133 Self {
134 cells,
135 width,
136 height,
137 }
138 }
139
140 /// Return the buffer width in terminal cell columns.
141 ///
142 /// # Returns
143 ///
144 /// The number of columns in each row.
145 ///
146 /// # Panics
147 ///
148 /// Never panics.
149 ///
150 /// # Usage notes
151 ///
152 /// This is the inherent accessor for the stored width. The [`Bounded`]
153 /// implementation reports the same value through [`Bounded::width`].
154 pub fn width(&self) -> u16 {
155 self.width
156 }
157
158 /// Return the buffer height in terminal cell rows.
159 ///
160 /// # Returns
161 ///
162 /// The number of rows in the buffer.
163 ///
164 /// # Panics
165 ///
166 /// Never panics.
167 ///
168 /// # Usage notes
169 ///
170 /// This is the inherent accessor for the stored height. The [`Bounded`]
171 /// implementation reports the same value through [`Bounded::height`].
172 pub fn height(&self) -> u16 {
173 self.height
174 }
175
176 /// Borrow one row as a contiguous slice.
177 ///
178 /// # Parameters
179 ///
180 /// - `y`: zero-based row index in buffer coordinates.
181 ///
182 /// # Returns
183 ///
184 /// `Some(&[Cell])` with length [`Buffer::width`] when `y` is inside the
185 /// buffer, or `None` when `y >= height`.
186 ///
187 /// # Panics
188 ///
189 /// Never panics.
190 ///
191 /// # Usage notes
192 ///
193 /// The returned slice may contain continuation cells that belong to
194 /// wide primaries earlier in the same row. Do not assume every slice
195 /// element starts an independent grapheme.
196 #[inline]
197 pub fn line(&self, y: u16) -> Option<&[Cell]> {
198 if y >= self.height {
199 return None;
200 }
201 let w = self.width as usize;
202 let start = (y as usize) * w;
203 Some(&self.cells[start..start + w])
204 }
205
206 /// Mutably borrow one row as a contiguous slice.
207 ///
208 /// # Parameters
209 ///
210 /// - `y`: zero-based row index in buffer coordinates.
211 ///
212 /// # Returns
213 ///
214 /// `Some(&mut [Cell])` with length [`Buffer::width`] when `y` is inside
215 /// the buffer, or `None` when `y >= height`.
216 ///
217 /// # Panics
218 ///
219 /// Never panics.
220 ///
221 /// # Usage notes
222 ///
223 /// Direct slice mutation bypasses the wide-cell accounting performed by
224 /// [`Buffer::set`]. It is appropriate for whole-row helpers that manage
225 /// continuations themselves; prefer [`Buffer::set`] for ordinary writes.
226 #[inline]
227 pub fn line_mut(&mut self, y: u16) -> Option<&mut [Cell]> {
228 if y >= self.height {
229 return None;
230 }
231 let w = self.width as usize;
232 let start = (y as usize) * w;
233 Some(&mut self.cells[start..start + w])
234 }
235
236 /// Swap the contents of rows `a` and `b` in place.
237 fn swap_rows(&mut self, a: usize, b: usize) {
238 if a == b {
239 return;
240 }
241 let w = self.width as usize;
242 if w == 0 {
243 return;
244 }
245 let (lo, hi) = if a < b { (a, b) } else { (b, a) };
246 let split = hi * w;
247 let (left, right) = self.cells.split_at_mut(split);
248 left[lo * w..(lo + 1) * w].swap_with_slice(&mut right[..w]);
249 }
250
251 /// Borrow one cell mutably.
252 ///
253 /// # Parameters
254 ///
255 /// - `pos`: zero-based cell coordinate in buffer coordinates.
256 ///
257 /// # Returns
258 ///
259 /// `Some(&mut Cell)` for an in-bounds position, or `None` when `pos`
260 /// lies outside the buffer.
261 ///
262 /// # Panics
263 ///
264 /// Never panics.
265 ///
266 /// # Usage notes
267 ///
268 /// Mutating a cell through this handle does not update neighboring
269 /// continuation cells. Do not change a cell from narrow to wide, wide to
270 /// narrow, or continuation to primary through this handle; use
271 /// [`Buffer::set`] or [`SurfaceMut::set_cell`] when the cell width may
272 /// change.
273 pub fn cell_mut(&mut self, pos: Position) -> Option<&mut Cell> {
274 if pos.y >= self.height || pos.x >= self.width {
275 return None;
276 }
277 let w = self.width as usize;
278 Some(&mut self.cells[(pos.y as usize) * w + (pos.x as usize)])
279 }
280
281 /// Set a cell at a position while preserving wide-cell invariants.
282 ///
283 /// # Parameters
284 ///
285 /// - `pos`: zero-based destination coordinate. Any type convertible into
286 /// [`Position`] is accepted.
287 /// - `cell`: cell to clone into the destination.
288 ///
289 /// # Behavior
290 ///
291 /// In-bounds narrow writes replace exactly one column, blanking any stale
292 /// wide-cell halves they overwrite. In-bounds wide writes place `cell` at
293 /// `pos` and write continuation placeholders into the columns it covers.
294 /// If a wide cell would extend past the right edge of the row, the
295 /// destination column is set to [`Cell::BLANK`] instead.
296 ///
297 /// Out-of-bounds writes are ignored.
298 ///
299 /// # Panics
300 ///
301 /// Never panics.
302 ///
303 /// # Usage notes
304 ///
305 /// A continuation is placed by the wide cell that owns it, as part of
306 /// that cell's own write, so a continuation passed here is ignored and
307 /// the column keeps the value its owner gave it.
308 ///
309 /// This is the implementation behind [`SurfaceMut::set_cell`] for
310 /// `Buffer`. Use it instead of [`Buffer::cell_mut`] whenever the write
311 /// might affect the width or role of neighboring cells.
312 pub fn set(&mut self, pos: impl Into<Position>, cell: &Cell) {
313 let pos = pos.into();
314 let y = pos.y as usize;
315 let x = pos.x as usize;
316 let width = self.width as usize;
317
318 if pos.y >= self.height || x >= width {
319 return;
320 }
321
322 let line = &mut self.cells[y * width..(y + 1) * width];
323
324 // A continuation is placed by the wide cell to its left, as part of
325 // that cell's own write, so one arriving here has nothing to say
326 // that the owning write did not already say. Ignoring it keeps the
327 // column exactly as its owner left it, which is what a caller
328 // mirroring a buffer cell by cell wants: it walks continuations
329 // because they are in the grid, not because it means to move them.
330 //
331 // Ignoring is also the only safe answer. Storing the cell would let
332 // a column carry a style its owner never had. Blanking it would put
333 // a one-column cell where the owner accounts for two, so the row
334 // would emit more columns than it holds. Refusing loudly would make
335 // a redundant write in an otherwise correct mirror fatal.
336 if cell.is_continuation() {
337 return;
338 }
339
340 if line[x].width() == 1 && cell.width() == 1 {
341 line[x].clone_from(cell);
342 return;
343 }
344
345 if line[x].width() == 2 && cell.width() == 2 && x + 1 < width {
346 let mut continuation = Cell::CONTINUATION;
347 continuation.copy_style_from(cell);
348 line[x + 1] = continuation;
349 line[x].clone_from(cell);
350 return;
351 }
352
353 // If we're overwriting a wide cell's continuation, blank the primary
354 // cell that owns it, so the wide grapheme does not survive with one
355 // half replaced. The branch above already returned for an incoming
356 // continuation, so the cell arriving here is narrow or wide.
357 if x > 0 && line[x].is_continuation() {
358 // Walk backward to find the primary cell
359 let mut pc = x - 1;
360 while pc > 0 && line[pc].is_continuation() {
361 pc -= 1;
362 }
363 // Verify we found a wide primary; only Wide cells own
364 // the continuation slot to their right.
365 if line[pc].is_wide() {
366 let pw = line[pc].width() as usize;
367 let end = (pc + pw).min(width);
368 // Preserve the broken wide cell's bg/attributes on the
369 // blanks so clearing it doesn't punch a hole in a colored
370 // background.
371 let mut blank = Cell::BLANK;
372 blank.copy_style_from(&line[pc]);
373 line[pc..end].fill(blank);
374 } else if line[pc].is_continuation() {
375 // Buffer is corrupted — just blank the single cell
376 line[x] = Cell::BLANK;
377 }
378 }
379
380 // If we're overwriting the primary cell of a wide char, blank continuations
381 if line[x].is_wide() {
382 let w = line[x].width() as usize;
383 let end = (x + w).min(width);
384 let mut blank = Cell::BLANK;
385 blank.copy_style_from(&line[x]);
386 line[x + 1..end].fill(blank);
387 }
388
389 let cell_width = cell.width() as usize;
390
391 // If the new cell is wide, blank cells it will cover
392 if cell.is_wide() {
393 // Truncate at end of line: if the wide cell doesn't fit, replace
394 // it with a blank that keeps the wide cell's bg/attributes.
395 //
396 // This has to be settled before any continuation is written. A
397 // cell can claim more columns than the row has left, and laying
398 // the continuations down first would leave the row holding the
399 // tail of a cell whose primary this branch then replaces.
400 if x + cell_width > width {
401 let mut blank = Cell::BLANK;
402 blank.copy_style_from(cell);
403 line[x] = blank;
404 return;
405 }
406
407 for i in x + 1..x + cell_width {
408 // If we'd overwrite a wide cell's primary, blank its continuations
409 if line[i].is_wide() {
410 let w = line[i].width() as usize;
411 let end = (i + w).min(width);
412 let mut blank = Cell::BLANK;
413 blank.copy_style_from(&line[i]);
414 line[i + 1..end].fill(blank);
415 }
416 // Continuations inherit the wide primary's style so the
417 // cell's bg/attributes are coherent across both columns.
418 let mut continuation = Cell::CONTINUATION;
419 continuation.copy_style_from(cell);
420 line[i] = continuation;
421 }
422 }
423
424 line[x].clone_from(cell);
425 }
426
427 /// Resize the buffer, preserving the top-left intersection.
428 ///
429 /// # Parameters
430 ///
431 /// - `width`: new row width in terminal cell columns.
432 /// - `height`: new height in terminal cell rows.
433 ///
434 /// # Behavior
435 ///
436 /// Cells inside the intersection of the old and new bounds keep their
437 /// row and column. Newly exposed cells are filled with [`Cell::BLANK`],
438 /// and cells outside the new bounds are discarded.
439 ///
440 /// # Panics
441 ///
442 /// Panics if the resized backing store cannot be allocated.
443 ///
444 /// # Usage notes
445 ///
446 /// Resizing copies cells structurally and does not reflow wide cells. If
447 /// the new right edge cuts through a wide grapheme, the copied
448 /// continuation or primary remains as stored until later writes or draws
449 /// normalize the affected edge.
450 pub fn resize(&mut self, width: u16, height: u16) {
451 if width == self.width && height == self.height {
452 return;
453 }
454
455 let new_total = (width as usize) * (height as usize);
456 if width == self.width {
457 self.cells.resize(new_total, Cell::BLANK);
458 self.height = height;
459 return;
460 }
461 let mut new_cells = vec![Cell::BLANK; new_total];
462
463 let copy_w = width.min(self.width) as usize;
464 let copy_h = height.min(self.height) as usize;
465 let old_w = self.width as usize;
466 let new_w = width as usize;
467 for y in 0..copy_h {
468 let src = y * old_w;
469 let dst = y * new_w;
470 new_cells[dst..dst + copy_w].clone_from_slice(&self.cells[src..src + copy_w]);
471 }
472
473 self.cells = new_cells;
474 self.width = width;
475 self.height = height;
476 }
477}
478
479impl Bounded for Buffer {
480 fn bounds(&self) -> Rect {
481 Rect::new(0, 0, self.width, self.height)
482 }
483}
484
485impl Surface for Buffer {
486 fn cell(&self, pos: Position) -> Option<&Cell> {
487 if pos.y >= self.height || pos.x >= self.width {
488 return None;
489 }
490 let w = self.width as usize;
491 Some(&self.cells[(pos.y as usize) * w + (pos.x as usize)])
492 }
493}
494
495impl SurfaceMut for Buffer {
496 fn set_cell(&mut self, pos: Position, cell: &Cell) {
497 self.set(pos, cell);
498 }
499
500 fn cell_mut(&mut self, pos: Position) -> Option<&mut Cell> {
501 Buffer::cell_mut(self, pos)
502 }
503
504 /// Fill the clipped intersection of `rect` with `cell`. For
505 /// width-1 fills this collapses the trait default's per-cell
506 /// `set_cell` loop into one `slice::fill` per row, with explicit
507 /// wide-cell cleanup around the fill so any cell overlapping the
508 /// region leaves no orphan primary or continuation behind. A cell
509 /// wide enough can hold the whole region, in which case the cleanup
510 /// reaches past both edges of the fill. Wide fills (`cell.width() > 1`)
511 /// stay on the stepped `set_cell` path so primary/continuation
512 /// pairing and the trailing-partial-slot blank are placed by the
513 /// same wide-cell handling that `set` already implements. Partial slots
514 /// retain the fill style. Cleanup outside the rectangle retains the
515 /// old primary's style.
516 fn fill_rect(&mut self, rect: Rect, cell: &Cell) {
517 let clipped = self.bounds().intersection(rect);
518 if clipped.is_empty() {
519 return;
520 }
521
522 let step = (cell.width() as u16).max(1);
523 if step > 1 {
524 let mut blank = Cell::BLANK;
525 blank.copy_style_from(cell);
526 // Stepped wide-cell fill: identical to the trait default.
527 // Inlined here so the SurfaceMut::fill_rect dispatch goes
528 // through this impl in both arms.
529 for y in clipped.top()..clipped.bottom() {
530 let mut x = clipped.left();
531 while !overruns(x, step, clipped.right()) {
532 self.set(Position::new(x, y), cell);
533 x += step;
534 }
535 while x < clipped.right() {
536 self.set(Position::new(x, y), &blank);
537 x += 1;
538 }
539 }
540 return;
541 }
542
543 let lo = clipped.left() as usize;
544 let hi = clipped.right() as usize;
545 let row_width = self.width as usize;
546
547 for y in clipped.top()..clipped.bottom() {
548 let row_start = (y as usize) * row_width;
549 let line = &mut self.cells[row_start..row_start + row_width];
550
551 // Left-edge straddle: a wide cell whose primary sits
552 // before `lo` and whose continuation falls at `lo`. Walk
553 // back to the primary and blank it (and any siblings
554 // outside the fill region) before the bulk fill so the
555 // primary isn't left dangling once its continuations get
556 // overwritten.
557 if lo > 0 && line[lo].is_continuation() {
558 let mut p = lo;
559 while p > 0 && line[p].is_continuation() {
560 p -= 1;
561 }
562 if !line[p].is_continuation() {
563 let pw = line[p].width() as usize;
564 let mut blank = Cell::BLANK;
565 blank.copy_style_from(&line[p]);
566 let end = (p + pw).min(lo);
567 line[p..end].fill(blank.clone());
568 // The same cell can also reach past `hi`, which is what
569 // happens when the fill lands wholly inside it. Those
570 // columns just lost the primary that owned them, so
571 // they cannot stay continuations. The right-edge pass
572 // below will not reach them: it walks back only as far
573 // as `lo`, and this primary sits before that.
574 if p + pw > hi {
575 let tail = (p + pw).min(row_width);
576 line[hi..tail].fill(blank);
577 }
578 }
579 }
580
581 // Right-edge straddle: a wide cell whose primary sits at
582 // some `p` in `[lo, hi)` and whose continuations extend
583 // past `hi - 1`. The primary itself will be overwritten by
584 // the bulk fill, but the orphan continuations at
585 // `[hi, p + pw)` need explicit blanking.
586 if hi < row_width && line[hi].is_continuation() {
587 let mut p = hi - 1;
588 while p > lo && line[p].is_continuation() {
589 p -= 1;
590 }
591 if !line[p].is_continuation() && p + (line[p].width() as usize) > hi {
592 let pw = line[p].width() as usize;
593 let end = (p + pw).min(row_width);
594 let mut blank = Cell::BLANK;
595 blank.copy_style_from(&line[p]);
596 line[hi..end].fill(blank);
597 }
598 }
599
600 line[lo..hi].fill(cell.clone());
601 }
602 }
603
604 fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
605 Buffer::insert_lines(self, y, n, bounds_bottom, fill);
606 }
607
608 fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
609 Buffer::delete_lines(self, y, n, bounds_bottom, fill);
610 }
611
612 fn insert_cells(&mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell) {
613 Buffer::insert_cells(self, pos, n, bounds_right, fill);
614 }
615
616 fn delete_cells(&mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell) {
617 Buffer::delete_cells(self, pos, n, bounds_right, fill);
618 }
619}