The Ratatui backend

If you already write your UI with Ratatui widgets, you do not have to give that up to get uncurses underneath. The uncurses-ratatui crate is a Ratatui Backend that wraps an uncurses Program: Ratatui keeps writing widgets, the program owns input and terminal modes, and its Screen diffs the frames and writes only the necessary bytes.

Install

Add the backend alongside Ratatui:

toml
[dependencies]
uncurses-ratatui = { git = "https://github.com/aymanbagabas/uncurses" }
ratatui = "0.30"

The features mirror the core crate: unicode-rs (default), icu, and async.

Setup and teardown

try_init enters raw mode, switches to the alternate screen, and hands you a ready Ratatui Terminal; restore restores the terminal state. One call each, bracketing your app.

rust
use std::io;

fn main() -> io::Result<()> {
    let mut terminal = uncurses_ratatui::try_init()?;
    let result = run(&mut terminal);
    uncurses_ratatui::restore(&mut terminal);
    result
}

fn run(_: &mut uncurses_ratatui::DefaultTerminal) -> io::Result<()> {
    Ok(())
}

try_init returns an io::Result; if you would rather panic on failure, init gives you the terminal directly. There are *_with_options variants when you want to pass Ratatui TerminalOptions and uncurses ProgramOptions. ProgramOptions is re-exported from uncurses_ratatui and defined in uncurses::program. It selects bracketed paste, mouse input, and legacy keys. It also holds adoption preferences for grapheme clusters, in-band resize, and synchronized output. A separate Probe selects queries. uncurses_ratatui re-exports that type.

rust
use std::io;

use ratatui::{TerminalOptions, Viewport};
use uncurses_ratatui::ProgramOptions;

fn main() -> io::Result<()> {
    let program_options = ProgramOptions {
        bracketed_paste: false,
        ..ProgramOptions::default()
    };

    let mut terminal = uncurses_ratatui::try_init_with_options(
        TerminalOptions {
            viewport: Viewport::Fullscreen,
        },
        program_options,
    )?;

    uncurses_ratatui::restore(&mut terminal);
    Ok(())
}

Drawing and input

terminal.draw is plain Ratatui. Render any widget into the frame; uncurses turns the resulting buffer into the smallest possible update. Input comes from uncurses through the backend.

rust
use uncurses::event::Event;
use uncurses_ratatui::DefaultTerminal;

fn run(terminal: &mut DefaultTerminal) -> std::io::Result<()> {
    loop {
        terminal.draw(|frame| {
            frame.render_widget("Hello from ratatui, via uncurses", frame.area());
        })?;

        let backend = terminal.backend_mut();
        if !backend.poll_event(None)? {
            continue;
        }
        let Some(ev) = backend.try_read_event()? else {
            continue;
        };
        if let Event::KeyPress(_) = ev {
            break;
        }
    }
    Ok(())
}

backend_mut exposes the same read_event, poll_event, and try_read_event you use on a bare Program, producing uncurses Event values. It also exposes the wrapped program as program() and program_mut(), so renderer access is backend.program_mut().screen_mut(). Your render layer is Ratatui and your input layer is uncurses, each doing what it is best at. Features covered in the other guides, including mouse input, paste, async events, and terminal queries, work through the backend.

Cursor reports

Ratatui asks the backend for the cursor position when it builds an inline terminal and when a draw finds a new size. The backend sends a cursor request and waits up to 100 ms for the first cursor report. It drops the F3 key that a row-1 report also spells, and puts the other events back for your loop. A cursor report carries no request ID, so an earlier request that still waits for its reply gives the backend the wrong position.

A selected width measurement sends its own cursor request. Use queries without cursor measurements for a bounded wait before draw. A timeout leaves cursor replies pending. This current-source fragment selects terminal-name discovery after terminal setup:

rust
use std::time::{Duration, Instant};
use std::io::Write;

use uncurses_ratatui::Probe;
use uncurses::event::Event;

let program = terminal.backend_mut().program_mut();
let mut probe = Probe::new(program.env());
probe.terminal_name = true;
let mut caps = probe.query_capabilities(program.screen_mut(), true)?;
program.screen_mut().flush()?;
let mut keys = Vec::new();
let deadline = Instant::now() + Duration::from_millis(300);
'wait: while let Some(remaining) = deadline.checked_duration_since(Instant::now()) {
    if !program.poll_event(Some(remaining))? {
        break;
    }
    while let Some(event) = program.try_read_event()? {
        caps.record(&event);
        match event {
            Event::PrimaryDeviceAttributes(_) => break 'wait,
            Event::KeyPress(_) | Event::KeyRelease(_) => keys.push(event),
            _ => {}
        }
    }
}
for event in keys.into_iter().rev() {
    program.unread_event(event);
}
program.apply_defaults(&caps)?;
terminal.draw(|frame| frame.render_widget("ready", frame.area()))?;

Apply the same rule to your own cursor requests: read each reply before the next draw. See examples/examples/ratatui_inline.rs for this order. Process other application events during collection, or retain them for the normal loop. Keep identified late replies recordable after the deadline.

Async input

With the async feature, the backend exposes its own event_stream() for a tokio::select! loop. It yields the same events as the sync reads:

rust
use std::time::Duration;

use tokio_stream::StreamExt;

let mut events = terminal.backend_mut().event_stream();
let mut tick = tokio::time::interval(Duration::from_millis(16));

loop {
    tokio::select! {
        maybe = events.next() => {
            let Some(ev) = maybe else { break };
            let _ev = ev?;
            // handle input
        }
        _ = tick.tick() => {
            terminal.draw(|frame| {
                frame.render_widget("async ratatui", frame.area());
            })?;
        }
    }
}

The stream shares the program’s decoder by handle, so you can hold it and still call mutable backend methods in the same loop. See the async events guide for the full pattern.

See the ratatui_minimal example for the smallest complete program, and browse the other ratatui_* examples for more, such as ratatui_popup, ratatui_inline, and ratatui_user_input.

Edit this page

Search documentation

Type to search the documentation.