Skip to content

Commit

Permalink
refactor(test): move sudden death logic to Test
Browse files Browse the repository at this point in the history
- Move logic for checking if a user makes a mistake during sudden death
  mode out of `src/main.rs` and into `src/test/mod.rs`. Note that this
  changes how sudden death mode works: previously, if a user made a
  mistake in sudden death mode, they would immediately begin a new test;
  now, they restart the current test.
  • Loading branch information
PappasBrent authored and max-niederman committed Feb 2, 2024
1 parent 0818d34 commit cb4fb14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
21 changes: 1 addition & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod test;
mod ui;

use config::Config;
use test::{is_missed_word_event, results::Results, Test};
use test::{results::Results, Test};

use crossterm::{
self, cursor,
Expand Down Expand Up @@ -267,25 +267,6 @@ fn main() -> io::Result<()> {
test.handle_key(key);
if test.complete {
state = State::Results(Results::from(&*test));
} else if test.sudden_death_enabled {
if test.words[test.current_word]
.events
.last()
.is_some_and(is_missed_word_event)
|| test.current_word > 0
&& test.words[test.current_word - 1]
.events
.last()
.is_some_and(is_missed_word_event)
{
state = State::Test(Test::new(
opt.gen_contents().expect(
"Couldn't get test contents. Make sure the specified language actually exists.",
),
!opt.no_backtrack,
opt.sudden_death
));
}
}
}
}
Expand Down
47 changes: 33 additions & 14 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ impl Test {
key,
})
} else if !word.progress.is_empty() || word.text.is_empty() {
word.events.push(TestEvent {
time: Instant::now(),
correct: Some(word.text == word.progress),
key,
});
self.next_word();
let correct = word.text == word.progress;
if self.sudden_death_enabled && !correct {
self.reset();
} else {
word.events.push(TestEvent {
time: Instant::now(),
correct: Some(correct),
key,
});
self.next_word();
}
}
}
KeyCode::Backspace => {
Expand Down Expand Up @@ -121,14 +126,19 @@ impl Test {
}
KeyCode::Char(c) => {
word.progress.push(c);
word.events.push(TestEvent {
time: Instant::now(),
correct: Some(word.text.starts_with(&word.progress[..])),
key,
});
if word.progress == word.text && self.current_word == self.words.len() - 1 {
self.complete = true;
self.current_word = 0;
let correct = word.text.starts_with(&word.progress[..]);
if self.sudden_death_enabled && !correct {
self.reset();
} else {
word.events.push(TestEvent {
time: Instant::now(),
correct: Some(correct),
key,
});
if word.progress == word.text && self.current_word == self.words.len() - 1 {
self.complete = true;
self.current_word = 0;
}
}
}
_ => {}
Expand All @@ -149,4 +159,13 @@ impl Test {
self.current_word += 1;
}
}

fn reset(&mut self) {
self.words.iter_mut().for_each(|word: &mut TestWord| {
word.progress.clear();
word.events.clear();
});
self.current_word = 0;
self.complete = false;
}
}

0 comments on commit cb4fb14

Please sign in to comment.