From 344132714ede0f71becc277f0439fe6ee4dc1f83 Mon Sep 17 00:00:00 2001 From: ProgKea <> Date: Sun, 26 Nov 2023 17:54:09 +0100 Subject: [PATCH 1/3] panic if the byte buffer is empty --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index bc5a3e5..2834d8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,6 +98,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) From 38b03d6f864546459c5df8f530c4193f3354fc91 Mon Sep 17 00:00:00 2001 From: ProgKea <> Date: Sun, 26 Nov 2023 18:04:58 +0100 Subject: [PATCH 2/3] implement drop trait to reset `raw_mode`, cursor hide... --- src/main.rs | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2834d8c..af0c4ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod ui; use config::Config; use test::{results::Results, Test}; +use std::ops::Drop; use crossterm::{ self, cursor, @@ -175,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, @@ -196,6 +208,22 @@ 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 { @@ -218,13 +246,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( @@ -233,6 +254,7 @@ fn main() -> crossterm::Result<()> { ), !opt.no_backtrack, )); + state.set_modes()?; state.render_into(&mut terminal, &config)?; loop { @@ -313,13 +335,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(()) } From dd798f757a4624b30517a295f2953f38854d8cc9 Mon Sep 17 00:00:00 2001 From: ProgKea <> Date: Sun, 26 Nov 2023 19:00:31 +0100 Subject: [PATCH 3/3] format with rustfmt --- src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index af0c4ab..402dab4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,8 @@ mod test; mod ui; use config::Config; -use test::{results::Results, Test}; use std::ops::Drop; +use test::{results::Results, Test}; use crossterm::{ self, cursor, @@ -218,7 +218,8 @@ impl Drop for State { cursor::RestorePosition, cursor::Show, terminal::LeaveAlternateScreen, - ).map_err(|err| { + ) + .map_err(|err| { eprintln!("Couldn't restore default terminal settings: {err}"); }); }