Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer { /* private fields */ }
Expand description

Off-screen storage for a rectangular grid of terminal cells.

A Buffer owns width * height Cell values in row-major order. Row y lives at cells[y * width..(y + 1) * width], and column x within that row is addressed by Position::new through the Surface and SurfaceMut APIs.

Use a buffer when rendering into memory before presenting the result to another surface, when composing with Windows, or when tests need a deterministic cell grid. Writes outside the buffer bounds are ignored; reads outside the bounds return None.

Wide cells are stored as a primary Cell followed by one continuation cell. Prefer Buffer::set or SurfaceMut::set_cell for writes so that continuation slots are kept consistent.

Implementations§

Source§

impl Buffer

Source

pub fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell)

Insert rows within a bounded vertical region.

Existing rows in [y, bounds_bottom) are pushed downward by n. Rows pushed past bounds_bottom are discarded, and the newly opened rows starting at y are filled with fill.

§Parameters
  • y: zero-based row where insertion begins.
  • n: number of rows to insert.
  • bounds_bottom: exclusive lower row bound for the affected region; clamped to the buffer height.
  • fill: cell used to fill inserted rows.
§Returns

Nothing.

§Panics

Never panics.

§Usage notes

Calls with y >= bounds_bottom are no-ops. Whole rows are swapped in row-major storage, so wide-cell pairs inside moved rows remain structurally unchanged.

Source

pub fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell)

Delete rows within a bounded vertical region.

Existing rows below the deleted range are pulled upward within [y, bounds_bottom). The freed rows at the bottom of the region are filled with fill.

§Parameters
  • y: zero-based first row to delete.
  • n: number of rows to delete.
  • bounds_bottom: exclusive lower row bound for the affected region; clamped to the buffer height.
  • fill: cell used to fill freed bottom rows.
§Returns

Nothing.

§Panics

Never panics.

§Usage notes

Calls with y >= bounds_bottom are no-ops. Deleting more rows than remain in the region is equivalent to deleting through bounds_bottom.

Source

pub fn insert_cells( &mut self, pos: impl Into<Position>, n: u16, bounds_right: u16, fill: &Cell, )

Insert cells within one row.

Cells in [pos.x, bounds_right) on pos.y are pushed right by n. Cells pushed past bounds_right are discarded, and the newly opened slots starting at pos.x are filled with fill.

§Parameters
  • pos: row and starting column for insertion.
  • n: number of cells to insert.
  • bounds_right: exclusive right column bound for the affected row region; clamped to the buffer width.
  • fill: cell used to fill newly opened slots.
§Returns

Nothing.

§Panics

Never panics.

§Usage notes

Calls with n == 0, an out-of-bounds row, or pos.x >= bounds_right are no-ops. Width-1 fills are written directly into the freed span. Wide fills are routed through Buffer::set so primary and continuation columns are placed consistently.

Source

pub fn delete_cells( &mut self, pos: impl Into<Position>, n: u16, bounds_right: u16, fill: &Cell, )

Delete cells within one row.

Cells in [pos.x + n, bounds_right) on pos.y are pulled left by n. The freed slots at the right edge of the affected region are filled with fill.

§Parameters
  • pos: row and starting column for deletion.
  • n: number of cells to delete.
  • bounds_right: exclusive right column bound for the affected row region; clamped to the buffer width.
  • fill: cell used to fill freed right-edge slots.
§Returns

Nothing.

§Panics

Never panics.

§Usage notes

Calls with n == 0, an out-of-bounds row, or pos.x >= bounds_right are no-ops. Width-1 fills are written directly into the freed span. Wide fills are routed through Buffer::set so primary and continuation columns are placed consistently.

Source§

impl Buffer

Source

pub fn new(width: u16, height: u16) -> Self

Create a new blank buffer.

The returned buffer has width * height cells, all initialized to Cell::BLANK. width and height are measured in terminal cell columns and rows, not bytes or grapheme clusters.

§Parameters
  • width: number of columns in each row.
  • height: number of rows.
§Returns

A row-major Buffer with bounds Rect::new(0, 0, width, height).

§Panics

Panics if width * height cannot be allocated.

§Usage notes

Zero-sized dimensions are valid. Accessors will then return empty rows or None according to the resulting bounds.

Source

pub fn width(&self) -> u16

Return the buffer width in terminal cell columns.

§Returns

The number of columns in each row.

§Panics

Never panics.

§Usage notes

This is the inherent accessor for the stored width. The Bounded implementation reports the same value through Bounded::width.

Source

pub fn height(&self) -> u16

Return the buffer height in terminal cell rows.

§Returns

The number of rows in the buffer.

§Panics

Never panics.

§Usage notes

This is the inherent accessor for the stored height. The Bounded implementation reports the same value through Bounded::height.

Source

pub fn line(&self, y: u16) -> Option<&[Cell]>

Borrow one row as a contiguous slice.

§Parameters
  • y: zero-based row index in buffer coordinates.
§Returns

Some(&[Cell]) with length Buffer::width when y is inside the buffer, or None when y >= height.

§Panics

Never panics.

§Usage notes

The returned slice may contain continuation cells that belong to wide primaries earlier in the same row. Do not assume every slice element starts an independent grapheme.

Source

pub fn line_mut(&mut self, y: u16) -> Option<&mut [Cell]>

Mutably borrow one row as a contiguous slice.

§Parameters
  • y: zero-based row index in buffer coordinates.
§Returns

Some(&mut [Cell]) with length Buffer::width when y is inside the buffer, or None when y >= height.

§Panics

Never panics.

§Usage notes

Direct slice mutation bypasses the wide-cell accounting performed by Buffer::set. It is appropriate for whole-row helpers that manage continuations themselves; prefer Buffer::set for ordinary writes.

Source

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

Borrow one cell mutably.

§Parameters
  • pos: zero-based cell coordinate in buffer coordinates.
§Returns

Some(&mut Cell) for an in-bounds position, or None when pos lies outside the buffer.

§Panics

Never panics.

§Usage notes

Mutating a cell through this handle does not update neighboring continuation cells. Do not change a cell from narrow to wide, wide to narrow, or continuation to primary through this handle; use Buffer::set or SurfaceMut::set_cell when the cell width may change.

Source

pub fn set(&mut self, pos: impl Into<Position>, cell: &Cell)

Set a cell at a position while preserving wide-cell invariants.

§Parameters
  • pos: zero-based destination coordinate. Any type convertible into Position is accepted.
  • cell: cell to clone into the destination.
§Behavior

In-bounds narrow writes replace exactly one column, blanking any stale wide-cell halves they overwrite. In-bounds wide writes place cell at pos and write continuation placeholders into the columns it covers. If a wide cell would extend past the right edge of the row, the destination column is set to Cell::BLANK instead.

Out-of-bounds writes are ignored.

§Panics

Never panics.

§Usage notes

This is the implementation behind SurfaceMut::set_cell for Buffer. Use it instead of Buffer::cell_mut whenever the write might affect the width or role of neighboring cells.

Source

pub fn resize(&mut self, width: u16, height: u16)

Resize the buffer, preserving the top-left intersection.

§Parameters
  • width: new row width in terminal cell columns.
  • height: new height in terminal cell rows.
§Behavior

Cells inside the intersection of the old and new bounds keep their row and column. Newly exposed cells are filled with Cell::BLANK, and cells outside the new bounds are discarded.

§Panics

Panics if the resized backing store cannot be allocated.

§Usage notes

Resizing copies cells structurally and does not reflow wide cells. If the new right edge cuts through a wide grapheme, the copied continuation or primary remains as stored until later writes or draws normalize the affected edge.

Trait Implementations§

Source§

impl Bounded for Buffer

Source§

fn bounds(&self) -> Rect

Return the valid region in this value’s own coordinate space. Read more
Source§

fn width(&self) -> u16

Return the width of Self::bounds in terminal cell columns. Read more
Source§

fn height(&self) -> u16

Return the height of Self::bounds in terminal cell rows. Read more
Source§

fn contains(&self, pos: Position) -> bool

Test whether a position lies inside Self::bounds. Read more
Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Buffer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Buffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Surface for Buffer

Source§

fn cell(&self, pos: Position) -> Option<&Cell>

Read the cell at a position. Read more
Source§

fn draw<T: SurfaceMut + ?Sized>(&self, target: &mut T, at: Position)

Copy self’s cells into target, mapping the top-left of self.bounds() to at in target coordinates. Read more
Source§

impl SurfaceMut for Buffer

Source§

fn fill_rect(&mut self, rect: Rect, cell: &Cell)

Fill the clipped intersection of rect with cell. For width-1 fills this collapses the trait default’s per-cell set_cell loop into one slice::fill per row, with explicit wide-cell edge-straddle cleanup at the left and right boundaries so any wide cell crossing the fill region leaves no orphan primary or continuation behind. Wide fills (cell.width() > 1) stay on the stepped set_cell path so primary/continuation pairing and the trailing-partial-slot blank are placed by the same wide-cell handling that set already implements.

Source§

fn set_cell(&mut self, pos: Position, cell: &Cell)

Place cell at pos. Implementations are responsible for wide-cell semantics (continuation markers, blanking covered cells) and any dirty tracking they care to do. Taking &Cell lets implementations skip the clone when the destination already matches. Read more
Source§

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

Mutable handle to the cell at pos. Returns None for out-of-bounds positions. Read more
Source§

fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell)

Insert n blank rows at y, pushing existing rows down within [y, bounds_bottom). Rows pushed past bounds_bottom are lost. Freed top rows are filled with fill. Read more
Source§

fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell)

Delete n rows at y, pulling existing rows up within [y, bounds_bottom). The bottom n rows of the window are filled with fill. Read more
Source§

fn insert_cells( &mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell, )

Insert n blank cells at pos, pushing cells in [pos.x, bounds_right) right within row pos.y. Cells pushed past bounds_right are lost. The freed slots [pos.x, pos.x + n) are filled with fill. Read more
Source§

fn delete_cells( &mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell, )

Delete n cells at pos, pulling cells in [pos.x + n, bounds_right) left within row pos.y. The freed slots [bounds_right - n, bounds_right) are filled with fill. Read more
Source§

fn fill(&mut self, cell: &Cell)

Fill the entire surface bounds with cell. Read more
Source§

fn clear(&mut self)

Clear the entire surface bounds to Cell::BLANK. Read more
Source§

fn clear_rect(&mut self, rect: Rect)

Clear a rectangle to Cell::BLANK. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<S> Encode for S
where S: Surface + ?Sized,

Source§

fn encode<W: Write>(&self, w: &mut W) -> Result<()>

Write the surface to w as escape sequences and text. Read more
Source§

fn encode_with<W: Write>(&self, w: &mut W, profile: Profile) -> Result<()>

Write the surface to w, downsampling colors to profile. Read more
Source§

fn display(&self) -> SurfaceDisplay<'_, Self>

Borrow the surface as a Display adapter. Read more
Source§

fn display_with(&self, profile: Profile) -> SurfaceDisplay<'_, Self>

Borrow the surface as a Display adapter that downsamples colors to profile. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,