ratatui::backend

Struct TestBackend

Source
pub struct TestBackend { /* private fields */ }
Expand description

A Backend implementation used for integration testing that renders to an memory buffer.

Note: that although many of the integration and unit tests in ratatui are written using this backend, it is preferable to write unit tests for widgets directly against the buffer rather than using this backend. This backend is intended for integration tests that test the entire terminal UI.

§Example

use ratatui::backend::{Backend, TestBackend};

let mut backend = TestBackend::new(10, 2);
backend.clear()?;
backend.assert_buffer_lines(["          "; 2]);

Implementations§

Source§

impl TestBackend

Source

pub fn new(width: u16, height: u16) -> Self

Creates a new TestBackend with the specified width and height.

Source

pub fn with_lines<'line, Lines>(lines: Lines) -> Self
where Lines: IntoIterator, Lines::Item: Into<Line<'line>>,

Creates a new TestBackend with the specified lines as the initial screen state.

The backend’s screen size is determined from the initial lines.

Source

pub const fn buffer(&self) -> &Buffer

Returns a reference to the internal buffer of the TestBackend.

Source

pub const fn scrollback(&self) -> &Buffer

Returns a reference to the internal scrollback buffer of the TestBackend.

The scrollback buffer represents the part of the screen that is currently hidden from view, but that could be accessed by scrolling back in the terminal’s history. This would normally be done using the terminal’s scrollbar or an equivalent keyboard shortcut.

The scrollback buffer starts out empty. Lines are appended when they scroll off the top of the main buffer. This happens when lines are appended to the bottom of the main buffer using Backend::append_lines.

The scrollback buffer has a maximum height of u16::MAX. If lines are appended to the bottom of the scrollback buffer when it is at its maximum height, a corresponding number of lines will be removed from the top.

Source

pub fn resize(&mut self, width: u16, height: u16)

Resizes the TestBackend to the specified width and height.

Source

pub fn assert_buffer(&self, expected: &Buffer)

Asserts that the TestBackend’s buffer is equal to the expected buffer.

This is a shortcut for assert_eq!(self.buffer(), &expected).

§Panics

When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.

Source

pub fn assert_scrollback(&self, expected: &Buffer)

Asserts that the TestBackend’s scrollback buffer is equal to the expected buffer.

This is a shortcut for assert_eq!(self.scrollback(), &expected).

§Panics

When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.

Source

pub fn assert_scrollback_empty(&self)

Asserts that the TestBackend’s scrollback buffer is empty.

§Panics

When the scrollback buffer is not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.

Source

pub fn assert_buffer_lines<'line, Lines>(&self, expected: Lines)
where Lines: IntoIterator, Lines::Item: Into<Line<'line>>,

Asserts that the TestBackend’s buffer is equal to the expected lines.

This is a shortcut for assert_eq!(self.buffer(), &Buffer::with_lines(expected)).

§Panics

When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.

Source

pub fn assert_scrollback_lines<'line, Lines>(&self, expected: Lines)
where Lines: IntoIterator, Lines::Item: Into<Line<'line>>,

Asserts that the TestBackend’s scrollback buffer is equal to the expected lines.

This is a shortcut for assert_eq!(self.scrollback(), &Buffer::with_lines(expected)).

§Panics

When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.

Source

pub fn assert_cursor_position<P: Into<Position>>(&mut self, position: P)

Asserts that the TestBackend’s cursor position is equal to the expected one.

This is a shortcut for assert_eq!(self.get_cursor_position().unwrap(), expected).

§Panics

When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual position.

Trait Implementations§

Source§

impl Backend for TestBackend

Source§

fn append_lines(&mut self, line_count: u16) -> Result<()>

Inserts n line breaks at the current cursor position.

After the insertion, the cursor x position will be incremented by 1 (unless it’s already at the end of line). This is a common behaviour of terminals in raw mode.

If the number of lines to append is fewer than the number of lines in the buffer after the cursor y position then the cursor is moved down by n rows.

If the number of lines to append is greater than the number of lines in the buffer after the cursor y position then that number of empty lines (at most the buffer’s height in this case but this limit is instead replaced with scrolling in most backend implementations) will be added after the current position and the cursor will be moved to the last row.

Source§

fn draw<'a, I>(&mut self, content: I) -> Result<()>
where I: Iterator<Item = (u16, u16, &'a Cell)>,

Draw the given content to the terminal screen. Read more
Source§

fn hide_cursor(&mut self) -> Result<()>

Hide the cursor on the terminal screen. Read more
Source§

fn show_cursor(&mut self) -> Result<()>

Show the cursor on the terminal screen. Read more
Source§

fn get_cursor_position(&mut self) -> Result<Position>

Get the current cursor position on the terminal screen. Read more
Source§

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. Read more
Source§

fn clear(&mut self) -> Result<()>

Clears the whole terminal screen Read more
Source§

fn clear_region(&mut self, clear_type: ClearType) -> Result<()>

Clears a specific region of the terminal specified by the ClearType parameter Read more
Source§

fn size(&self) -> Result<Size>

Get the size of the terminal screen in columns/rows as a Size. Read more
Source§

fn window_size(&mut self) -> Result<WindowSize>

Get the size of the terminal screen in columns/rows and pixels as a WindowSize. Read more
Source§

fn flush(&mut self) -> Result<()>

Flush any buffered content to the terminal screen.
Source§

fn get_cursor(&mut self) -> Result<(u16, u16)>

👎Deprecated: the method get_cursor_position indicates more clearly what about the cursor to get
Get the current cursor position on the terminal screen. Read more
Source§

fn set_cursor(&mut self, x: u16, y: u16) -> Result<()>

👎Deprecated: the method set_cursor_position indicates more clearly what about the cursor to set
Set the cursor position on the terminal screen to the given x and y coordinates. Read more
Source§

impl Clone for TestBackend

Source§

fn clone(&self) -> TestBackend

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TestBackend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for TestBackend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the TestBackend for display by calling the buffer_view function on its internal buffer.

Source§

impl Hash for TestBackend

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TestBackend

Source§

fn eq(&self, other: &TestBackend) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for TestBackend

Source§

impl StructuralPartialEq for TestBackend

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToLine for T
where T: Display,

Source§

fn to_line(&self) -> Line<'_>

Converts the value to a Line.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSpan for T
where T: Display,

Source§

fn to_span(&self) -> Span<'_>

Converts the value to a Span.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToText for T
where T: Display,

Source§

fn to_text(&self) -> Text<'_>

Converts the value to a Text.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.