Skip to main content

ConsoleControl

Trait ConsoleControl 

Source
pub trait ConsoleControl: Sealed {
    type Saved: Send + Sync + Copy + 'static;

    const HANDLER_CONTRACT: HandlerContract;

    // Required methods
    fn capture(&self) -> Result<Self::Saved>;
    fn restore(&self, saved: &Self::Saved) -> Result<()>;
    fn write_raw(&self, bytes: &[u8]);
    fn trap(&self, on_interrupt: fn()) -> Result<()>;
    fn interrupt_exit_code(&self) -> i32;
}
Expand description

Save/restore terminal state and trap the interrupt-class events.

Required Associated Constants§

Required Associated Types§

Source

type Saved: Send + Sync + Copy + 'static

The saved modes, held by the caller — which is what lets the caller keep them in a OnceLock rather than a leaked AtomicPtr.

Required Methods§

Source

fn capture(&self) -> Result<Self::Saved>

Capture the modes as they are now. Call before anything enters raw mode: a later call would save raw modes as the thing to restore to.

Source

fn restore(&self, saved: &Self::Saved) -> Result<()>

Put the modes back. Must be callable from the interrupt handler, so on Unix this is tcsetattr and nothing else.

Source

fn write_raw(&self, bytes: &[u8])

Write bytes straight to the terminal, bypassing any buffering — the escape-sequence half of a restore. Async-signal-safe on Unix.

Source

fn trap(&self, on_interrupt: fn()) -> Result<()>

Run on_interrupt for each interrupt-class event, then exit.

on_interrupt must respect HANDLER_CONTRACT. A plain fn rather than a closure so there is nothing to allocate or capture on the Unix path.

Source

fn interrupt_exit_code(&self) -> i32

The process exit code for “killed by an interrupt”.

128 + signal is a POSIX shell convention with no Windows meaning. Windows returns 130 for Ctrl-C anyway, because scripts and CI check for it — a deliberate borrowing, flagged here rather than left to look like an accident.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§