Be more robust in restoring canonical mode
This commit is contained in:
parent
8825cdb058
commit
44a1d5ce9a
28
src/main.rs
28
src/main.rs
@ -22,6 +22,28 @@ lazy_static! {
|
|||||||
pub static ref ORIGINAL_TERMIOS: Arc<Mutex<Termios>> = Arc::new(Mutex::new(get_termios(STDOUT_FILENO)));
|
pub static ref ORIGINAL_TERMIOS: Arc<Mutex<Termios>> = Arc::new(Mutex::new(get_termios(STDOUT_FILENO)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct OnExit<T: FnOnce() -> ()> {
|
||||||
|
destructor: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> OnExit<T> where T: FnOnce() -> () {
|
||||||
|
fn new(destructor: T) -> OnExit<T> {
|
||||||
|
OnExit {
|
||||||
|
destructor: Some(destructor),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for OnExit<T> where T: FnOnce() -> () {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if self.destructor.is_some() {
|
||||||
|
let option = self.destructor.take();
|
||||||
|
let destructor = option.unwrap();
|
||||||
|
destructor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
// Save existing Termios settings
|
// Save existing Termios settings
|
||||||
lazy_static::initialize(&ORIGINAL_TERMIOS);
|
lazy_static::initialize(&ORIGINAL_TERMIOS);
|
||||||
@ -29,6 +51,9 @@ fn main() -> Result<(), Error> {
|
|||||||
// Disable canonical/"cooked" terminal mode
|
// Disable canonical/"cooked" terminal mode
|
||||||
enable_raw_mode();
|
enable_raw_mode();
|
||||||
|
|
||||||
|
// Revert raw mode on exit
|
||||||
|
let _destructor = OnExit::new(disable_raw_mode);
|
||||||
|
|
||||||
// Initialize the editor
|
// Initialize the editor
|
||||||
let mut editor = Editor::new();
|
let mut editor = Editor::new();
|
||||||
|
|
||||||
@ -51,8 +76,5 @@ fn main() -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore previous terminal flags before exit
|
|
||||||
disable_raw_mode();
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user