Skip to main content

euv_core/platform/terminal/
struct.rs

1use super::*;
2
3#[cfg(any(
4    target_os = "macos",
5    target_os = "ios",
6    target_os = "watchos",
7    target_os = "freebsd"
8))]
9/// Raw Darwin/BSD POSIX `termios` structure matching `<sys/termios.h>`.
10#[repr(C)]
11#[derive(Clone, Copy, Debug)]
12pub(crate) struct UnixTermios {
13    /// Input mode flags (`c_iflag`).
14    pub(crate) c_iflag: u64,
15    /// Output mode flags (`c_oflag`).
16    pub(crate) c_oflag: u64,
17    /// Control mode flags (`c_cflag`).
18    pub(crate) c_cflag: u64,
19    /// Local mode flags (`c_lflag`).
20    pub(crate) c_lflag: u64,
21    /// Control characters array (`NCCS = 20`).
22    pub(crate) c_cc: [u8; 20],
23    /// Input baud rate (`c_ispeed`).
24    pub(crate) c_ispeed: u64,
25    /// Output baud rate (`c_ospeed`).
26    pub(crate) c_ospeed: u64,
27}
28
29#[cfg(any(target_os = "linux", target_os = "android"))]
30/// Raw Linux POSIX `termios` structure matching `<termios.h>`.
31#[repr(C)]
32#[derive(Clone, Copy, Debug)]
33pub(crate) struct UnixTermios {
34    /// Input mode flags (`c_iflag`).
35    pub(crate) c_iflag: u32,
36    /// Output mode flags (`c_oflag`).
37    pub(crate) c_oflag: u32,
38    /// Control mode flags (`c_cflag`).
39    pub(crate) c_cflag: u32,
40    /// Local mode flags (`c_lflag`).
41    pub(crate) c_lflag: u32,
42    /// Line discipline (`c_line`).
43    pub(crate) c_line: u8,
44    /// Control characters array (`NCCS = 32`).
45    pub(crate) c_cc: [u8; 32],
46    /// Input baud rate (`c_ispeed`).
47    pub(crate) c_ispeed: u32,
48    /// Output baud rate (`c_ospeed`).
49    pub(crate) c_ospeed: u32,
50}
51
52#[cfg(unix)]
53/// Raw POSIX `winsize` ioctl payload returned by `TIOCGWINSZ`.
54#[repr(C)]
55#[derive(Clone, Copy, Debug, Default)]
56pub(crate) struct UnixWinsize {
57    /// Terminal height in character rows.
58    pub(crate) ws_row: u16,
59    /// Terminal width in character columns.
60    pub(crate) ws_col: u16,
61    /// Optional horizontal size in pixels.
62    pub(crate) ws_xpixel: u16,
63    /// Optional vertical size in pixels.
64    pub(crate) ws_ypixel: u16,
65}
66
67#[cfg(unix)]
68/// Raw POSIX `pollfd` structure for non-blocking PTY readiness polling.
69#[repr(C)]
70#[derive(Clone, Copy, Debug, Default)]
71pub(crate) struct UnixPollFd {
72    /// File descriptor to monitor.
73    pub(crate) fd: i32,
74    /// Requested event bitmask (`POLLIN`).
75    pub(crate) events: i16,
76    /// Returned event bitmask.
77    pub(crate) revents: i16,
78}
79
80#[cfg(target_os = "windows")]
81/// Raw Win32 console `COORD` structure.
82#[repr(C)]
83#[derive(Clone, Copy, Debug, Default)]
84pub(crate) struct Win32Coord {
85    /// Horizontal character coordinate.
86    pub(crate) x: i16,
87    /// Vertical character coordinate.
88    pub(crate) y: i16,
89}
90
91#[cfg(target_os = "windows")]
92/// Raw Win32 console `SMALL_RECT` structure.
93#[repr(C)]
94#[derive(Clone, Copy, Debug, Default)]
95pub(crate) struct Win32SmallRect {
96    /// Left column bound.
97    pub(crate) left: i16,
98    /// Top row bound.
99    pub(crate) top: i16,
100    /// Right column bound.
101    pub(crate) right: i16,
102    /// Bottom row bound.
103    pub(crate) bottom: i16,
104}
105
106#[cfg(target_os = "windows")]
107/// Raw Win32 `CONSOLE_SCREEN_BUFFER_INFO` structure.
108#[repr(C)]
109#[derive(Clone, Copy, Debug, Default)]
110pub(crate) struct Win32ConsoleScreenBufferInfo {
111    /// Screen buffer size in cells.
112    pub(crate) dw_size: Win32Coord,
113    /// Current cursor position.
114    pub(crate) dw_cursor_position: Win32Coord,
115    /// Active character attributes.
116    pub(crate) w_attributes: u16,
117    /// Visible console window rectangle.
118    pub(crate) sr_window: Win32SmallRect,
119    /// Maximum console window size.
120    pub(crate) dw_maximum_window_size: Win32Coord,
121}
122
123/// Terminal grid cell geometry converting character `(cols, rows)` to EUV logical dimensions.
124#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
125pub struct TerminalCellMetrics {
126    /// Visible terminal width in character columns.
127    #[get(type(copy))]
128    pub(crate) cols: u16,
129    /// Visible terminal height in character rows.
130    #[get(type(copy))]
131    pub(crate) rows: u16,
132    /// Logical pixel width assigned to one character column.
133    #[get(type(copy))]
134    pub(crate) cell_width_px: f32,
135    /// Logical pixel height assigned to one character row.
136    #[get(type(copy))]
137    pub(crate) cell_height_px: f32,
138}
139
140/// Runtime TTY, pipe, color depth, and dimension probe snapshot for the active host process.
141#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
142pub struct TerminalTtyProbe {
143    /// Whether standard input is attached to an interactive TTY.
144    #[get(type(copy))]
145    pub(crate) stdin_is_tty: bool,
146    /// Whether standard output is attached to an interactive TTY.
147    #[get(type(copy))]
148    pub(crate) stdout_is_tty: bool,
149    /// Whether standard error is attached to an interactive TTY.
150    #[get(type(copy))]
151    pub(crate) stderr_is_tty: bool,
152    /// Detected terminal color depth (`None` when piped or `NO_COLOR` is set).
153    #[get(type(copy))]
154    pub(crate) color: TerminalColorCapability,
155    /// Whether the `NO_COLOR` environment override is active.
156    #[get(type(copy))]
157    pub(crate) no_color: bool,
158    /// Detected terminal width in columns.
159    #[get(type(copy))]
160    pub(crate) cols: u16,
161    /// Detected terminal height in rows.
162    #[get(type(copy))]
163    pub(crate) rows: u16,
164}
165
166/// RAII terminal raw-mode, alternate-screen, mouse, and cursor restoration guard surviving normal and panic exits.
167#[derive(CustomDebug, Data)]
168pub struct TerminalRawGuard {
169    /// Whether Unix termios or Windows console raw mode was activated on a real OS handle.
170    #[get(type(copy))]
171    pub(crate) os_raw_active: bool,
172    /// Whether alternate screen buffer was entered.
173    #[get(type(copy))]
174    pub(crate) alt_screen: bool,
175    /// Whether mouse SGR 1006 tracking was enabled.
176    #[get(type(copy))]
177    pub(crate) mouse_enabled: bool,
178    /// Whether bracketed paste mode was enabled.
179    #[get(type(copy))]
180    pub(crate) paste_enabled: bool,
181    /// Whether cursor visibility was hidden.
182    #[get(type(copy))]
183    pub(crate) cursor_hidden: bool,
184    /// Whether restoration sequences and OS modes have already been restored.
185    #[get(type(copy))]
186    pub(crate) restored: bool,
187    /// Whether enter/restore sequences should also write to OS `stdout`.
188    #[get(type(copy))]
189    pub(crate) write_to_stdout: bool,
190    #[cfg(unix)]
191    /// Saved original Unix `termios` state restored on `Drop`.
192    #[get(skip)]
193    #[get_mut(skip)]
194    #[set(skip)]
195    pub(crate) saved_termios: Option<UnixTermios>,
196    #[cfg(target_os = "windows")]
197    /// Saved original Windows console `(stdin_mode, stdout_mode)` restored on `Drop`.
198    #[get(skip)]
199    #[get_mut(skip)]
200    #[set(skip)]
201    pub(crate) saved_win_modes: Option<(u32, u32)>,
202    /// Optional deterministic capture sink recording enter/exit escape sequences for tests.
203    #[debug(skip)]
204    #[get(skip)]
205    #[get_mut(skip)]
206    #[set(skip)]
207    pub(crate) sink: Option<Rc<RefCell<String>>>,
208}
209
210/// Stateful VT100 / xterm / CSI / SS3 / SGR-1006 mouse and bracketed-paste byte stream parser.
211#[derive(Clone, Data, Debug, PartialEq)]
212pub struct TerminalInputParser {
213    /// Trailing incomplete escape or UTF-8 bytes carried across incremental reads.
214    pub(crate) pending: Vec<u8>,
215    /// Whether the parser is currently accumulating a bracketed paste payload.
216    #[get(type(copy))]
217    pub(crate) in_paste: bool,
218    /// Accumulated UTF-8 text inside an active bracketed paste block.
219    pub(crate) paste_buffer: String,
220    /// Logical cell pixel width used to map mouse columns to EUV logical coordinates.
221    #[get(type(copy))]
222    pub(crate) cell_width_px: f32,
223    /// Logical cell pixel height used to map mouse rows to EUV logical coordinates.
224    #[get(type(copy))]
225    pub(crate) cell_height_px: f32,
226}
227
228/// Live window record tracked by `TerminalPlatform`.
229#[derive(Clone, Data, Debug)]
230pub struct TerminalWindowRecord {
231    /// Window configuration snapshot.
232    pub(crate) config: WindowConfig,
233    /// Current logical and physical window state.
234    #[get(type(copy))]
235    pub(crate) state: WindowState,
236    /// Active character cell metrics.
237    #[get(type(copy))]
238    pub(crate) metrics: TerminalCellMetrics,
239    /// Count of presented terminal frames or CLI flushes.
240    #[get(type(copy))]
241    pub(crate) present_count: u64,
242    /// Most recent rendered ANSI or plain-text frame string.
243    pub(crate) last_output: String,
244    /// Last presented RGBA8 framebuffer fallback snapshot when graphical draw is used.
245    pub(crate) last_frame: Framebuffer,
246}
247
248/// Cross-platform Terminal TUI and Static/Pipe CLI host implementing `PlatformBackend` and `PlatformHostAdapter`.
249#[derive(CustomDebug, Data)]
250pub struct TerminalPlatform {
251    /// Requested operating mode (`Auto`, `InteractiveTui`, or `StaticCli`).
252    #[get(type(copy))]
253    pub(crate) requested_mode: TerminalHostMode,
254    /// Resolved concrete operating mode (`InteractiveTui` or `StaticCli`).
255    #[get(type(copy))]
256    pub(crate) resolved_mode: TerminalHostMode,
257    /// Initial TTY / pipe / color capability probe snapshot.
258    #[get(type(copy))]
259    pub(crate) probe: TerminalTtyProbe,
260    /// Active cell metrics for column/row <-> logical pixel conversion.
261    #[get(type(copy))]
262    pub(crate) metrics: TerminalCellMetrics,
263    /// Monotonic window identifier allocator.
264    #[get(pub(crate), type(copy))]
265    #[get_mut(pub(crate))]
266    #[set(pub(crate))]
267    pub(crate) next_id: u64,
268    /// Primary active terminal window identifier, if created.
269    #[get(skip)]
270    pub(crate) primary_window: Option<WindowId>,
271    /// Active terminal window records.
272    pub(crate) windows: HashMap<WindowId, TerminalWindowRecord>,
273    /// Queued translated window and system events awaiting `poll_events()`.
274    pub(crate) pending_events: Vec<WindowEvent>,
275    /// VT/xterm keyboard and SGR mouse byte parser.
276    pub(crate) parser: TerminalInputParser,
277    /// Active RAII terminal raw-mode restoration guard when interactive PTY mode is entered.
278    #[get(skip)]
279    pub(crate) raw_guard: Option<TerminalRawGuard>,
280    /// Captured standard output buffer for deterministic CLI testing and stream inspection.
281    pub(crate) stdout_log: Vec<u8>,
282    /// Captured standard error diagnostic buffer for deterministic CLI testing.
283    pub(crate) stderr_log: Vec<u8>,
284    /// Whether `write_stdout` / `write_stderr` should also emit to process OS streams.
285    #[get(type(copy))]
286    pub(crate) emit_os_streams: bool,
287    /// Process exit code propagated by CLI or TUI termination paths (`0` = success).
288    #[get(type(copy))]
289    pub(crate) exit_code: i32,
290    /// True once downstream pipe closed (`BrokenPipe`), suppressing further writes without panicking.
291    #[get(type(copy))]
292    pub(crate) broken_pipe: bool,
293    /// In-memory clipboard buffer (or OSC 52 payload).
294    #[get(skip)]
295    pub(crate) clipboard: Option<String>,
296    /// In-memory sandbox storage backing `PlatformStorage`.
297    pub(crate) storage: HashMap<String, String>,
298    /// Logical route navigation stack backing `PlatformNavigation`.
299    pub(crate) route_stack: Vec<String>,
300    /// Active OS theme hint (`Dark` default for terminals).
301    #[get(type(copy))]
302    pub(crate) theme: ThemeMode,
303    /// Whether frame pumping is paused for background/suspend freeze.
304    #[get(type(copy))]
305    pub(crate) pump_paused: bool,
306    #[cfg(not(target_arch = "wasm32"))]
307    /// Timestamp of the latest raw PTY input arrival used for `input_to_output_ms` measurement.
308    #[get(skip)]
309    #[get_mut(skip)]
310    #[set(skip)]
311    pub(crate) last_input_at: Option<Instant>,
312    /// Maximum byte size per coalesced CLI output batch before applying flush/backpressure.
313    #[get(type(copy))]
314    pub(crate) cli_batch_limit_bytes: usize,
315    /// Persistent double-buffered differential ANSI renderer backing direct `present_scene_tree` presentation.
316    pub(crate) renderer: TerminalRenderer,
317    /// When `true`, snapshots each RGBA framebuffer in `present_framebuffer` (`false` avoids cloning unused pixel buffers).
318    #[get(type(copy))]
319    pub(crate) capture_last_frame: bool,
320    /// Cumulative number of `wait_for_events` calls issued on this terminal platform.
321    #[get(type(copy))]
322    pub(crate) wait_calls: u64,
323    /// Cumulative number of single buffered stdout writes flushed by `present_scene_tree`.
324    #[get(type(copy))]
325    pub(crate) stdout_flush_calls: u64,
326    /// Most recent non-GPU, non-Hz TUI/CLI telemetry report.
327    #[get(type(copy))]
328    pub(crate) last_telemetry: TerminalNonGpuTelemetry,
329    /// Uncapped host display refresh rate in Hertz (`HIGH_REFRESH_DEFAULT_HZ` unless overridden by host/environment).
330    #[get(type(copy))]
331    pub(crate) refresh_rate_hz: f32,
332}