Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tab to next #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use serde::{
#[serde(default)]
pub struct Config {
pub default_language: String,
pub tab_to_next: bool,
pub theme: Theme,
}

impl Default for Config {
fn default() -> Self {
Self {
default_language: "english200".into(),
tab_to_next: false,
theme: Theme::default(),
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
#[folder = "resources/runtime"]
struct Resources;

#[derive(Debug, Parser)]
#[derive(Debug, Parser, Clone)]
#[command(about, version)]
struct Opt {
/// Read test contents from the specified file, or "-" for stdin
Expand Down Expand Up @@ -64,6 +64,10 @@ struct Opt {
/// Enable sudden death mode to restart on first error
#[arg(long)]
sudden_death: bool,

/// Click TAB to start new Test while writing a test
#[arg(long)]
tab_to_next: bool,
}

impl Opt {
Expand Down Expand Up @@ -234,6 +238,8 @@ fn main() -> io::Result<()> {
),
!opt.no_backtrack,
opt.sudden_death,
opt.tab_to_next || config.tab_to_next,
opt.clone(),
));

state.render_into(&mut terminal, &config)?;
Expand Down Expand Up @@ -283,7 +289,9 @@ fn main() -> io::Result<()> {
"Couldn't get test contents. Make sure the specified language actually exists.",
),
!opt.no_backtrack,
opt.sudden_death
opt.sudden_death,
opt.tab_to_next || config.tab_to_next,
opt.clone()
));
}
Event::Key(KeyEvent {
Expand All @@ -301,11 +309,15 @@ fn main() -> io::Result<()> {
.flat_map(|w| vec![w.clone(); 5])
.collect();
practice_words.shuffle(&mut thread_rng());
state = State::Test(Test::new(
let mut test = Test::new(
practice_words,
!opt.no_backtrack,
opt.sudden_death,
));
opt.tab_to_next || config.tab_to_next,
opt.clone(),
);
test.practice();
state = State::Test(test);
}
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
Expand Down
41 changes: 40 additions & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use std::fmt;
use std::time::Instant;

use rand::{seq::SliceRandom, thread_rng};

use crate::Opt;

pub struct TestEvent {
pub time: Instant,
pub key: KeyEvent,
Expand Down Expand Up @@ -53,16 +57,29 @@ pub struct Test {
pub complete: bool,
pub backtracking_enabled: bool,
pub sudden_death_enabled: bool,
pub tab_to_next_enabled: bool,

is_practice_mode: bool,
config: Opt,
}

impl Test {
pub fn new(words: Vec<String>, backtracking_enabled: bool, sudden_death_enabled: bool) -> Self {
pub fn new(
words: Vec<String>,
backtracking_enabled: bool,
sudden_death_enabled: bool,
tab_to_next_enabled: bool,
config: Opt,
) -> Self {
Self {
words: words.into_iter().map(TestWord::from).collect(),
current_word: 0,
complete: false,
backtracking_enabled,
sudden_death_enabled,
tab_to_next_enabled,
is_practice_mode: false,
config,
}
}

Expand Down Expand Up @@ -141,6 +158,11 @@ impl Test {
}
}
}
KeyCode::Tab => {
if self.tab_to_next_enabled {
self.next_test();
}
}
_ => {}
};
}
Expand Down Expand Up @@ -168,4 +190,21 @@ impl Test {
self.current_word = 0;
self.complete = false;
}

pub fn practice(&mut self) {
self.is_practice_mode = true;
}

fn next_test(&mut self) {
if self.is_practice_mode {
self.words.shuffle(&mut thread_rng());
self.reset();
} else {
self.reset();
let new_test_words = self.config.gen_contents().expect(
"Couldn't get test contents. Make sure the specified language actually exists.",
);
self.words = new_test_words.into_iter().map(TestWord::from).collect();
}
}
}
Loading