Skip to main content

uncurses/renderer/
mod.rs

1//! Cell-diff renderer and terminal byte planner.
2//!
3//! The renderer turns a desired `RenderBuffer` into the shortest safe
4//! sequence of terminal bytes it can produce for the configured
5//! [`Optimizations`]. Applications use it through [`crate::screen::Screen`].
6//! The public [`cost`] module exposes sequence predictors.
7//!
8//! ## Diff pipeline
9//!
10//! The renderer keeps a tracked current buffer (`cur_buf`) representing
11//! what it believes is on the terminal. The buffer flow first syncs
12//! touched desired cells into the renderer-owned staging buffer
13//! (`back_buf`), filtering unchanged cells. Rendering then prepares the
14//! frame, optionally applies scroll optimizations, transforms touched
15//! rows, and finalizes the pen/cursor state.
16//!
17//! ```text
18//! desired cells ─▶ sync_front ─▶ back_buf (actual changes)
19//!                                      │
20//!                                      ▼
21//! cur_buf (tracked screen) ─────▶ row diff / scroll diff
22//!                                      │
23//!                                      ▼
24//!                           cursor + pen planners ─▶ escape bytes
25//!                                      │
26//!                                      ▼
27//!                         cur_buf updated to match terminal
28//! ```
29//!
30//! ## Cursor movement planning
31//!
32//! Cursor movement is planned by byte cost before any bytes are
33//! materialized. Absolute mode seeds CUP and, for local moves, lets
34//! relative decompositions compete. Relative mode considers optional
35//! prefixes (`none`, carriage return, and in absolute mode home), then
36//! vertical and horizontal legs. Horizontal planning can use hardware
37//! tabs, CHT/CBT, CUF/CUB, literal backspace, or re-emitting matching
38//! cells as an overwrite advance. Forward tabs use the terminal's true
39//! next tab stop, not a right-edge-clamped value, so a tab is never
40//! chosen unless it lands at or before the target column.
41//! Sequence predictors live in [`cost`]. The cursor planner owns
42//! cell-aware overwrite costs beside their emission walk.
43//!
44//! ```text
45//! from ─▶ prefix? ─▶ vertical leg ─▶ horizontal leg ─▶ to
46//!          │             │                 │
47//!          │             │                 ├─ CUF/CUB, BS, tabs, CHT/CBT
48//!          │             └─ LF/CUD/CUU/RI/VPA
49//!          └─ none / CR / HOME (when eligible)
50//! ```
51//!
52//! ## Pen and SGR state
53//!
54//! The renderer tracks the active style ("pen") alongside cursor
55//! position. Before emitting a cell it writes only the style difference
56//! needed from the current pen to the target cell style, including OSC 8
57//! hyperlink open/close transitions. At frame end the pen is reset to
58//! default only if the terminal state is not already default.
59//!
60//! ## Tab stops
61//!
62//! `TabStops` stores configurable horizontal tab stops and
63//! precomputes previous, clamped-next, and unclamped-next lookup tables.
64//! The unclamped next stop models the column a terminal tab would really
65//! reach beyond the surface edge; cursor planning depends on that to
66//! avoid over-counting forward tab moves near the right margin.
67
68pub(crate) mod buffer;
69pub(crate) mod caps;
70pub(crate) mod color_cache;
71mod commit;
72pub mod cost;
73pub(crate) mod cursor;
74#[cfg(test)]
75mod cursor_planner_tests;
76pub(crate) mod frame;
77pub(crate) mod hash;
78pub(crate) mod pen;
79pub(crate) mod scroll;
80pub(crate) mod state;
81pub(crate) mod sync;
82pub(crate) mod tabstops;
83pub(crate) mod transform;
84
85#[cfg(uncurses_bench)]
86mod bench;
87
88#[cfg(test)]
89mod transform_branch_tests;
90
91#[cfg(test)]
92#[path = "tests/golden.rs"]
93mod golden;
94
95#[cfg(test)]
96#[path = "tests/render.rs"]
97mod render_tests;
98
99#[cfg(test)]
100#[path = "tests/optimizations_golden.rs"]
101mod optimizations_golden;
102
103pub(crate) use buffer::RenderBuffer;
104pub use caps::Optimizations;
105pub(crate) use state::Renderer;