uncurses/lib.rs
1//! `uncurses` is a Rust library for building terminal user interfaces. It
2//! provides a direct, framework-free way to draw to the terminal and read
3//! input, giving you control over every cell and your own event loop, whether
4//! you run inline, take over the full screen, mix the two, or leave the
5//! console unmanaged and just shape your output. It hands you the pieces (a
6//! cell grid with a diffing renderer, a typed input decoder, ANSI escape
7//! helpers, and a raw-mode terminal handle) and decides nothing for you.
8//! There is no terminfo database and no widget tree.
9//!
10//! # Where to start
11//!
12//! Three routes cover most needs. Pick the one that fits your use case.
13//!
14//! - **[`program::Program`]** is the interactive facade. It owns a terminal,
15//! an [`event::EventSource`], and a `Screen` to draw with. It manages raw
16//! mode, terminal modes, capability tracking, and teardown, and hands you
17//! the screen through [`screen`](program::Program::screen) /
18//! [`screen_mut`](program::Program::screen_mut). Reach for it to drive an
19//! interactive app, inline or fullscreen. See the [`program`] module docs
20//! for the full lifecycle.
21//! - **[`screen::Screen`]** is the diffing renderer on its own: a cell grid, a
22//! renderer, and any [`Write`](std::io::Write). You paint cells and call
23//! [`render`](screen::Screen::render); it emits only what changed. It reads
24//! no input and touches no terminal mode, so it stands alone for
25//! output-only programs, tests, and offscreen rendering.
26//! - **[`buffer::TextBuffer`]** (and any [`buffer::Surface`]) is the
27//! stateless route. Paint a full frame into an in-memory grid and
28//! serialize it to escape bytes with the [`text::Encode`] trait. There is
29//! no renderer and no terminal session, which makes it the tool for
30//! one-shot frames, snapshot tests, transcripts, and append-style output.
31//!
32//! # Quick start with `Program`
33//!
34//! ```no_run
35//! use uncurses::color::Color;
36//! use uncurses::program::Program;
37//! use uncurses::style::Style;
38//! use uncurses::text::TextSurface;
39//!
40//! # fn main() -> std::io::Result<()> {
41//! let mut program = Program::stdio()?;
42//! let result = (|| {
43//! program.init()?;
44//!
45//! let style = Style::default().bold().fg(Color::Green);
46//! let screen = program.screen_mut();
47//! screen.set_str((0, 0), "Hello, terminal!", style);
48//! screen.render()?;
49//! Ok(())
50//! })();
51//!
52//! let finish = program.finish();
53//! result.and(finish)
54//! # }
55//! ```
56//!
57//! The final call attempts terminal restoration after success or an error,
58//! including an initialization error.
59//!
60//! # Quick start with `TextBuffer`
61//!
62//! Paint a [`buffer::TextBuffer`] and serialize it yourself, with no
63//! terminal involved:
64//!
65//! ```rust
66//! use uncurses::buffer::TextBuffer;
67//! use uncurses::color::Color;
68//! use uncurses::style::Style;
69//! use uncurses::text::{Encode, TextSurface};
70//!
71//! let mut frame = TextBuffer::new(80, 24);
72//! let style = Style::default()
73//! .bold()
74//! .fg(Color::Green);
75//! frame.set_str((0, 0), "Hello, terminal!", style);
76//!
77//! // Serialize the painted grid to escape bytes you can write anywhere.
78//! let bytes = frame.display().to_string();
79//! assert!(bytes.contains("Hello, terminal!"));
80//! ```
81//!
82//! # The module map
83//!
84//! | Module | What lives there |
85//! | --- | --- |
86//! | [`program`] | The interactive [`Program`](program::Program): session lifecycle, terminal modes, input observation, and access to its screen. |
87//! | [`screen`] | The standalone [`Screen`](screen::Screen) diffing renderer, cell grid, and output writer. |
88//! | [`buffer`] | Cell-grid storage ([`Buffer`](buffer::Buffer), [`TextBuffer`](buffer::TextBuffer), [`Window`](buffer::Window)) and the [`Surface`](buffer::Surface) / [`SurfaceMut`](buffer::SurfaceMut) traits every drawable shares. |
89//! | [`text`] | Text shaping, width measurement, the [`TextSurface`](text::TextSurface) painting trait that adds `set_str` to any surface, and the [`Encode`](text::Encode) trait that serializes a surface to escapes. |
90//! | [`style`] | [`Style`](style::Style), colors, attributes, and SGR plus hyperlink (OSC 8) encoding. |
91//! | [`color`] | Color types and capability [`Profile`](color::Profile)s with automatic downsampling. |
92//! | [`event`] | The [`EventSource`](event::EventSource) decoder, typed [`Event`](event::Event) values, and (with the `async` feature) an `EventStream`. |
93//! | [`ansi`] | Raw escape-sequence encoders and parsers for the cursor, modes, colors, queries, and the long tail of terminal control. |
94//! | [`terminal`] | The [`Terminal`](terminal::Terminal) handle, raw-mode lifecycle, window-size queries, and environment lookups. |
95//! | [`cell`] | The [`Cell`](cell::Cell) value type. |
96//! | [`unicode`] | Grapheme-cluster segmentation and other Unicode text primitives. |
97//! | [`layout`] | [`Position`](layout::Position), [`Size`](layout::Size), and [`Rect`](layout::Rect) geometry. |
98//!
99//! # Output buffering and flushing
100//!
101//! Painting is infallible. Drawing cells with
102//! [`set_str`](text::TextSurface::set_str),
103//! [`set_cell`](screen::Screen::set_cell), and friends only updates an
104//! in-memory frame; nothing is written until you call
105//! [`render`](screen::Screen::render), which diffs that frame against the
106//! terminal and writes just the changed cells.
107//!
108//! Mode changes are applied immediately. Entering the alternate screen,
109//! hiding the cursor, enabling mouse reporting, setting the title, and similar
110//! switches write their escape sequence on the spot. A stateless
111//! [`TextBuffer`](buffer::TextBuffer) has no writer of its own:
112//! [`encode`](text::Encode::encode) hands you the bytes and you decide where
113//! they go.
114
115#![cfg_attr(docsrs, feature(doc_cfg))]
116#![cfg_attr(uncurses_bench, feature(test))]
117
118#[cfg(not(any(feature = "icu", feature = "unicode-rs")))]
119compile_error!(
120 "uncurses requires one of the `icu` or `unicode-rs` features to be enabled (the default)"
121);
122
123/// The `libc` crate this was built against, re-exported so the platform types
124/// in the public API are nameable without depending on `libc` directly and
125/// matching its major version by hand. [`terminal::State`] exposes
126/// `libc::termios` values.
127#[cfg(unix)]
128pub use libc;
129
130pub mod ansi;
131pub mod buffer;
132pub mod cell;
133pub mod color;
134pub mod event;
135pub mod layout;
136pub mod program;
137pub mod screen;
138pub mod style;
139pub mod terminal;
140pub mod text;
141pub mod unicode;
142
143pub(crate) mod renderer;
144
145#[cfg(all(test, unix, not(target_os = "l4re")))]
146mod testutil;
147
148#[cfg(debug_assertions)]
149mod trace;