Skip to content

Commit

Permalink
refactor(test): store backtracking option in Test
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman committed Sep 30, 2023
1 parent ad82ee7 commit 3cf7e1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
Expand All @@ -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'),
Expand All @@ -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'),
Expand Down
8 changes: 5 additions & 3 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ pub struct Test {
pub words: Vec<TestWord>,
pub current_word: usize,
pub complete: bool,
pub backtracking_enabled: bool,
}

impl Test {
pub fn new(words: Vec<String>) -> Self {
pub fn new(words: Vec<String>, 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;
}
Expand All @@ -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 {
Expand Down

0 comments on commit 3cf7e1f

Please sign in to comment.