euv_core/render/terminal/struct.rs
1use super::*;
2
3/// Single character cell inside a `TerminalCellGrid` supporting wide CJK glyphs, Extended Grapheme Clusters (combining marks & ZWJ emoji), and truecolor styling with zero heap allocation.
4#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
5pub struct TerminalCell {
6 /// Leading Unicode scalar value (`' '` when empty, `'\0'` on trailing wide-char continuation cells).
7 #[get(type(copy))]
8 pub(crate) ch: char,
9 /// Display column width: `1` for narrow, `2` for wide lead cell, `0` for wide continuation cell.
10 #[get(type(copy))]
11 pub(crate) width: u8,
12 /// Optional 24-bit foreground text color.
13 #[get(skip)]
14 pub(crate) fg: Option<Color>,
15 /// Optional 24-bit cell background color.
16 #[get(skip)]
17 pub(crate) bg: Option<Color>,
18 /// Whether bold SGR emphasis is active.
19 #[get(type(copy))]
20 pub(crate) bold: bool,
21 /// Whether reverse-video SGR highlight (used for focus indicators) is active.
22 #[get(type(copy))]
23 pub(crate) reverse: bool,
24 /// Whether underline SGR decoration is active.
25 #[get(type(copy))]
26 pub(crate) underline: bool,
27 /// Length in UTF-8 bytes of trailing scalars in the same Extended Grapheme Cluster (`0..=28`).
28 #[get(type(copy))]
29 pub(crate) tail_len: u8,
30 /// Inline UTF-8 byte buffer holding combining marks, variation selectors, or ZWJ emoji sequences.
31 #[get(skip)]
32 #[get_mut(skip)]
33 #[set(skip)]
34 pub(crate) tail_utf8: [u8; TERMINAL_CELL_TAIL_CAPACITY],
35}
36
37/// Dense `cols x rows` character cell surface with per-row dirty tracking and row hashes for O(1) unchanged-row diff skipping.
38#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
39pub struct TerminalCellGrid {
40 /// Terminal grid width in character columns.
41 #[get(type(copy))]
42 pub(crate) cols: u16,
43 /// Terminal grid height in character rows.
44 #[get(type(copy))]
45 pub(crate) rows: u16,
46 /// Row-major cell buffer of length `cols as usize * rows as usize`.
47 pub(crate) cells: Vec<TerminalCell>,
48 /// Cached FNV-1a hash per row enabling O(1) clean-row skipping during incremental diff encoding.
49 pub(crate) row_hashes: Vec<u64>,
50 /// Per-row dirty mask recording which rows were mutated since the last `reset()`.
51 pub(crate) dirty_rows: Vec<bool>,
52 /// Precomputed FNV-1a hash of a completely blank row of length `cols`.
53 #[get(type(copy))]
54 pub(crate) blank_row_hash: u64,
55}
56
57/// Per-frame terminal cell-diff and bandwidth telemetry produced by `TerminalRenderer::render_tree`.
58#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
59pub struct TerminalDiffStats {
60 /// Total cells in the active `cols x rows` terminal grid.
61 #[get(type(copy))]
62 pub(crate) total_cells: usize,
63 /// Number of cells that differed from the previous frame (equals `total_cells` on full sync).
64 #[get(type(copy))]
65 pub(crate) changed_cells: usize,
66 /// Number of UTF-8 bytes emitted in the rendered output payload.
67 #[get(type(copy))]
68 pub(crate) output_bytes: usize,
69 /// `true` when zero cells changed in interactive mode and zero bytes were flushed.
70 #[get(type(copy))]
71 pub(crate) is_idle: bool,
72 /// `true` when the frame performed a full screen synchronization (first frame, resize, or post-error recovery).
73 #[get(type(copy))]
74 pub(crate) full_sync: bool,
75}
76
77/// Compact O(1) frame state fingerprint governing zero-work idle short-circuiting in interactive TUI mode.
78#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
79pub struct TerminalFrameFingerprint {
80 /// Scene tree revision counter.
81 #[get(type(copy))]
82 pub(crate) scene_revision: u64,
83 /// Currently focused scene node identifier, if any.
84 #[get(skip)]
85 pub(crate) focused: Option<NodeId>,
86 /// Current caret blinking visibility phase.
87 #[get(type(copy))]
88 pub(crate) caret_visible: bool,
89 /// Caret byte offset within the focused text input, if any.
90 #[get(type(copy))]
91 pub(crate) focused_caret_offset: usize,
92 /// Hash across active scroll container offsets.
93 #[get(type(copy))]
94 pub(crate) scroll_hash: u64,
95 /// Terminal viewport width in columns.
96 #[get(type(copy))]
97 pub(crate) cols: u16,
98 /// Terminal viewport height in rows.
99 #[get(type(copy))]
100 pub(crate) rows: u16,
101 /// Render mode (`InteractiveTui` vs `StaticPlainText`).
102 #[get(type(copy))]
103 pub(crate) mode: TerminalRenderMode,
104}
105
106/// Outcome of a batched `TerminalRenderer::render_tree_to_writer` call including swap commit status.
107#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
108pub struct TerminalWriteResult {
109 /// Write status (`Written`, `BrokenPipe`, or `IoError`).
110 #[get(type(copy))]
111 pub(crate) status: CliWriteStatus,
112 /// Per-frame diff and bandwidth telemetry.
113 #[get(type(copy))]
114 pub(crate) stats: TerminalDiffStats,
115 /// `true` when double-buffer ownership was swapped after a confirmed successful write.
116 #[get(type(copy))]
117 pub(crate) swapped: bool,
118 /// Optional human-readable error message when `status != CliWriteStatus::Written`.
119 #[get(skip)]
120 pub(crate) error_message: Option<String>,
121}
122
123/// RAII terminal session guard restoring cursor visibility, raw mode flags, and alternate screen on drop.
124#[derive(CustomDebug, Data)]
125pub struct TerminalSessionGuard {
126 /// Whether alternate screen mode was entered and must be exited on drop.
127 #[get(type(copy))]
128 pub(crate) alt_screen: bool,
129 /// Whether cursor visibility was hidden and must be restored on drop.
130 #[get(type(copy))]
131 pub(crate) cursor_hidden: bool,
132 /// Whether terminal raw/bracketed input modes were enabled and must be reset on drop.
133 #[get(type(copy))]
134 pub(crate) raw_mode: bool,
135 /// Whether terminal restoration has already completed.
136 #[get(type(copy))]
137 pub(crate) restored: bool,
138 /// Whether enter/restore sequences should also be flushed directly to standard output on native targets.
139 #[get(type(copy))]
140 pub(crate) write_to_stdout: bool,
141 /// Optional capture buffer recording emitted enter and restore escape sequences for deterministic testing.
142 #[debug(skip)]
143 #[get(skip)]
144 #[get_mut(skip)]
145 #[set(skip)]
146 pub(crate) sink: Option<Rc<RefCell<String>>>,
147}
148
149/// Stateful double-buffered terminal renderer projecting a retained `SceneTree` into coalesced ANSI TUI diffs or zero-ANSI static CLI plain text.
150#[derive(Clone, Data, Debug, Default, PartialEq)]
151pub struct TerminalRenderer {
152 /// Persistent previously presented cell grid swapped in-place with `current` via `swap` (zero per-frame full-grid clones; unallocated in `StaticPlainText` mode).
153 pub(crate) previous: TerminalCellGrid,
154 /// `true` iff `previous` holds a confirmed committed interactive frame matching `last_cols x last_rows`.
155 #[get(type(copy))]
156 pub(crate) previous_valid: bool,
157 /// Working character grid populated during the current frame.
158 pub(crate) current: TerminalCellGrid,
159 /// Cached frame fingerprint enabling zero-work idle short-circuiting.
160 #[get(skip)]
161 pub(crate) last_fingerprint: Option<TerminalFrameFingerprint>,
162 /// Scroll container handles observed during the last projection for O(#scrolls) scroll-offset hashing.
163 pub(crate) tracked_scroll_nodes: Vec<NodeId>,
164 /// Reusable ANSI output scratch buffer reducing steady-state heap churn.
165 pub(crate) ansi_scratch: String,
166 /// Last rendered terminal column count for resize detection.
167 #[get(type(copy))]
168 pub(crate) last_cols: u16,
169 /// Last rendered terminal row count for resize detection.
170 #[get(type(copy))]
171 pub(crate) last_rows: u16,
172 /// Total non-skipped frames projected through this renderer.
173 #[get(type(copy))]
174 pub(crate) frames_rendered: u64,
175 /// Total interactive frames short-circuited in O(1) with zero projection and zero output bytes.
176 #[get(type(copy))]
177 pub(crate) skipped_idle_frames: u64,
178 /// Number of rows actually projected during the most recent frame (`0` on short-circuited idle frames).
179 #[get(type(copy))]
180 pub(crate) last_projected_rows: usize,
181 /// Whether DEC Mode 2026 synchronized update sequences wrap non-empty interactive ANSI payloads.
182 #[get(type(copy))]
183 pub(crate) sync_output_mode: bool,
184 /// Whether keyboard navigation has requested terminal session exit.
185 #[get(type(copy))]
186 pub(crate) exit_requested: bool,
187}
188
189/// Internal character-grid layout viewport passed recursively while projecting scene nodes.
190#[derive(Clone, Copy, Data, Debug, Default, Eq, New, PartialEq)]
191pub(crate) struct TerminalRegion {
192 /// Left column bound of the active character region.
193 #[get(type(copy))]
194 pub(crate) x: u16,
195 /// Available horizontal width in terminal columns.
196 #[get(type(copy))]
197 pub(crate) width: u16,
198 /// Exclusive bottom row limit of the active character region.
199 #[get(type(copy))]
200 pub(crate) max_y: u16,
201 /// Currently focused scene node handle, if any.
202 #[get(type(copy))]
203 pub(crate) focused: Option<NodeId>,
204}