fix: use terminal guard which allows drop-based cleanup

This commit is contained in:
Stevan Freeborn
2026-07-22 22:44:17 -05:00
parent 879ca2c6c6
commit 41898c1b53
+18 -4
View File
@@ -125,9 +125,25 @@ impl Termato {
}
}
struct TerminalGuard;
impl TerminalGuard {
fn new() -> Result<Self, io::Error> {
enable_raw_mode()?;
stdout().execute(EnterAlternateScreen)?;
Ok(TerminalGuard)
}
}
impl Drop for TerminalGuard {
fn drop(&mut self) {
let _ = disable_raw_mode();
let _ = stdout().execute(LeaveAlternateScreen);
}
}
fn main() -> Result<(), io::Error> {
enable_raw_mode()?;
stdout().execute(EnterAlternateScreen)?;
let _guard = TerminalGuard::new()?;
let backend = CrosstermBackend::new(stdout());
let mut terminal = Terminal::new(backend)?;
@@ -249,8 +265,6 @@ fn main() -> Result<(), io::Error> {
}
}
disable_raw_mode()?;
stdout().execute(LeaveAlternateScreen)?;
Ok(())
}