Your first app
Time to put it together. We will build a small interactive counter: a number you
nudge up and down with the arrow keys, reset with r, and quit with q. It is
tiny, but it has every moving part a real app has: a session, some state, a draw
function, and an event loop.
The shape of an app
Almost every uncurses app follows the same four-beat structure: a type that owns the program and app state, a setup step, a loop, and a teardown step.
flowchart TB
accTitle: The application lifecycle
accDescr {
The application opens its Program, initializes state, and configures the terminal.
It repeats the read, update, and redraw cycle, then finishes the Program.
}
begin["new: open the program, initialize state"]
begin --> setup["start: configure the terminal"]
setup --> eventloop["run: read event, update state, redraw"]
eventloop --> eventloop
eventloop --> teardown["stop: finish the program"]We will hang those four beats off a single App struct. The snippets below are
fragments from one complete file.
Setting up
new opens the program and seeds the counter at zero. start configures the
terminal after the caller owns the app and can restore it on an error.
use uncurses::buffer::SurfaceMut;
use uncurses::color::Color;
use uncurses::event::Event;
use uncurses::program::Program;
use uncurses::style::Style;
use uncurses::terminal::{Stdin, Stdout};
use uncurses::text::TextSurface;
struct App {
program: Program<Stdin, Stdout>,
count: i64,
}
impl App {
fn new() -> std::io::Result<Self> {
Ok(Self {
program: Program::stdio()?,
count: 0,
})
}
fn start(&mut self) -> std::io::Result<()> {
self.program.init()?;
self.program.enter_alt_screen()?;
self.program.hide_cursor()
}
}Key matching accepts strings, so the loop can ask whether a key matches "q" or
"ctrl+c" directly.
Drawing a frame
render paints the whole frame from scratch every time: clear the grid, write
the title, the value, and a hint line, then push it with one render call on the
program’s Screen. Painting is cheap and the renderer only sends the cells that
actually changed, so “redraw everything” is the right default.
impl App {
fn render(&mut self) -> std::io::Result<()> {
let screen = self.program.screen_mut();
screen.clear();
let title = Style::default().bold().fg(Color::BrightCyan);
screen.set_str((2, 1), "Counter", title);
let value = Style::default().bold();
screen.set_str((2, 3), &format!("count: {}", self.count), value);
let hint = Style::default().fg(Color::BrightBlack);
screen.set_str((2, 5), "up/down: change r: reset q: quit", hint);
screen.render()
}
}The event loop
run draws once, then blocks on read_event and reacts. Arrows change the
count, r resets it, a resize updates the screen size, and a quit key breaks
the loop. We only redraw when something actually changed.
impl App {
fn run(&mut self) -> std::io::Result<()> {
self.render()?;
loop {
let ev = self.program.read_event()?;
match ev {
Event::KeyPress(ref k) if k.matches_any(["q", "ctrl+c"]) => break,
Event::KeyPress(ref k) if k.matches("up") => self.count += 1,
Event::KeyPress(ref k) if k.matches("down") => self.count -= 1,
Event::KeyPress(ref k) if k.matches("r") => self.count = 0,
Event::Resize(_) => self.program.autoresize()?,
_ => continue,
}
self.render()?;
}
Ok(())
}
}The continue on the catch-all arm skips the redraw for events we ignore, so the
terminal only repaints when the frame would actually differ.
Putting the terminal back
stop consumes the app and finishes the program, restoring the terminal exactly
as it was. main calls new, start, run, and stop in order.
It still calls stop if start or run returns an error.
This gives terminal restoration a chance to run after setup or application errors.
impl App {
fn stop(self) -> std::io::Result<()> {
self.program.finish()
}
}
fn main() -> std::io::Result<()> {
let mut app = App::new()?;
let result = app.start().and_then(|()| app.run());
let stop = app.stop();
result.and(stop)
}That ordering matters: bind the loop result, tear down, then return the result.
If you bubble up the error before stop, the terminal stays in raw mode and your
shell prompt comes back garbled.
Where to go next
That is a complete, well-behaved terminal app in under a hundred lines. From here:
- Add mouse support with
ProgramOptions { mouse: Some(MouseTracking::empty()), ..Default::default() }, then match onEvent::MouseClick. - Lay widgets out by reading
program.screen().width()andprogram.screen().height()and doing the arithmetic, the way thecounterexample centers a button. - Browse the examples for editors, file explorers, inline prompts, and more.
- Dig into the Concepts to understand cells, surfaces, width, and the event model in depth.
