diff --git a/src/main.rs b/src/main.rs index bc5a3e5..402dab4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod test; mod ui; use config::Config; +use std::ops::Drop; use test::{results::Results, Test}; use crossterm::{ @@ -98,6 +99,10 @@ impl Opt { .map(|f| f.data.into_owned()) })?; + if bytes.is_empty() { + panic!("Language file had not content."); + } + let mut rng = thread_rng(); let mut language: Vec<&str> = str::from_utf8(&bytes) @@ -171,6 +176,17 @@ enum State { } impl State { + fn set_modes(&self) -> crossterm::Result<()> { + terminal::enable_raw_mode()?; + execute!( + io::stdout(), + cursor::Hide, + cursor::SavePosition, + terminal::EnterAlternateScreen, + )?; + Ok(()) + } + fn render_into( &self, terminal: &mut Terminal, @@ -192,6 +208,23 @@ impl State { } } +impl Drop for State { + fn drop(&mut self) { + let _ = terminal::disable_raw_mode().map_err(|err| { + eprintln!("Couldn't disable raw mode: {err}"); + }); + let _ = execute!( + io::stdout(), + cursor::RestorePosition, + cursor::Show, + terminal::LeaveAlternateScreen, + ) + .map_err(|err| { + eprintln!("Couldn't restore default terminal settings: {err}"); + }); + } +} + fn main() -> crossterm::Result<()> { let opt = Opt::from_args(); if opt.debug { @@ -214,13 +247,6 @@ fn main() -> crossterm::Result<()> { let backend = CrosstermBackend::new(io::stdout()); let mut terminal = Terminal::new(backend)?; - terminal::enable_raw_mode()?; - execute!( - io::stdout(), - cursor::Hide, - cursor::SavePosition, - terminal::EnterAlternateScreen, - )?; terminal.clear()?; let mut state = State::Test(Test::new( @@ -229,6 +255,7 @@ fn main() -> crossterm::Result<()> { ), !opt.no_backtrack, )); + state.set_modes()?; state.render_into(&mut terminal, &config)?; loop { @@ -309,13 +336,5 @@ fn main() -> crossterm::Result<()> { state.render_into(&mut terminal, &config)?; } - terminal::disable_raw_mode()?; - execute!( - io::stdout(), - cursor::RestorePosition, - cursor::Show, - terminal::LeaveAlternateScreen, - )?; - Ok(()) }