euv-core 0.48.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))]
/// Raw Darwin/BSD POSIX `termios` structure matching `<sys/termios.h>`.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct UnixTermios {
    /// Input mode flags (`c_iflag`).
    pub(crate) c_iflag: u64,
    /// Output mode flags (`c_oflag`).
    pub(crate) c_oflag: u64,
    /// Control mode flags (`c_cflag`).
    pub(crate) c_cflag: u64,
    /// Local mode flags (`c_lflag`).
    pub(crate) c_lflag: u64,
    /// Control characters array (`NCCS = 20`).
    pub(crate) c_cc: [u8; 20],
    /// Input baud rate (`c_ispeed`).
    pub(crate) c_ispeed: u64,
    /// Output baud rate (`c_ospeed`).
    pub(crate) c_ospeed: u64,
}

#[cfg(any(target_os = "linux", target_os = "android"))]
/// Raw Linux POSIX `termios` structure matching `<termios.h>`.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct UnixTermios {
    /// Input mode flags (`c_iflag`).
    pub(crate) c_iflag: u32,
    /// Output mode flags (`c_oflag`).
    pub(crate) c_oflag: u32,
    /// Control mode flags (`c_cflag`).
    pub(crate) c_cflag: u32,
    /// Local mode flags (`c_lflag`).
    pub(crate) c_lflag: u32,
    /// Line discipline (`c_line`).
    pub(crate) c_line: u8,
    /// Control characters array (`NCCS = 32`).
    pub(crate) c_cc: [u8; 32],
    /// Input baud rate (`c_ispeed`).
    pub(crate) c_ispeed: u32,
    /// Output baud rate (`c_ospeed`).
    pub(crate) c_ospeed: u32,
}

#[cfg(unix)]
/// Raw POSIX `winsize` ioctl payload returned by `TIOCGWINSZ`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct UnixWinsize {
    /// Terminal height in character rows.
    pub(crate) ws_row: u16,
    /// Terminal width in character columns.
    pub(crate) ws_col: u16,
    /// Optional horizontal size in pixels.
    pub(crate) ws_xpixel: u16,
    /// Optional vertical size in pixels.
    pub(crate) ws_ypixel: u16,
}

#[cfg(unix)]
/// Raw POSIX `pollfd` structure for non-blocking PTY readiness polling.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct UnixPollFd {
    /// File descriptor to monitor.
    pub(crate) fd: i32,
    /// Requested event bitmask (`POLLIN`).
    pub(crate) events: i16,
    /// Returned event bitmask.
    pub(crate) revents: i16,
}

#[cfg(target_os = "windows")]
/// Raw Win32 console `COORD` structure.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32Coord {
    /// Horizontal character coordinate.
    pub(crate) x: i16,
    /// Vertical character coordinate.
    pub(crate) y: i16,
}

#[cfg(target_os = "windows")]
/// Raw Win32 console `SMALL_RECT` structure.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32SmallRect {
    /// Left column bound.
    pub(crate) left: i16,
    /// Top row bound.
    pub(crate) top: i16,
    /// Right column bound.
    pub(crate) right: i16,
    /// Bottom row bound.
    pub(crate) bottom: i16,
}

#[cfg(target_os = "windows")]
/// Raw Win32 `CONSOLE_SCREEN_BUFFER_INFO` structure.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Win32ConsoleScreenBufferInfo {
    /// Screen buffer size in cells.
    pub(crate) dw_size: Win32Coord,
    /// Current cursor position.
    pub(crate) dw_cursor_position: Win32Coord,
    /// Active character attributes.
    pub(crate) w_attributes: u16,
    /// Visible console window rectangle.
    pub(crate) sr_window: Win32SmallRect,
    /// Maximum console window size.
    pub(crate) dw_maximum_window_size: Win32Coord,
}

/// Terminal grid cell geometry converting character `(cols, rows)` to EUV logical dimensions.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct TerminalCellMetrics {
    /// Visible terminal width in character columns.
    #[get(type(copy))]
    pub(crate) cols: u16,
    /// Visible terminal height in character rows.
    #[get(type(copy))]
    pub(crate) rows: u16,
    /// Logical pixel width assigned to one character column.
    #[get(type(copy))]
    pub(crate) cell_width_px: f32,
    /// Logical pixel height assigned to one character row.
    #[get(type(copy))]
    pub(crate) cell_height_px: f32,
}

/// Runtime TTY, pipe, color depth, and dimension probe snapshot for the active host process.
#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
pub struct TerminalTtyProbe {
    /// Whether standard input is attached to an interactive TTY.
    #[get(type(copy))]
    pub(crate) stdin_is_tty: bool,
    /// Whether standard output is attached to an interactive TTY.
    #[get(type(copy))]
    pub(crate) stdout_is_tty: bool,
    /// Whether standard error is attached to an interactive TTY.
    #[get(type(copy))]
    pub(crate) stderr_is_tty: bool,
    /// Detected terminal color depth (`None` when piped or `NO_COLOR` is set).
    #[get(type(copy))]
    pub(crate) color: TerminalColorCapability,
    /// Whether the `NO_COLOR` environment override is active.
    #[get(type(copy))]
    pub(crate) no_color: bool,
    /// Detected terminal width in columns.
    #[get(type(copy))]
    pub(crate) cols: u16,
    /// Detected terminal height in rows.
    #[get(type(copy))]
    pub(crate) rows: u16,
}

/// RAII terminal raw-mode, alternate-screen, mouse, and cursor restoration guard surviving normal and panic exits.
#[derive(CustomDebug, Data)]
pub struct TerminalRawGuard {
    /// Whether Unix termios or Windows console raw mode was activated on a real OS handle.
    #[get(type(copy))]
    pub(crate) os_raw_active: bool,
    /// Whether alternate screen buffer was entered.
    #[get(type(copy))]
    pub(crate) alt_screen: bool,
    /// Whether mouse SGR 1006 tracking was enabled.
    #[get(type(copy))]
    pub(crate) mouse_enabled: bool,
    /// Whether bracketed paste mode was enabled.
    #[get(type(copy))]
    pub(crate) paste_enabled: bool,
    /// Whether cursor visibility was hidden.
    #[get(type(copy))]
    pub(crate) cursor_hidden: bool,
    /// Whether restoration sequences and OS modes have already been restored.
    #[get(type(copy))]
    pub(crate) restored: bool,
    /// Whether enter/restore sequences should also write to OS `stdout`.
    #[get(type(copy))]
    pub(crate) write_to_stdout: bool,
    #[cfg(unix)]
    /// Saved original Unix `termios` state restored on `Drop`.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) saved_termios: Option<UnixTermios>,
    #[cfg(target_os = "windows")]
    /// Saved original Windows console `(stdin_mode, stdout_mode)` restored on `Drop`.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) saved_win_modes: Option<(u32, u32)>,
    /// Optional deterministic capture sink recording enter/exit escape sequences for tests.
    #[debug(skip)]
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) sink: Option<Rc<RefCell<String>>>,
}

/// Stateful VT100 / xterm / CSI / SS3 / SGR-1006 mouse and bracketed-paste byte stream parser.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct TerminalInputParser {
    /// Trailing incomplete escape or UTF-8 bytes carried across incremental reads.
    pub(crate) pending: Vec<u8>,
    /// Whether the parser is currently accumulating a bracketed paste payload.
    #[get(type(copy))]
    pub(crate) in_paste: bool,
    /// Accumulated UTF-8 text inside an active bracketed paste block.
    pub(crate) paste_buffer: String,
    /// Logical cell pixel width used to map mouse columns to EUV logical coordinates.
    #[get(type(copy))]
    pub(crate) cell_width_px: f32,
    /// Logical cell pixel height used to map mouse rows to EUV logical coordinates.
    #[get(type(copy))]
    pub(crate) cell_height_px: f32,
}

/// Live window record tracked by `TerminalPlatform`.
#[derive(Clone, Data, Debug)]
pub struct TerminalWindowRecord {
    /// Window configuration snapshot.
    pub(crate) config: WindowConfig,
    /// Current logical and physical window state.
    #[get(type(copy))]
    pub(crate) state: WindowState,
    /// Active character cell metrics.
    #[get(type(copy))]
    pub(crate) metrics: TerminalCellMetrics,
    /// Count of presented terminal frames or CLI flushes.
    #[get(type(copy))]
    pub(crate) present_count: u64,
    /// Most recent rendered ANSI or plain-text frame string.
    pub(crate) last_output: String,
    /// Last presented RGBA8 framebuffer fallback snapshot when graphical draw is used.
    pub(crate) last_frame: Framebuffer,
}

/// Cross-platform Terminal TUI and Static/Pipe CLI host implementing `PlatformBackend` and `PlatformHostAdapter`.
#[derive(CustomDebug, Data)]
pub struct TerminalPlatform {
    /// Requested operating mode (`Auto`, `InteractiveTui`, or `StaticCli`).
    #[get(type(copy))]
    pub(crate) requested_mode: TerminalHostMode,
    /// Resolved concrete operating mode (`InteractiveTui` or `StaticCli`).
    #[get(type(copy))]
    pub(crate) resolved_mode: TerminalHostMode,
    /// Initial TTY / pipe / color capability probe snapshot.
    #[get(type(copy))]
    pub(crate) probe: TerminalTtyProbe,
    /// Active cell metrics for column/row <-> logical pixel conversion.
    #[get(type(copy))]
    pub(crate) metrics: TerminalCellMetrics,
    /// Monotonic window identifier allocator.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) next_id: u64,
    /// Primary active terminal window identifier, if created.
    #[get(skip)]
    pub(crate) primary_window: Option<WindowId>,
    /// Active terminal window records.
    pub(crate) windows: HashMap<WindowId, TerminalWindowRecord>,
    /// Queued translated window and system events awaiting `poll_events()`.
    pub(crate) pending_events: Vec<WindowEvent>,
    /// VT/xterm keyboard and SGR mouse byte parser.
    pub(crate) parser: TerminalInputParser,
    /// Active RAII terminal raw-mode restoration guard when interactive PTY mode is entered.
    #[get(skip)]
    pub(crate) raw_guard: Option<TerminalRawGuard>,
    /// Captured standard output buffer for deterministic CLI testing and stream inspection.
    pub(crate) stdout_log: Vec<u8>,
    /// Captured standard error diagnostic buffer for deterministic CLI testing.
    pub(crate) stderr_log: Vec<u8>,
    /// Whether `write_stdout` / `write_stderr` should also emit to process OS streams.
    #[get(type(copy))]
    pub(crate) emit_os_streams: bool,
    /// Process exit code propagated by CLI or TUI termination paths (`0` = success).
    #[get(type(copy))]
    pub(crate) exit_code: i32,
    /// True once downstream pipe closed (`BrokenPipe`), suppressing further writes without panicking.
    #[get(type(copy))]
    pub(crate) broken_pipe: bool,
    /// In-memory clipboard buffer (or OSC 52 payload).
    #[get(skip)]
    pub(crate) clipboard: Option<String>,
    /// In-memory sandbox storage backing `PlatformStorage`.
    pub(crate) storage: HashMap<String, String>,
    /// Logical route navigation stack backing `PlatformNavigation`.
    pub(crate) route_stack: Vec<String>,
    /// Active OS theme hint (`Dark` default for terminals).
    #[get(type(copy))]
    pub(crate) theme: ThemeMode,
    /// Whether frame pumping is paused for background/suspend freeze.
    #[get(type(copy))]
    pub(crate) pump_paused: bool,
    #[cfg(not(target_arch = "wasm32"))]
    /// Timestamp of the latest raw PTY input arrival used for `input_to_output_ms` measurement.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_input_at: Option<Instant>,
    /// Maximum byte size per coalesced CLI output batch before applying flush/backpressure.
    #[get(type(copy))]
    pub(crate) cli_batch_limit_bytes: usize,
    /// Most recent non-GPU, non-Hz TUI/CLI telemetry report.
    #[get(type(copy))]
    pub(crate) last_telemetry: TerminalNonGpuTelemetry,
}