Skip to main content

Module buffer

Module buffer 

Source
Expand description

Cell-grid storage and surface traits.

This module provides Buffer, row helpers, rectangular views, and owned Windows for working with terminal cells. Use it when code needs an off-screen grid, a clipped view of an existing grid, or a common Surface / SurfaceMut API that drawing code can target.

§Surface trait family

The surface traits describe terminal-cell grids in layers:

  • Bounded exposes the rectangular extent of a grid.
  • Surface adds read-only cell access and the default draw blit operation.
  • SurfaceMut adds mutation, clearing, rectangular fills, and terminal-style insert/delete operations.

Code written against Surface or SurfaceMut can operate on a Buffer, TextBuffer, Window, View, or Screen without caring where the cells are stored. The shared contract is a rectangular coordinate space of Cell values addressed by Position.

use uncurses::buffer::{Buffer, Surface, SurfaceMut};
use uncurses::cell::Cell;
use uncurses::layout::Position;

let mut buf = Buffer::new(4, 2);
buf.set_cell(Position::new(0, 0), &Cell::narrow("x"));
assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "x");

§Buffer storage

Buffer stores a fixed-size grid in one row-major Vec<Cell>. Column x and row y map to cells[y * width + x], so each row is a contiguous slice and cloning the buffer requires only one allocation. Resizing allocates a new row-major backing store, copies the intersection of the old and new extents, and fills new slots with Cell::BLANK.

         col:  0   1   2   3
             ┌───┬───┬───────┬───┐
row 0 (y=0)  │ H │ i │ 日    │ ! │
             └───┴───┴───────┴───┘
                        ▲
                        └─ wide primary at x=2 covers columns 2 and 3

§Wide cells and drawing

A two-column grapheme is represented by a wide primary cell followed by a continuation placeholder in the next column. Buffer::set writes that placeholder automatically, blanks stale halves when overwriting an existing wide cell, and replaces a wide cell with a blank when it would not fit at the end of a row. The default Surface::draw implementation preserves the same invariant when blitting between surfaces: orphan continuations and clipped wide primaries are emitted as blanks.

Structs§

Buffer
Off-screen storage for a rectangular grid of terminal cells.
TextBuffer
A Buffer paired with a text-width policy.
View
Borrowed sub-rect over an existing surface.
Window
A self-contained pane: a Buffer plus its position on a target surface.

Traits§

Bounded
A value with a rectangular extent in terminal-cell coordinates.
Surface
A rectangular cell grid that can be read.
SurfaceMut
A rectangular cell grid that can be modified.

Functions§

blank_line
Create a new blank line of the given width.
fill_line
Create a new line of the given width filled with fill. Wide fills (fill.width() > 1) lay down primary + continuation pairs; any trailing slot too narrow to fit another pair is a plain blank.

Type Aliases§

Line
A single line of cells, used by helpers that allocate owned rows (e.g. fill_line). Buffer accessors return slices (&[Cell]) so callers don’t pay an extra dereference through a per-row Vec.