pub trait Backend {
Show 13 methods
// Required methods
fn draw<'a, I>(&mut self, content: I) -> Result<()>
where I: Iterator<Item = (u16, u16, &'a Cell)>;
fn hide_cursor(&mut self) -> Result<()>;
fn show_cursor(&mut self) -> Result<()>;
fn get_cursor_position(&mut self) -> Result<Position>;
fn set_cursor_position<P: Into<Position>>(
&mut self,
position: P,
) -> Result<()>;
fn clear(&mut self) -> Result<()>;
fn size(&self) -> Result<Size>;
fn window_size(&mut self) -> Result<WindowSize>;
fn flush(&mut self) -> Result<()>;
// Provided methods
fn append_lines(&mut self, _n: u16) -> Result<()> { ... }
fn get_cursor(&mut self) -> Result<(u16, u16)> { ... }
fn set_cursor(&mut self, x: u16, y: u16) -> Result<()> { ... }
fn clear_region(&mut self, clear_type: ClearType) -> Result<()> { ... }
}
Expand description
The Backend
trait provides an abstraction over different terminal libraries. It defines the
methods required to draw content, manipulate the cursor, and clear the terminal screen.
Most applications should not need to interact with the Backend
trait directly as the
Terminal
struct provides a higher level interface for interacting with the terminal.
Required Methods§
Sourcefn draw<'a, I>(&mut self, content: I) -> Result<()>
fn draw<'a, I>(&mut self, content: I) -> Result<()>
Draw the given content to the terminal screen.
The content is provided as an iterator over (u16, u16, &Cell)
tuples, where the first two
elements represent the x and y coordinates, and the third element is a reference to the
Cell
to be drawn.
Sourcefn hide_cursor(&mut self) -> Result<()>
fn hide_cursor(&mut self) -> Result<()>
Hide the cursor on the terminal screen.
See also show_cursor
.
§Example
use ratatui::backend::Backend;
backend.hide_cursor()?;
// do something with hidden cursor
backend.show_cursor()?;
Sourcefn show_cursor(&mut self) -> Result<()>
fn show_cursor(&mut self) -> Result<()>
Show the cursor on the terminal screen.
See hide_cursor
for an example.
Sourcefn get_cursor_position(&mut self) -> Result<Position>
fn get_cursor_position(&mut self) -> Result<Position>
Get the current cursor position on the terminal screen.
The returned tuple contains the x and y coordinates of the cursor. The origin (0, 0) is at the top left corner of the screen.
See set_cursor_position
for an example.
Sourcefn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> Result<()>
fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> Result<()>
Set the cursor position on the terminal screen to the given x and y coordinates.
The origin (0, 0) is at the top left corner of the screen.
§Example
use ratatui::{backend::Backend, layout::Position};
backend.set_cursor_position(Position { x: 10, y: 20 })?;
assert_eq!(backend.get_cursor_position()?, Position { x: 10, y: 20 });
Sourcefn window_size(&mut self) -> Result<WindowSize>
fn window_size(&mut self) -> Result<WindowSize>
Get the size of the terminal screen in columns/rows and pixels as a WindowSize
.
The reason for this not returning only the pixel size, given the redundancy with the
size()
method, is that the underlying backends most likely get both values with one
syscall, and the user is also most likely to need columns and rows along with pixel size.
Provided Methods§
Sourcefn append_lines(&mut self, _n: u16) -> Result<()>
fn append_lines(&mut self, _n: u16) -> Result<()>
Insert n
line breaks to the terminal screen.
This method is optional and may not be implemented by all backends.
Sourcefn get_cursor(&mut self) -> Result<(u16, u16)>
👎Deprecated: the method get_cursor_position indicates more clearly what about the cursor to get
fn get_cursor(&mut self) -> Result<(u16, u16)>
Get the current cursor position on the terminal screen.
The returned tuple contains the x and y coordinates of the cursor. The origin (0, 0) is at the top left corner of the screen.
Sourcefn set_cursor(&mut self, x: u16, y: u16) -> Result<()>
👎Deprecated: the method set_cursor_position indicates more clearly what about the cursor to set
fn set_cursor(&mut self, x: u16, y: u16) -> Result<()>
Set the cursor position on the terminal screen to the given x and y coordinates.
The origin (0, 0) is at the top left corner of the screen.
Sourcefn clear_region(&mut self, clear_type: ClearType) -> Result<()>
fn clear_region(&mut self, clear_type: ClearType) -> Result<()>
Clears a specific region of the terminal specified by the ClearType
parameter
This method is optional and may not be implemented by all backends. The default
implementation calls clear
if the clear_type
is ClearType::All
and returns an
error otherwise.
§Example
use ratatui::backend::{Backend, ClearType};
backend.clear_region(ClearType::All)?;
§Errors
This method will return an error if the terminal screen could not be cleared. It will also
return an error if the clear_type
is not supported by the backend.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.