Skip to main content

euv_core/render/terminal/
struct.rs

1use super::*;
2
3/// Single character cell inside a `TerminalCellGrid` supporting wide CJK glyphs and truecolor styling.
4#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
5pub struct TerminalCell {
6    /// Displayed 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}
28
29/// Dense `cols x rows` character cell surface for deterministic terminal TUI and CLI rendering.
30#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
31pub struct TerminalCellGrid {
32    /// Terminal grid width in character columns.
33    #[get(type(copy))]
34    pub(crate) cols: u16,
35    /// Terminal grid height in character rows.
36    #[get(type(copy))]
37    pub(crate) rows: u16,
38    /// Row-major cell buffer of length `cols as usize * rows as usize`.
39    pub(crate) cells: Vec<TerminalCell>,
40}
41
42/// Per-frame terminal cell-diff and bandwidth telemetry produced by `TerminalRenderer::render_tree`.
43#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
44pub struct TerminalDiffStats {
45    /// Total cells in the active `cols x rows` terminal grid.
46    #[get(type(copy))]
47    pub(crate) total_cells: usize,
48    /// Number of cells that differed from the previous frame (equals `total_cells` on full sync).
49    #[get(type(copy))]
50    pub(crate) changed_cells: usize,
51    /// Number of UTF-8 bytes emitted in the rendered output payload.
52    #[get(type(copy))]
53    pub(crate) output_bytes: usize,
54    /// `true` when zero cells changed in interactive mode and zero bytes were flushed.
55    #[get(type(copy))]
56    pub(crate) is_idle: bool,
57    /// `true` when the frame performed a full screen synchronization (first frame or resize).
58    #[get(type(copy))]
59    pub(crate) full_sync: bool,
60}
61
62/// RAII terminal session guard restoring cursor visibility, raw mode flags, and alternate screen on drop.
63#[derive(CustomDebug, Data)]
64pub struct TerminalSessionGuard {
65    /// Whether alternate screen mode was entered and must be exited on drop.
66    #[get(type(copy))]
67    pub(crate) alt_screen: bool,
68    /// Whether cursor visibility was hidden and must be restored on drop.
69    #[get(type(copy))]
70    pub(crate) cursor_hidden: bool,
71    /// Whether terminal raw/bracketed input modes were enabled and must be reset on drop.
72    #[get(type(copy))]
73    pub(crate) raw_mode: bool,
74    /// Whether terminal restoration has already completed.
75    #[get(type(copy))]
76    pub(crate) restored: bool,
77    /// Whether enter/restore sequences should also be flushed directly to standard output on native targets.
78    #[get(type(copy))]
79    pub(crate) write_to_stdout: bool,
80    /// Optional capture buffer recording emitted enter and restore escape sequences for deterministic testing.
81    #[debug(skip)]
82    #[get(skip)]
83    #[get_mut(skip)]
84    #[set(skip)]
85    pub(crate) sink: Option<Rc<RefCell<String>>>,
86}
87
88/// Stateful terminal renderer projecting a retained `SceneTree` into interactive ANSI diffs or static CLI plain text.
89#[derive(Clone, Data, Debug, Default, PartialEq)]
90pub struct TerminalRenderer {
91    /// Snapshot of the previously rendered grid for incremental cell-diffing.
92    #[get(skip)]
93    pub(crate) previous: Option<TerminalCellGrid>,
94    /// Working character grid populated during the current frame.
95    pub(crate) current: TerminalCellGrid,
96    /// Last rendered terminal column count for resize detection.
97    #[get(type(copy))]
98    pub(crate) last_cols: u16,
99    /// Last rendered terminal row count for resize detection.
100    #[get(type(copy))]
101    pub(crate) last_rows: u16,
102    /// Total frames projected through this renderer.
103    #[get(type(copy))]
104    pub(crate) frames_rendered: u64,
105    /// Whether keyboard navigation has requested terminal session exit.
106    #[get(type(copy))]
107    pub(crate) exit_requested: bool,
108}
109
110/// Internal character-grid layout viewport passed recursively while projecting scene nodes.
111#[derive(Clone, Copy, Data, Debug, Default, Eq, New, PartialEq)]
112pub(crate) struct TerminalRegion {
113    /// Left column bound of the active character region.
114    #[get(type(copy))]
115    pub(crate) x: u16,
116    /// Available horizontal width in terminal columns.
117    #[get(type(copy))]
118    pub(crate) width: u16,
119    /// Exclusive bottom row limit of the active character region.
120    #[get(type(copy))]
121    pub(crate) max_y: u16,
122    /// Currently focused scene node handle, if any.
123    #[get(type(copy))]
124    pub(crate) focused: Option<NodeId>,
125}