Skip to main content

Buffer

Struct Buffer 

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

A grid of cells representing the terminal screen.

The buffer stores cells in a contiguous Vec for cache efficiency. Access is in row-major order: index = y * width + x.

§Overflow Storage

Complex graphemes (>4 bytes) are stored in a separate arena (Vec). The cell contains an index into this overflow storage when the OVERFLOW flag is set.

Implementations§

Source§

impl Buffer

Source

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

Create a new buffer with the given dimensions.

All cells are initialized to empty (space with default colors).

§Panics

Panics if width or height is 0.

Source

pub const fn width(&self) -> u16

Get the buffer width.

Source

pub const fn height(&self) -> u16

Get the buffer height.

Source

pub const fn len(&self) -> usize

Get the total number of cells.

Source

pub const fn is_empty(&self) -> bool

Check if the buffer is empty (should never be true after construction).

Source

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

Get a reference to the underlying cell slice.

Source

pub fn cells_mut(&mut self) -> &mut [Cell]

Get a mutable reference to the underlying cell slice.

Source

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

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

Returns None if coordinates are out of bounds.

Source

pub const fn coords_of(&self, index: usize) -> Option<(u16, u16)>

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

Source

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

Get a reference to a cell at (x, y).

Returns None if coordinates are out of bounds.

Source

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

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

Returns None if coordinates are out of bounds.

Source

pub fn set(&mut self, x: u16, y: u16, cell: Cell) -> bool

Set a cell at (x, y).

Returns false if coordinates are out of bounds.

Source

pub fn set_grapheme( &mut self, x: u16, y: u16, grapheme: &str, fg: Rgb, bg: Rgb, ) -> u8

Set a grapheme at (x, y), handling overflow automatically.

For wide characters (CJK), this also sets a continuation cell at (x+1, y).

Returns the display width of the grapheme, or 0 if out of bounds.

Source

pub fn get_grapheme(&self, x: u16, y: u16) -> Option<&str>

Get the grapheme at (x, y), including overflow lookup.

Returns None if out of bounds or if it’s a continuation cell.

Source

pub fn get_overflow(&self, index: u32) -> Option<&str>

Get an overflow grapheme by its index.

This is used by the diffing engine when rendering overflow cells.

Source

pub fn fill_rect(&mut self, x: u16, y: u16, width: u16, height: u16, cell: Cell)

Fill a rectangular region with a cell.

Source

pub fn clear(&mut self)

Clear the entire buffer (fill with empty cells).

Source

pub fn clear_rect(&mut self, x: u16, y: u16, width: u16, height: u16)

Clear a rectangular region.

Source

pub fn resize(&mut self, new_width: u16, new_height: u16)

Resize the buffer, preserving content where possible.

New cells are initialized to empty.

Source

pub fn copy_from(&mut self, other: &Self)

Copy content from another buffer.

The buffers must have the same dimensions.

Source

pub const fn swap(&mut self, other: &mut Self)

Swap the contents of two buffers.

This is O(1) - just pointer swaps.

Source

pub fn rows(&self) -> impl Iterator<Item = &[Cell]>

Get an iterator over rows.

Source

pub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [Cell]>

Get a mutable iterator over rows.

Source

pub fn memory_usage(&self) -> usize

Get memory usage in bytes (approximate).

Trait Implementations§

Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Buffer

Returns a duplicate of the value. Read more
1.0.0 · 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

Auto Trait Implementations§

§

impl Freeze for Buffer

§

impl RefUnwindSafe for Buffer

§

impl Send for Buffer

§

impl Sync for Buffer

§

impl Unpin for Buffer

§

impl UnwindSafe for Buffer

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> 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.