pub struct Buffer {
pub degradation: DegradationLevel,
/* private fields */
}Expand description
A 2D grid of terminal cells.
§Example
use ftui_render::buffer::Buffer;
use ftui_render::cell::Cell;
let mut buffer = Buffer::new(80, 24);
buffer.set(0, 0, Cell::from_char('H'));
buffer.set(1, 0, Cell::from_char('i'));Fields§
§degradation: DegradationLevelCurrent degradation level for this frame.
Widgets read this during rendering to decide how much visual fidelity
to provide. Set by the runtime before calling Model::view().
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 the default (empty cell with white foreground and transparent background).
§Panics
Panics if width or height is 0.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the buffer is empty (should never be true for valid buffers).
Sourcepub fn content_height(&self) -> u16
pub fn content_height(&self) -> u16
Return the height of content (last non-empty row + 1).
Rows are considered empty only if all cells are the default cell. Returns 0 if the buffer contains no content.
Sourcepub fn mark_all_dirty(&mut self)
pub fn mark_all_dirty(&mut self)
Mark all rows as dirty (e.g., after a full clear or bulk write).
Sourcepub fn clear_dirty(&mut self)
pub fn clear_dirty(&mut self)
Reset all dirty flags and spans to clean.
Call this after the diff has consumed the dirty state (between frames).
Sourcepub fn is_row_dirty(&self, y: u16) -> bool
pub fn is_row_dirty(&self, y: u16) -> bool
Check if a specific row is dirty.
Sourcepub fn dirty_rows(&self) -> &[bool]
pub fn dirty_rows(&self) -> &[bool]
Get the dirty row flags as a slice.
Each element corresponds to a row: true means the row was modified
since the last clear_dirty() call.
Sourcepub fn dirty_row_count(&self) -> usize
pub fn dirty_row_count(&self) -> usize
Count the number of dirty rows.
Sourcepub fn dirty_span_stats(&self) -> DirtySpanStats
pub fn dirty_span_stats(&self) -> DirtySpanStats
Summarize dirty-span stats for logging/telemetry.
Sourcepub fn dirty_span_config(&self) -> DirtySpanConfig
pub fn dirty_span_config(&self) -> DirtySpanConfig
Access the dirty-span configuration.
Sourcepub fn set_dirty_span_config(&mut self, config: DirtySpanConfig)
pub fn set_dirty_span_config(&mut self, config: DirtySpanConfig)
Update dirty-span configuration (clears existing spans when changed).
Sourcepub fn get(&self, x: u16, y: u16) -> Option<&Cell>
pub fn get(&self, x: u16, y: u16) -> Option<&Cell>
Get a reference to the 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 the cell at (x, y).
Returns None if coordinates are out of bounds.
Proactively marks the row dirty since the caller may mutate the cell.
Sourcepub fn get_unchecked(&self, x: u16, y: u16) -> &Cell
pub fn get_unchecked(&self, x: u16, y: u16) -> &Cell
Get a reference to the cell at (x, y) without bounds checking.
§Panics
Panics in debug mode if coordinates are out of bounds. May cause undefined behavior in release mode if out of bounds.
Sourcepub fn set_fast(&mut self, x: u16, y: u16, cell: Cell)
pub fn set_fast(&mut self, x: u16, y: u16, cell: Cell)
Fast-path cell write for the common case.
Bypasses scissor intersection, opacity blending, and overlap cleanup when all of the following hold:
- The cell is single-width (
width() <= 1) and not a continuation - The cell background is either fully opaque or fully transparent
(
bg.a() == 255 || bg.a() == 0) - Only the base scissor is active (no nested push)
- Only the base opacity is active (no nested push)
- The existing cell at the target is also single-width and not a continuation
Falls through to [set()] for any non-trivial case, so behavior is
always identical to calling set() directly.
Sourcepub fn set(&mut self, x: u16, y: u16, cell: Cell)
pub fn set(&mut self, x: u16, y: u16, cell: Cell)
Set the cell at (x, y).
This method:
- Respects the current scissor region (skips if outside)
- Applies the current opacity stack to cell colors
- Does nothing if coordinates are out of bounds
- Automatically sets CONTINUATION cells for multi-width content
- Atomic wide writes: If a wide character doesn’t fully fit in the scissor region/bounds, NOTHING is written.
For bulk operations without scissor/opacity/safety, use [set_raw].
Sourcepub fn set_raw(&mut self, x: u16, y: u16, cell: Cell)
pub fn set_raw(&mut self, x: u16, y: u16, cell: Cell)
Set the cell at (x, y) without scissor or opacity processing.
This is faster but bypasses clipping and transparency. Does nothing if coordinates are out of bounds.
Sourcepub fn fill(&mut self, rect: Rect, cell: Cell)
pub fn fill(&mut self, rect: Rect, cell: Cell)
Fill a rectangular region with the given cell.
Respects scissor region and applies opacity.
Sourcepub fn reset_for_frame(&mut self)
pub fn reset_for_frame(&mut self)
Reset per-frame state and clear all cells.
This restores scissor/opacity stacks to their base values to ensure each frame starts from a clean rendering state.
Sourcepub fn clear_with(&mut self, cell: Cell)
pub fn clear_with(&mut self, cell: Cell)
Clear all cells to the given cell.
Sourcepub fn cells(&self) -> &[Cell]
pub fn cells(&self) -> &[Cell]
Get raw access to the cell slice.
This is useful for diffing against another buffer.
Sourcepub fn cells_mut(&mut self) -> &mut [Cell]
pub fn cells_mut(&mut self) -> &mut [Cell]
Get mutable raw access to the cell slice.
Marks all rows dirty since caller may modify arbitrary cells.
Sourcepub fn push_scissor(&mut self, rect: Rect)
pub fn push_scissor(&mut self, rect: Rect)
Push a scissor (clipping) region onto the stack.
The effective scissor is the intersection of all pushed rects. If the intersection is empty, no cells will be drawn.
Sourcepub fn pop_scissor(&mut self)
pub fn pop_scissor(&mut self)
Pop a scissor region from the stack.
Does nothing if only the base scissor remains.
Sourcepub fn current_scissor(&self) -> Rect
pub fn current_scissor(&self) -> Rect
Get the current effective scissor region.
Sourcepub fn scissor_depth(&self) -> usize
pub fn scissor_depth(&self) -> usize
Get the scissor stack depth.
Sourcepub fn push_opacity(&mut self, opacity: f32)
pub fn push_opacity(&mut self, opacity: f32)
Push an opacity multiplier onto the stack.
The effective opacity is the product of all pushed values.
Values are clamped to [0.0, 1.0].
Sourcepub fn pop_opacity(&mut self)
pub fn pop_opacity(&mut self)
Pop an opacity value from the stack.
Does nothing if only the base opacity remains.
Sourcepub fn current_opacity(&self) -> f32
pub fn current_opacity(&self) -> f32
Get the current effective opacity.
Sourcepub fn opacity_depth(&self) -> usize
pub fn opacity_depth(&self) -> usize
Get the opacity stack depth.
Sourcepub fn copy_from(
&mut self,
src: &Buffer,
src_rect: Rect,
dst_x: u16,
dst_y: u16,
)
pub fn copy_from( &mut self, src: &Buffer, src_rect: Rect, dst_x: u16, dst_y: u16, )
Copy a rectangular region from another buffer.
Copies cells from src at src_rect to this buffer at dst_pos.
Respects scissor region.
Sourcepub fn content_eq(&self, other: &Buffer) -> bool
pub fn content_eq(&self, other: &Buffer) -> bool
Check if two buffers have identical content.