Skip to main content

euv_core/platform/terminal/
struct.rs

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