From 65b7d249a34a944e7a2b93760f182abddd07b060 Mon Sep 17 00:00:00 2001 From: eld4niz Date: Wed, 3 Apr 2024 02:12:44 +0200 Subject: [PATCH 1/3] feature: add a panic hook to reset terminal upon panic --- src/main.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main.rs b/src/main.rs index 7c1d394..f6c01db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use ratatui::backend::TermionBackend; use ratatui::Terminal; use std::error::Error; use std::io::stdout; +use std::io::{self, Write}; +use std::panic; use termion::input::MouseTerminal; use termion::raw::IntoRawMode; use termion::screen::IntoAlternateScreen; @@ -15,6 +17,32 @@ use termion::screen::IntoAlternateScreen; * @return Result */ fn main() -> Result<(), Box> { + + let raw_output = io::stdout().into_raw_mode()?; + raw_output.suspend_raw_mode()?; + + let panic_hook = panic::take_hook(); + + panic::set_hook(Box::new (move |panic| { + let panic_cleanup = || -> Result<(), Box> { + let mut output = io::stdout(); + + write!( + output, + "{}{}{}", + termion::clear::All, + termion::screen::ToMainScreen, + termion::cursor::Show + )?; + raw_output.suspend_raw_mode()?; + output.flush()?; + Ok(()) + }; + + panic_cleanup().expect("Failed to cleanup after panic"); + panic_hook(panic); + })); + let args = args::get_args().get_matches(); let kernel = Kernel::new(&args); let events = Events::new( From 9b6afb2abeb62da250ec0da88643506168fae36d Mon Sep 17 00:00:00 2001 From: eld4niz Date: Wed, 3 Apr 2024 19:10:48 +0200 Subject: [PATCH 2/3] setup_panic_hook implemented --- src/main.rs | 30 ++---------------------------- src/util.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index f6c01db..1210a6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ use kmon::args; +use kmon::util; use kmon::event::Events; use kmon::kernel::Kernel; use ratatui::backend::TermionBackend; use ratatui::Terminal; use std::error::Error; use std::io::stdout; -use std::io::{self, Write}; -use std::panic; use termion::input::MouseTerminal; use termion::raw::IntoRawMode; use termion::screen::IntoAlternateScreen; @@ -17,32 +16,6 @@ use termion::screen::IntoAlternateScreen; * @return Result */ fn main() -> Result<(), Box> { - - let raw_output = io::stdout().into_raw_mode()?; - raw_output.suspend_raw_mode()?; - - let panic_hook = panic::take_hook(); - - panic::set_hook(Box::new (move |panic| { - let panic_cleanup = || -> Result<(), Box> { - let mut output = io::stdout(); - - write!( - output, - "{}{}{}", - termion::clear::All, - termion::screen::ToMainScreen, - termion::cursor::Show - )?; - raw_output.suspend_raw_mode()?; - output.flush()?; - Ok(()) - }; - - panic_cleanup().expect("Failed to cleanup after panic"); - panic_hook(panic); - })); - let args = args::get_args().get_matches(); let kernel = Kernel::new(&args); let events = Events::new( @@ -53,6 +26,7 @@ fn main() -> Result<(), Box> { &kernel.logs, ); if !cfg!(test) { + util::setup_panic_hook()?; let stdout = stdout().into_raw_mode()?.into_alternate_screen()?; let stdout = MouseTerminal::from(stdout); let backend = TermionBackend::new(stdout); diff --git a/src/util.rs b/src/util.rs index a1c0268..817fe02 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,8 @@ use std::process::Command; +use std::error::Error; +use std::io::{self, Write}; +use std::panic; +use termion::raw::IntoRawMode; /* Macro for concise initialization of hashmap */ macro_rules! map { @@ -62,6 +66,33 @@ pub fn exec_cmd(cmd: &str, cmd_args: &[&str]) -> Result { } } +pub fn setup_panic_hook() -> Result<(), Box> { + let raw_output = io::stdout().into_raw_mode()?; + + raw_output.suspend_raw_mode()?; + + let panic_hook = panic::take_hook(); + panic::set_hook(Box::new(move |panic| { + let panic_cleanup = || -> Result<(), Box> { + let mut output = io::stdout(); + write!( + output, + "{}{}{}", + termion::clear::All, + termion::screen::ToMainScreen, + termion::cursor::Show + )?; + raw_output.suspend_raw_mode()?; + output.flush()?; + Ok(()) + }; + panic_cleanup().expect("failed to clean up for panic"); + panic_hook(panic); + })); + + Ok(()) +} + #[cfg(test)] mod tests { use super::*; From 0e57843af9bd239c4b2c2d925f9a18e909c2b889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Thu, 4 Apr 2024 00:32:53 +0300 Subject: [PATCH 3/3] refactor: clean up implementation --- src/main.rs | 2 +- src/util.rs | 52 +++++++++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1210a6b..8b0e677 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use kmon::args; -use kmon::util; use kmon::event::Events; use kmon::kernel::Kernel; +use kmon::util; use ratatui::backend::TermionBackend; use ratatui::Terminal; use std::error::Error; diff --git a/src/util.rs b/src/util.rs index 817fe02..dbd499c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,7 @@ -use std::process::Command; use std::error::Error; use std::io::{self, Write}; use std::panic; +use std::process::Command; use termion::raw::IntoRawMode; /* Macro for concise initialization of hashmap */ @@ -66,31 +66,37 @@ pub fn exec_cmd(cmd: &str, cmd_args: &[&str]) -> Result { } } +/** + * Sets up the panic hook for the terminal. + * + * See + * + * @return Result + */ pub fn setup_panic_hook() -> Result<(), Box> { - let raw_output = io::stdout().into_raw_mode()?; - - raw_output.suspend_raw_mode()?; + let raw_output = io::stdout().into_raw_mode()?; + raw_output.suspend_raw_mode()?; - let panic_hook = panic::take_hook(); - panic::set_hook(Box::new(move |panic| { - let panic_cleanup = || -> Result<(), Box> { - let mut output = io::stdout(); - write!( - output, - "{}{}{}", - termion::clear::All, - termion::screen::ToMainScreen, - termion::cursor::Show - )?; - raw_output.suspend_raw_mode()?; - output.flush()?; - Ok(()) - }; - panic_cleanup().expect("failed to clean up for panic"); - panic_hook(panic); - })); + let panic_hook = panic::take_hook(); + panic::set_hook(Box::new(move |panic| { + let panic_cleanup = || -> Result<(), Box> { + let mut output = io::stdout(); + write!( + output, + "{}{}{}", + termion::clear::All, + termion::screen::ToMainScreen, + termion::cursor::Show + )?; + raw_output.suspend_raw_mode()?; + output.flush()?; + Ok(()) + }; + panic_cleanup().expect("failed to clean up for panic"); + panic_hook(panic); + })); - Ok(()) + Ok(()) } #[cfg(test)]