Skip to main content

Crate uncurses

Crate uncurses 

Source
Expand description

uncurses is a terminal toolkit library for building terminal user interfaces. It hands you the pieces (a cell grid with a diffing renderer, a typed input decoder, ANSI escape helpers, and a raw-mode terminal handle) and stays out of the way: you own the event loop and decide when bytes hit the wire. There is no terminfo database and no widget tree.

§Where to start

Two routes cover most needs. Pick the one that fits your use case.

  • screen::Screen is the facade and the home of the diffing renderer. It owns a terminal and an event::EventSource, tracks the live terminal across frames, and emits only the cells that changed. It also manages raw mode, capability detection, sane default modes, and teardown. Reach for it to drive an interactive app, in either inline or fullscreen layout. See the screen module docs for the full lifecycle.
  • buffer::TextBuffer (and any buffer::Surface) is the stateless route. Paint a full frame into an in-memory grid and serialize it to escape bytes with the text::Encode trait. There is no renderer and no terminal session, which makes it the tool for one-shot frames, snapshot tests, transcripts, and append-style output.

§Quick start with Screen

use uncurses::buffer::SurfaceMut;
use uncurses::color::Color;
use uncurses::screen::Screen;
use uncurses::style::Style;
use uncurses::text::TextSurface;

let mut screen = Screen::stdio()?;
screen.init()?; // raw mode + capability detection

let style = Style::default().bold().fg(Color::Green);
screen.set_str((0, 0), "Hello, terminal!", style);
screen.render()?; // stage the diff and flush it

screen.finish() // tear down modes and restore the terminal

§Quick start with TextBuffer

Paint a buffer::TextBuffer and serialize it yourself, with no terminal involved:

use uncurses::buffer::TextBuffer;
use uncurses::color::Color;
use uncurses::style::Style;
use uncurses::text::{Encode, TextSurface};

let mut frame = TextBuffer::new(80, 24);
let style = Style::default()
    .bold()
    .fg(Color::Green);
frame.set_str((0, 0), "Hello, terminal!", style);

// Serialize the painted grid to escape bytes you can write anywhere.
let bytes = frame.display().to_string();
assert!(bytes.contains("Hello, terminal!"));

§The module map

ModuleWhat lives there
screenThe self-managing Screen facade and its diffing renderer.
bufferCell-grid storage (Buffer, TextBuffer, Window) and the Surface / SurfaceMut traits every drawable shares.
textText shaping, width measurement, the TextSurface painting trait that adds set_str to any surface, and the Encode trait that serializes a surface to escapes.
styleStyle, colors, attributes, and SGR plus hyperlink (OSC 8) encoding.
colorColor types and capability Profiles with automatic downsampling.
eventThe EventSource decoder, typed Event values, and (with the async feature) an EventStream.
ansiRaw escape-sequence encoders and parsers for the cursor, modes, colors, queries, and the long tail of terminal control.
terminalThe Terminal handle, raw-mode lifecycle, window-size queries, and environment snapshot.
cellThe Cell value type.
unicodeGrapheme-cluster segmentation and other Unicode text primitives.
layoutPosition, Size, and Rect geometry.

§Output buffering and flushing

Painting is infallible. Drawing cells with set_str, set_cell, and friends only updates an in-memory frame; nothing is written until you call render, which diffs that frame against the terminal and writes just the changed cells.

Mode changes are applied immediately. Entering the alternate screen, hiding the cursor, enabling mouse reporting, setting the title, and similar switches write their escape sequence on the spot. A stateless TextBuffer has no writer of its own: encode hands you the bytes and you decide where they go.

Modules§

ansi
ANSI and terminal-control sequence subsystem.
buffer
Cell-grid storage and surface traits.
cell
Terminal cell values and grapheme segmentation.
color
Terminal color values, palettes, and capability profiles.
event
Terminal events and event-stream decoding.
layout
Geometry primitives for the cell grid.
screen
Screen — a self-managing terminal application facade.
style
Text style values and terminal SGR/OSC 8 rendering.
terminal
Terminal handles, raw-mode state, window-size queries, and tty helpers.
text
Text measurement and string painting for terminal-cell surfaces.
unicode
Unicode text primitives.