diff --git a/src/main.rs b/src/main.rs index a68ff96..8e68b02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,9 +219,12 @@ fn main() -> crossterm::Result<()> { )?; terminal.clear()?; - let mut state = State::Test(Test::new(opt.gen_contents().expect( - "Couldn't get test contents. Make sure the specified language actually exists.", - ))); + let mut state = State::Test(Test::new( + opt.gen_contents().expect( + "Couldn't get test contents. Make sure the specified language actually exists.", + ), + !opt.no_backtrack, + )); state.render_into(&mut terminal, &config)?; loop { @@ -252,7 +255,7 @@ fn main() -> crossterm::Result<()> { match state { State::Test(ref mut test) => { if let Event::Key(key) = event { - test.handle_key(key, opt.no_backtrack); + test.handle_key(key); if test.complete { state = State::Results(Results::from(&*test)); } @@ -265,9 +268,12 @@ fn main() -> crossterm::Result<()> { modifiers: KeyModifiers::NONE, .. }) => { - state = State::Test(Test::new(opt.gen_contents().expect( + state = State::Test(Test::new( + opt.gen_contents().expect( "Couldn't get test contents. Make sure the specified language actually exists.", - ))); + ), + !opt.no_backtrack + )); } Event::Key(KeyEvent { code: KeyCode::Char('p'), @@ -284,7 +290,7 @@ fn main() -> crossterm::Result<()> { .flat_map(|w| vec![w.clone(); 5]) .collect(); practice_words.shuffle(&mut thread_rng()); - state = State::Test(Test::new(practice_words)); + state = State::Test(Test::new(practice_words, !opt.no_backtrack)); } Event::Key(KeyEvent { code: KeyCode::Char('q'), diff --git a/src/test/mod.rs b/src/test/mod.rs index 01c3e48..dc33e2f 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -41,18 +41,20 @@ pub struct Test { pub words: Vec, pub current_word: usize, pub complete: bool, + pub backtracking_enabled: bool, } impl Test { - pub fn new(words: Vec) -> Self { + pub fn new(words: Vec, backtracking_enabled: bool) -> Self { Self { words: words.into_iter().map(TestWord::from).collect(), current_word: 0, complete: false, + backtracking_enabled, } } - pub fn handle_key(&mut self, key: KeyEvent, no_backtrack: bool) { + pub fn handle_key(&mut self, key: KeyEvent) { if key.kind != KeyEventKind::Press { return; } @@ -77,7 +79,7 @@ impl Test { } } KeyCode::Backspace => { - if word.progress.is_empty() && !no_backtrack { + if word.progress.is_empty() && self.backtracking_enabled { self.last_word(); } else { word.events.push(TestEvent {