pub struct Buffer {
pub area: Rect,
pub content: Vec<Cell>,
}
Expand description
A buffer that maps to the desired content of the terminal after the draw call
No widget in the library interacts directly with the terminal. Instead each of them is required to draw their state to an intermediate buffer. It is basically a grid where each cell contains a grapheme, a foreground color and a background color. This grid will then be used to output the appropriate escape sequences and characters to draw the UI as the user has defined it.
§Examples:
use ratatui::{buffer::Cell, prelude::*};
let mut buf = Buffer::empty(Rect {
x: 0,
y: 0,
width: 10,
height: 5,
});
buf.get_mut(0, 2).set_symbol("x");
assert_eq!(buf.get(0, 2).symbol(), "x");
buf.set_string(
3,
0,
"string",
Style::default().fg(Color::Red).bg(Color::White),
);
let cell = buf.get(5, 0);
assert_eq!(cell.symbol(), "r");
assert_eq!(cell.fg, Color::Red);
assert_eq!(cell.bg, Color::White);
buf.get_mut(5, 0).set_char('x');
assert_eq!(buf.get(5, 0).symbol(), "x");
RunFields§
§area: Rect
The area represented by this buffer
content: Vec<Cell>
The content of the buffer. The length of this Vec should always be equal to area.width * area.height
Implementations§
source§impl Buffer
impl Buffer
sourcepub fn filled(area: Rect, cell: &Cell) -> Buffer
pub fn filled(area: Rect, cell: &Cell) -> Buffer
Returns a Buffer with all cells initialized with the attributes of the given Cell
sourcepub fn with_lines<'a, Iter>(lines: Iter) -> Buffer
pub fn with_lines<'a, Iter>(lines: Iter) -> Buffer
Returns a Buffer containing the given lines
sourcepub fn get(&self, x: u16, y: u16) -> &Cell
pub fn get(&self, x: u16, y: u16) -> &Cell
Returns a reference to Cell at the given coordinates
sourcepub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell
pub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell
Returns a mutable reference to Cell at the given coordinates
sourcepub fn index_of(&self, x: u16, y: u16) -> usize
pub fn index_of(&self, x: u16, y: u16) -> usize
Returns the index in the Vec<Cell>
for the given global (x, y) coordinates.
Global coordinates are offset by the Buffer’s area offset (x
/y
).
§Examples
let rect = Rect::new(200, 100, 10, 10);
let buffer = Buffer::empty(rect);
// Global coordinates to the top corner of this buffer's area
assert_eq!(buffer.index_of(200, 100), 0);
Run§Panics
Panics when given an coordinate that is outside of this Buffer’s area.
sourcepub fn pos_of(&self, i: usize) -> (u16, u16)
pub fn pos_of(&self, i: usize) -> (u16, u16)
Returns the (global) coordinates of a cell given its index
Global coordinates are offset by the Buffer’s area offset (x
/y
).
§Examples
let rect = Rect::new(200, 100, 10, 10);
let buffer = Buffer::empty(rect);
assert_eq!(buffer.pos_of(0), (200, 100));
assert_eq!(buffer.pos_of(14), (204, 101));
Run§Panics
Panics when given an index that is outside the Buffer’s content.
sourcepub fn set_string<T, S>(&mut self, x: u16, y: u16, string: T, style: S)
pub fn set_string<T, S>(&mut self, x: u16, y: u16, string: T, style: S)
Print a string, starting at the position (x, y)
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
sourcepub fn set_stringn<T, S>(
&mut self,
x: u16,
y: u16,
string: T,
width: usize,
style: S
) -> (u16, u16)
pub fn set_stringn<T, S>( &mut self, x: u16, y: u16, string: T, width: usize, style: S ) -> (u16, u16)
Print at most the first n characters of a string if enough space is available until the end of the line
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
sourcepub fn set_line(
&mut self,
x: u16,
y: u16,
line: &Line<'_>,
width: u16
) -> (u16, u16)
pub fn set_line( &mut self, x: u16, y: u16, line: &Line<'_>, width: u16 ) -> (u16, u16)
Print a line, starting at the position (x, y)
sourcepub fn set_span(
&mut self,
x: u16,
y: u16,
span: &Span<'_>,
width: u16
) -> (u16, u16)
pub fn set_span( &mut self, x: u16, y: u16, span: &Span<'_>, width: u16 ) -> (u16, u16)
Print a span, starting at the position (x, y)
sourcepub fn set_style<S>(&mut self, area: Rect, style: S)
pub fn set_style<S>(&mut self, area: Rect, style: S)
Set the style of all cells in the given area.
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
sourcepub fn resize(&mut self, area: Rect)
pub fn resize(&mut self, area: Rect)
Resize the buffer so that the mapped area matches the given area and that the buffer length is equal to area.width * area.height
sourcepub fn diff<'a>(&self, other: &'a Buffer) -> Vec<(u16, u16, &'a Cell)>
pub fn diff<'a>(&self, other: &'a Buffer) -> Vec<(u16, u16, &'a Cell)>
Builds a minimal sequence of coordinates and Cells necessary to update the UI from self to other.
We’re assuming that buffers are well-formed, that is no double-width cell is followed by a non-blank cell.
§Multi-width characters handling:
(Index:) `01`
Prev: `コ`
Next: `aa`
Updates: `0: a, 1: a'
(Index:) `01`
Prev: `a `
Next: `コ`
Updates: `0: コ` (double width symbol at index 0 - skip index 1)
(Index:) `012`
Prev: `aaa`
Next: `aコ`
Updates: `0: a, 1: コ` (double width symbol at index 1 - skip index 2)
Trait Implementations§
source§impl Debug for Buffer
impl Debug for Buffer
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Writes a debug representation of the buffer to the given formatter.
The format is like a pretty printed struct, with the following fields:
area
: displayed asRect { x: 1, y: 2, width: 3, height: 4 }
content
: displayed as a list of strings representing the content of the bufferstyles
: displayed as a list of:{ x: 1, y: 2, fg: Color::Red, bg: Color::Blue, modifier: Modifier::BOLD }
only showing a value when there is a change in style.