Guide API
Skip to main content

Buffer

Struct Buffer 

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

In-memory screen grid; the render target for one complete frame.

Render back-buffer and cell diff.

Buffer is the in-memory screen grid: the whole view tree is painted into it each frame, then Buffer::diff compares it against the previous frame so only changed cells are flushed to the terminal.

The diff algorithm is adapted from ratatui’s Buffer::diff (MIT). Our Cell (see cell.rs) already encodes double-width glyphs with explicit wide/trail flags, so the wide-char skipping is driven off those rather than off unicode-width. ratatui’s skip field is dropped — tvision-rs repaints the whole tree, so there is nothing to opt out of.

Call Buffer::new to create a blank grid sized to the terminal, paint the full view tree into it via the renderer, then call Buffer::diff against the previous frame’s buffer to obtain only the changed cells to flush to the terminal. The Program root owns two buffers (current and previous) and swaps them each iteration of the event loop.

Unlike ratatui, Buffer stores only width × height rather than an area: Rect, because the back buffer is always screen-origin ((0, 0) to (width-1, height-1)). There is no offset to carry.

§Turbo Vision heritage

Replaces Turbo Vision’s per-view incremental screen writes with a double-buffered whole-tree repaint plus cell diff (deviation D8). The grid and diff are adapted from ratatui rather than ported from the original.

Replaces TVideoBuf (a fixed 80×25 word array of char+attr cells) and the per-view incremental writeView calls. The dynamic-size grid and double-buffered whole-tree repaint follow magiblot/tvision’s modernised screen model rather than the original fixed-buffer approach.

Implementations§

Source§

impl Buffer

Source

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

A new buffer filled with Cell::default() (a blank space, default style).

Source

pub fn filled(width: u16, height: u16, cell: Cell) -> Self

A new buffer where every cell is a clone of cell.

Source

pub fn width(&self) -> u16

The buffer width in columns.

Source

pub fn height(&self) -> u16

The buffer height in rows.

Source

pub fn area_len(&self) -> usize

Total number of cells (width * height).

Source

pub fn index_of(&self, x: u16, y: u16) -> usize

Convert (x, y) to a linear index into content.

Panics in debug builds if x >= width or y >= height.

Source

pub fn pos_of(&self, i: usize) -> (u16, u16)

Convert a linear index back to (x, y).

Source

pub fn get(&self, x: u16, y: u16) -> &Cell

Return a shared reference to the cell at (x, y).

Source

pub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell

Return a mutable reference to the cell at (x, y).

Source

pub fn cells(&self) -> &[Cell]

A slice over all cells in row-major order (for tests / snapshots).

Source

pub fn row(&self, y: u16) -> &[Cell]

A shared slice over row y (width cells), for width-aware row writes.

Panics in debug builds if y >= height.

Source

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

A mutable slice over row y (width cells), for width-aware row writes (e.g. feeding text::draw_str a clipped sub-slice).

Panics in debug builds if y >= height.

Source

pub fn reset(&mut self)

Reset every cell to Cell::default().

Source

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

Resize the buffer to width × height, resetting all content to Cell::default().

Source

pub fn diff<'a>(&self, other: &'a Buffer) -> Vec<(u16, u16, &'a Cell)>

Compare self (the previous/front buffer) against other (the next/back buffer) and return the cells in other that differ.

Each entry is (x, y, &cell) pointing into other.

Both buffers must have the same dimensions. The invariant is asserted in debug builds; in release the comparison is limited to the overlapping index range.

The algorithm is adapted from ratatui’s Buffer::diff: it iterates the flat cell array, suppressing the trail cell of a wide glyph via to_skip, and using invalidated to force repainting the cell that follows a cell whose previous width was greater than its current width (e.g. a wide glyph replaced by a narrow one — the trail cell must be repainted even if it happens to be equal to the old trail cell).

Trait Implementations§

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 Eq for Buffer

Source§

impl PartialEq for Buffer

Source§

fn eq(&self, other: &Buffer) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Buffer

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<T> CustomValue for T
where T: Any + Debug,

Source§

fn as_any_rc(self: Rc<T>) -> Rc<dyn Any>

Upcast Rc<Self> to Rc<dyn Any> so FieldValue::as_custom / FieldValue::value_as can downcast.
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.