Expand description
The panic-safe terminal lifecycle: TerminalGuard.
Why: tagent’s current run_tui (crates/trusty-agents/src/repl/tui/run.rs
setup_terminal/restore_terminal) only restores the terminal AFTER
event_loop() returns normally — a panic anywhere inside the loop unwinds
straight past that teardown call and leaves the operator’s terminal in raw
mode + the alternate screen (no echo, no visible prompt, Ctrl-C doesn’t
even show up). That is a real bug in the code this crate will eventually
replace, not a spec this crate should reproduce. trusty-mpm’s coordinator
TUI (crates/trusty-mpm/src/tui/coordinator/mod.rs TerminalGuard) already
fixed this the right way — restoration lives in Drop, which Rust runs on
BOTH the normal return path and the panic-unwind path — and this module
generalizes that fix to be the one terminal-lifecycle type both tagent
(Slice 10) and tcode (Slice 3) share.
What: TerminalGuard::enter performs the three terminal-mutating steps
(raw mode, alternate screen, mouse capture) and returns a guard alongside
a ready-to-use ratatui::Terminal. The guard’s Drop best-effort undoes
all three; every step swallows its own error since a Drop impl cannot
propagate one and a partial restore is still strictly better than none.
The actual OS-level restore calls are routed through the private
[TerminalOps] trait so tests can substitute a recording fake instead
of exercising a real TTY — sandboxes and CI routinely have no controlling
terminal at all, and the property under test (“does Drop run during
unwind”) has nothing to do with whether disable_raw_mode() itself would
succeed on this particular host.
§Spec References
SPEC-TTUI-05~draft— Slice 2 deliverable (§5, Slice 2): panic-safe terminal guard.
Structs§
- Terminal
Guard - RAII guard that restores the terminal on drop — including during a panic.