Home

Awesome

Ratatui-image

CI Badge Crate Badge Docs Badge

Showcase:

Screen recording

Image widgets with multiple graphics protocol backends for ratatui

ratatui is an immediate-mode TUI library. ratatui-image tackles 3 general problems when rendering images with an immediate-mode TUI:

Query the terminal for available graphics protocols

Some terminals may implement one or more graphics protocols, such as Sixels, or the iTerm2 or Kitty graphics protocols. Guess by env vars. If that fails, query the terminal with some control sequences. Fallback to "halfblocks" which uses some unicode half-block characters with fore- and background colors.

Query the terminal for the font-size in pixels.

If there is an actual graphics protocol available, it is necessary to know the font-size to be able to map the image pixels to character cell area. The image can be resized, fit, or cropped to an area. Query the terminal for the window and columns/rows sizes, and derive the font-size.

Render the image by the means of the guessed protocol.

Some protocols, like Sixels, are essentially "immediate-mode", but we still need to avoid the TUI from overwriting the image area, even with blank characters. Other protocols, like Kitty, are essentially stateful, but at least provide a way to re-render an image that has been loaded, at a different or same position.

Quick start

use ratatui::{backend::TestBackend, Terminal, Frame};
use ratatui_image::{picker::Picker, StatefulImage, protocol::StatefulProtocol};

struct App {
    // We need to hold the render state.
    image: StatefulProtocol,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let backend = TestBackend::new(80, 30);
    let mut terminal = Terminal::new(backend)?;

    // Should use Picker::from_query_stdio() to get the font size and protocol,
    // but we can't put that here because that would break doctests!
    let mut picker = Picker::from_fontsize((8, 12));

    // Load an image with the image crate.
    let dyn_img = image::io::Reader::open("./assets/Ada.png")?.decode()?;

    // Create the Protocol which will be used by the widget.
    let image = picker.new_resize_protocol(dyn_img);

    let mut app = App { image };

    // This would be your typical `loop {` in a real app:
    terminal.draw(|f| ui(f, &mut app))?;

    Ok(())
}

fn ui(f: &mut Frame<'_>, app: &mut App) {
    // The image widget.
    let image = StatefulImage::default();
    // Render with the protocol state.
    f.render_stateful_widget(image, f.area(), &mut app.image);
}

The [picker::Picker] helper is there to do all this font-size and graphics-protocol guessing, and also to map character-cell-size to pixel size so that we can e.g. "fit" an image inside a desired columns+rows bound, and so on.

Widget choice

Examples

The lib also includes a binary that renders an image file, but it is focused on testing.

Features

Compatibility matrix

Compatibility and QA:

TerminalProtocolFixedNotes
XtermSixel✔️Run with -ti 340 to make sure sixel support is enabled.
FootSixel✔️Wayland.
KittyKitty✔️
WeztermiTerm2✔️Also would support Sixel and Kitty, but only iTerm2 actually works bug-free.
AlacrittySixelThere is a sixel fork, but it's probably never getting merged, and does not clear graphics.
iTerm2iTerm2Feedback from mac users wanted.
KonsoleSixelPossibly fixed in 24.12
ContourSixelDoes not clear graphics.
ctxSixelBuggy.
BlackboxSixelUntested.

A basic screenshot test is run with xterm on Xvfb in the CI (or cargo make screenshot-xvfb && cargo make screenshot-diff).

Halfblocks should work in all terminals, even if the font size could not be detected, with a 4:8 pixel ratio.

Known issues

SummaryLink
Termwiz backend does not work at all#1
Sixel image rendered on the last line of terminal causes a scroll#57
Tmux needs a revisit-

Projects that use ratatui-image

Comparison

Contributing

PRs and issues/discussions welcome!

You can run an aproximation of the CI with cargo make ci. I must manually approve CI runs for new PRs to prevent github-action attacks. The demo is useful to test that everything works correctly.

License: MIT