Skip to main content

TerminalGuard

Struct TerminalGuard 

Source
pub struct TerminalGuard { /* private fields */ }
Expand description

RAII guard that restores the terminal on drop — including during a panic.

Why: see the module doc comment. Holding this guard for the lifetime of the render loop is what makes terminal restoration unconditional: there is no code path (early return, ?, or panic) that can skip Drop. What: constructed by TerminalGuard::enter, which is the ONLY way to get one — that constructor is also what performs the matching “enter” side effects, so a TerminalGuard existing is always proof the terminal was actually put into raw/alt-screen/mouse-capture mode. Drop undoes all three, ignoring errors (nothing useful can be done while unwinding). Test: tests::drop_restores_on_normal_scope_exit, tests::drop_restores_on_panic_unwind.

Implementations§

Source§

impl TerminalGuard

Source

pub fn enter() -> Result<(Self, Terminal<CrosstermBackend<Stdout>>)>

Enter raw mode, the alternate screen, and mouse capture, returning a guard (whose Drop restores all three) alongside a ready-to-draw ratatui::Terminal.

Why: bundling guard + terminal construction into one fallible call means a caller can never end up with a live Terminal and no guard (or vice versa) — the two are only ever created together. What: enable_raw_mode() first; if entering the alternate screen or mouse capture then fails, raw mode is unwound before the error is returned (so a failed enter() never leaves the terminal half-mutated for the caller to clean up). On success, builds a CrosstermBackend<Stdout>-backed Terminal. Test: exercised transitively by crate::run’s event-loop tests via ratatui::backend::TestBackend; enter() itself needs a real TTY so it is not unit-tested here (mirrors the precedent in crates/trusty-agents/src/repl/tui/run.rs::setup_terminal, which is likewise exercised only by launching the TUI).

Trait Implementations§

Source§

impl Drop for TerminalGuard

Source§

fn drop(&mut self)

Why: see TerminalGuard — runs on both normal return and panic unwind, which is the entire point of this type existing. What: delegates to the guard’s [TerminalOps::leave]. Test: tests::drop_restores_on_normal_scope_exit, tests::drop_restores_on_panic_unwind.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more