Skip to main content

Crate uncurses

Crate uncurses 

Source
Expand description

uncurses is a Rust library for building terminal user interfaces. It provides a direct, framework-free way to draw to the terminal and read input, giving you control over every cell and your own event loop, whether you run inline, take over the full screen, mix the two, or leave the console unmanaged and just shape your output. 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 decides nothing for you. There is no terminfo database and no widget tree.

§Where to start

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

  • program::Program is the interactive facade. It owns a terminal, an event::EventSource, and a Screen to draw with. It manages raw mode, terminal modes, capability tracking, and teardown, and hands you the screen through screen / screen_mut. Reach for it to drive an interactive app, inline or fullscreen. See the program module docs for the full lifecycle.
  • screen::Screen is the diffing renderer on its own: a cell grid, a renderer, and any Write. You paint cells and call render; it emits only what changed. It reads no input and touches no terminal mode, so it stands alone for output-only programs, tests, and offscreen rendering.
  • 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 Program

use uncurses::color::Color;
use uncurses::program::Program;
use uncurses::style::Style;
use uncurses::text::TextSurface;

let mut program = Program::stdio()?;
program.init()?; // raw mode

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

program.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 lookups.
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.

Re-exports§

pub use libc;

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.
program
Program — the interactive terminal session.
screen
Screen — the cell-diff renderer you draw into.
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.