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
impl Buffer
Sourcepub fn new(width: u16, height: u16) -> Self
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.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Check if the buffer is empty (should never be true after construction).
Sourcepub fn cells_mut(&mut self) -> &mut [Cell]
pub fn cells_mut(&mut self) -> &mut [Cell]
Get a mutable reference to the underlying cell slice.
Sourcepub const fn index_of(&self, x: u16, y: u16) -> Option<usize>
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.
Sourcepub const fn coords_of(&self, index: usize) -> Option<(u16, u16)>
pub const fn coords_of(&self, index: usize) -> Option<(u16, u16)>
Convert a linear index to (x, y) coordinates.
Sourcepub fn get(&self, x: u16, y: u16) -> Option<&Cell>
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.
Sourcepub fn get_mut(&mut self, x: u16, y: u16) -> Option<&mut Cell>
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.
Sourcepub fn set(&mut self, x: u16, y: u16, cell: Cell) -> bool
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.
Sourcepub fn set_grapheme(
&mut self,
x: u16,
y: u16,
grapheme: &str,
fg: Rgb,
bg: Rgb,
) -> u8
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.
Sourcepub fn get_grapheme(&self, x: u16, y: u16) -> Option<&str>
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.
Sourcepub fn get_overflow(&self, index: u32) -> Option<&str>
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.
Sourcepub fn fill_rect(&mut self, x: u16, y: u16, width: u16, height: u16, cell: Cell)
pub fn fill_rect(&mut self, x: u16, y: u16, width: u16, height: u16, cell: Cell)
Fill a rectangular region with a cell.
Sourcepub fn clear_rect(&mut self, x: u16, y: u16, width: u16, height: u16)
pub fn clear_rect(&mut self, x: u16, y: u16, width: u16, height: u16)
Clear a rectangular region.
Sourcepub fn resize(&mut self, new_width: u16, new_height: u16)
pub fn resize(&mut self, new_width: u16, new_height: u16)
Resize the buffer, preserving content where possible.
New cells are initialized to empty.
Sourcepub fn copy_from(&mut self, other: &Self)
pub fn copy_from(&mut self, other: &Self)
Copy content from another buffer.
The buffers must have the same dimensions.
Sourcepub const fn swap(&mut self, other: &mut Self)
pub const fn swap(&mut self, other: &mut Self)
Swap the contents of two buffers.
This is O(1) - just pointer swaps.
Sourcepub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [Cell]>
pub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [Cell]>
Get a mutable iterator over rows.
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Get memory usage in bytes (approximate).