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
impl TerminalGuard
Sourcepub fn enter() -> Result<(Self, Terminal<CrosstermBackend<Stdout>>)>
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
impl Drop for TerminalGuard
Source§fn drop(&mut self)
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.
Auto Trait Implementations§
impl !RefUnwindSafe for TerminalGuard
impl !Sync for TerminalGuard
impl !UnwindSafe for TerminalGuard
impl Freeze for TerminalGuard
impl Send for TerminalGuard
impl Unpin for TerminalGuard
impl UnsafeUnpin for TerminalGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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