Skip to content

Commit

Permalink
add "no backtrack" option to disable going back to previous words (#82)
Browse files Browse the repository at this point in the history
* Added --no-backtrack options for no backtrack between words

* refactor(test): store backtracking option in Test

---------

Co-authored-by: Max Niederman <max@maxniederman.com>
  • Loading branch information
gbrlmarn and max-niederman committed Sep 30, 2023
1 parent 5b33304 commit 30e7d0e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ USAGE:
ttyper [FLAGS] [OPTIONS] [contents]
FLAGS:
--no-backtrack Enable no backtrack between words
-d, --debug
-h, --help Prints help information
--list-languages List installed languages
Expand Down
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ struct Opt {
/// List installed languages
#[structopt(long)]
list_languages: bool,

/// Enable no backtrack between words
#[structopt(long)]
no_backtrack: bool,
}

impl Opt {
Expand Down Expand Up @@ -215,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 @@ -261,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 @@ -280,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
6 changes: 4 additions & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ 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,
}
}

Expand Down Expand Up @@ -77,7 +79,7 @@ impl Test {
}
}
KeyCode::Backspace => {
if word.progress.is_empty() {
if word.progress.is_empty() && self.backtracking_enabled {
self.last_word();
} else {
word.events.push(TestEvent {
Expand Down

0 comments on commit 30e7d0e

Please sign in to comment.