pub struct Screen { /* private fields */ }Expand description
A double-buffered screen of cells.
The screen tracks the current state of every cell. When rendering, the diff from the previous frame determines which cells need updating. This avoids redrawing the entire screen on every frame.
Implementations§
Source§impl Screen
impl Screen
Sourcepub fn new(width: u16, height: u16) -> Self
pub fn new(width: u16, height: u16) -> Self
Create a new screen with the given dimensions, filled with default cells.
Sourcepub fn get(&self, row: u16, col: u16) -> Option<&Cell>
pub fn get(&self, row: u16, col: u16) -> Option<&Cell>
Get a cell at the given position. Returns None if out of bounds.
Sourcepub fn get_mut(&mut self, row: u16, col: u16) -> Option<&mut Cell>
pub fn get_mut(&mut self, row: u16, col: u16) -> Option<&mut Cell>
Get a mutable cell at the given position. Returns None if out of bounds.
Sourcepub fn set_cell(&mut self, row: u16, col: u16, cell: Cell)
pub fn set_cell(&mut self, row: u16, col: u16, cell: Cell)
Set a cell at the given position. Does nothing if out of bounds.
Sourcepub fn write_str(
&mut self,
row: u16,
col: u16,
text: &str,
fg: Color,
bg: Color,
style: Style,
)
pub fn write_str( &mut self, row: u16, col: u16, text: &str, fg: Color, bg: Color, style: Style, )
Write a string starting at the given position, with the given style.
Characters that exceed the screen width are truncated.
Sourcepub fn write_str_wrapped(
&mut self,
start_row: u16,
start_col: u16,
text: &str,
fg: Color,
bg: Color,
style: Style,
wrap: bool,
) -> u16
pub fn write_str_wrapped( &mut self, start_row: u16, start_col: u16, text: &str, fg: Color, bg: Color, style: Style, wrap: bool, ) -> u16
Write a string starting at the given position, truncating or wrapping.
If wrap is true, text wraps to the next line. If false, text is
truncated at the right edge.
Sourcepub fn write_str_wrapped_clipped(
&mut self,
start_row: u16,
start_col: u16,
text: &str,
fg: Color,
bg: Color,
style: Style,
left_margin: u16,
max_row: u16,
wrap_col: u16,
) -> u16
pub fn write_str_wrapped_clipped( &mut self, start_row: u16, start_col: u16, text: &str, fg: Color, bg: Color, style: Style, left_margin: u16, max_row: u16, wrap_col: u16, ) -> u16
Write a string with wrapping, but clip rendering at the given maximum row and wrap at the given column.
wrap_col is the maximum column number; text wraps when col >= wrap_col.
max_row is the maximum row; text stops when row > max_row.
left_margin is the column where wrapped lines start.
Sourcepub fn write_str_wrapped_skip_clipped(
&mut self,
start_row: u16,
start_col: u16,
text: &str,
fg: Color,
bg: Color,
style: Style,
left_margin: u16,
max_row: u16,
wrap_col: u16,
skip_rows: usize,
)
pub fn write_str_wrapped_skip_clipped( &mut self, start_row: u16, start_col: u16, text: &str, fg: Color, bg: Color, style: Style, left_margin: u16, max_row: u16, wrap_col: u16, skip_rows: usize, )
Write a string with wrapping, skip the first skip_rows visual rows,
and clip rendering at the given maximum row and wrap column.
wrap_col is the maximum column number; text wraps when col >= wrap_col.
skip_rows is the number of visual rows to skip before rendering.
max_row is the maximum row; text stops when row > max_row.
left_margin is the column where wrapped lines start.
Sourcepub fn fill_rect(&mut self, rect: Rect, cell: Cell)
pub fn fill_rect(&mut self, rect: Rect, cell: Cell)
Fill a rectangular area with the given cell.
Sourcepub fn hline(
&mut self,
row: u16,
col_start: u16,
col_end: u16,
ch: char,
fg: Color,
bg: Color,
)
pub fn hline( &mut self, row: u16, col_start: u16, col_end: u16, ch: char, fg: Color, bg: Color, )
Draw a horizontal line using the given character.
Sourcepub fn vline(
&mut self,
col: u16,
row_start: u16,
row_end: u16,
ch: char,
fg: Color,
bg: Color,
)
pub fn vline( &mut self, col: u16, row_start: u16, row_end: u16, ch: char, fg: Color, bg: Color, )
Draw a vertical line using the given character.
Sourcepub fn draw_box(&mut self, rect: Rect, fg: Color, bg: Color, style: Style)
pub fn draw_box(&mut self, rect: Rect, fg: Color, bg: Color, style: Style)
Draw a box (border) around a rectangular area.
Sourcepub fn diff_from(&self, previous: &Screen) -> Vec<DiffOp>
pub fn diff_from(&self, previous: &Screen) -> Vec<DiffOp>
Compute the diff between this screen and a previous frame.
Returns a list of DiffOp entries that, when applied in order,
will bring the terminal from the previous state to the current state.
Sourcepub fn render_diff(ops: &[DiffOp], width: u16) -> String
pub fn render_diff(ops: &[DiffOp], width: u16) -> String
Render a list of diff operations to an ANSI escape sequence string.
This is the core of the efficient rendering: we only write cells that actually changed, and we batch cursor movements.