refactor: extract terminal guard
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
use crossterm::{
|
||||||
|
ExecutableCommand,
|
||||||
|
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
|
||||||
|
};
|
||||||
|
use std::io::{self, stdout};
|
||||||
|
|
||||||
|
pub struct TerminalGuard;
|
||||||
|
|
||||||
|
impl TerminalGuard {
|
||||||
|
pub 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user