Struct ratatui::backend::TermwizBackend

source ·
pub struct TermwizBackend { /* private fields */ }
Available on crate feature termwiz only.
Expand description

A Backend implementation that uses Termwiz to render to the terminal.

The TermwizBackend struct is a wrapper around a BufferedTerminal, which is used to send commands to the terminal. It provides methods for drawing content, manipulating the cursor, and clearing the terminal screen.

Most applications should not call the methods on TermwizBackend directly, but will instead use the Terminal struct, which provides a more ergonomic interface.

This backend automatically enables raw mode and switches to the alternate screen when it is created using the TermwizBackend::new method (and disables raw mode and returns to the main screen when dropped). Use the TermwizBackend::with_buffered_terminal to create a new instance with a custom BufferedTerminal if this is not desired.

§Example

use ratatui::prelude::*;

let backend = TermwizBackend::new()?;
let mut terminal = Terminal::new(backend)?;

terminal.clear()?;
terminal.draw(|frame| {
    // -- snip --
})?;

See the the Examples directory for more examples. See the backend module documentation for more details on raw mode and alternate screen.

Implementations§

source§

impl TermwizBackend

source

pub fn new() -> Result<Self, Box<dyn Error>>

Creates a new Termwiz backend instance.

The backend will automatically enable raw mode and enter the alternate screen.

§Errors

Returns an error if unable to do any of the following:

  • query the terminal capabilities.
  • enter raw mode.
  • enter the alternate screen.
  • create the system or buffered terminal.
§Example
let backend = TermwizBackend::new()?;
Examples found in repository?
examples/demo/termwiz.rs (line 15)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box<dyn Error>> {
    let backend = TermwizBackend::new()?;
    let mut terminal = Terminal::new(backend)?;
    terminal.hide_cursor()?;

    // create app and run it
    let app = App::new("Termwiz Demo", enhanced_graphics);
    let res = run_app(&mut terminal, app, tick_rate);

    terminal.show_cursor()?;
    terminal.flush()?;

    if let Err(err) = res {
        println!("{err:?}");
    }

    Ok(())
}
source

pub const fn with_buffered_terminal( instance: BufferedTerminal<SystemTerminal> ) -> Self

Creates a new Termwiz backend instance with the given buffered terminal.

source

pub const fn buffered_terminal(&self) -> &BufferedTerminal<SystemTerminal>

Returns a reference to the buffered terminal used by the backend.

source

pub fn buffered_terminal_mut(&mut self) -> &mut BufferedTerminal<SystemTerminal>

Returns a mutable reference to the buffered terminal used by the backend.

Examples found in repository?
examples/demo/termwiz.rs (line 45)
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
fn run_app(
    terminal: &mut Terminal<TermwizBackend>,
    mut app: App,
    tick_rate: Duration,
) -> Result<(), Box<dyn Error>> {
    let mut last_tick = Instant::now();
    loop {
        terminal.draw(|f| ui::draw(f, &mut app))?;

        let timeout = tick_rate.saturating_sub(last_tick.elapsed());
        if let Some(input) = terminal
            .backend_mut()
            .buffered_terminal_mut()
            .terminal()
            .poll_input(Some(timeout))?
        {
            match input {
                InputEvent::Key(key_code) => match key_code.key {
                    KeyCode::UpArrow | KeyCode::Char('k') => app.on_up(),
                    KeyCode::DownArrow | KeyCode::Char('j') => app.on_down(),
                    KeyCode::LeftArrow | KeyCode::Char('h') => app.on_left(),
                    KeyCode::RightArrow | KeyCode::Char('l') => app.on_right(),
                    KeyCode::Char(c) => app.on_key(c),
                    _ => {}
                },
                InputEvent::Resized { cols, rows } => {
                    terminal
                        .backend_mut()
                        .buffered_terminal_mut()
                        .resize(cols, rows);
                }
                _ => {}
            }
        }

        if last_tick.elapsed() >= tick_rate {
            app.on_tick();
            last_tick = Instant::now();
        }
        if app.should_quit {
            return Ok(());
        }
    }
}

Trait Implementations§

source§

impl Backend for TermwizBackend

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(&mut self) -> Result<(u16, u16)>

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

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

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

Clears the whole terminal screen Read more
source§

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

Get the size of the terminal screen in columns/rows as a Rect. 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 append_lines(&mut self, _n: u16) -> Result<()>

Insert n line breaks to the 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

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> 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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.