katui

Getting started

In this guide you add katui to a project, run a small app, and see what katui does when it starts.

What you need

Make a project

cargo new hello-katui
cd hello-katui
cargo add katui --features app

The app feature brings in the app runner and the widgets. Other features add more later: game for games, html for documents, ratatui for the ratatui backend.

Write the app

Replace src/main.rs with this:

use katui::app::{Control, UiApp, run};
use katui::ui::{Align, Element};
use katui::widgets::{label, panel, primary_button};

#[derive(Default)]
struct Counter(u32);

impl UiApp for Counter {
    type Message = ();

    fn view(&self) -> Element<()> {
        panel([
            label(format!("Clicked {} times", self.0)),
            primary_button("Add one", ()),
        ])
        .align(Align::Center)
    }

    fn update(&mut self, (): ()) -> Control {
        self.0 += 1;
        Control::Redraw
    }
}

fn main() -> Result<(), katui::rt::RtError> {
    run(Counter::default())
}

Then run it:

cargo run

You see a panel with a line of text and a button. Click the button and the count goes up. Press Ctrl+C to quit, and your terminal comes back the way it was.

What just happened

The app has two parts.

You never draw anything yourself. katui calls view again after each update, compares the result with the last frame, and sends the terminal only the parts that changed.

When run starts, katui also asks the terminal what it can do and picks kitty graphics, sixel or quarter blocks from the answer. Your code stays the same in every case. To see what katui found in your terminal, run just info in a clone of the katui repository. To pick the backend yourself, set KATUI_GRAPHICS to kitty, sixel or blocks:

KATUI_GRAPHICS=blocks cargo run

Next

Read Pixel UI apps to build something bigger: a shopping list with a text field, a list and keyboard shortcuts.