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

Add "practice missed words" mode #89

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn main() -> crossterm::Result<()> {
}
}
}
State::Results(_) => match event {
State::Results(ref result) => match event {
Event::Key(KeyEvent {
code: KeyCode::Char('r'),
kind: KeyEventKind::Press,
Expand All @@ -260,6 +260,17 @@ fn main() -> crossterm::Result<()> {
"Couldn't get test contents. Make sure the specified language actually exists.",
)));
}
Event::Key(KeyEvent {
code: KeyCode::Char('p'),
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
..
}) => {
if result.missed_words.is_empty() {
continue;
}
state = State::Test(Test::from_missed_words(&result.missed_words));
}
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
kind: KeyEventKind::Press,
Expand Down
13 changes: 13 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod results;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use std::fmt;
use std::time::Instant;
use rand::seq::SliceRandom;

pub struct TestEvent {
pub time: Instant,
Expand Down Expand Up @@ -52,6 +53,18 @@ impl Test {
}
}

pub fn from_missed_words(missed_words: &Vec<String>) -> Self {
glazari marked this conversation as resolved.
Show resolved Hide resolved
// repeat each word 5 times
let mut missed_words: Vec<String> = missed_words
.into_iter()
.flat_map(|w| vec![w.clone(); 5])
.collect();

missed_words.shuffle(&mut rand::thread_rng());

Self::new(missed_words)
}

pub fn handle_key(&mut self, key: KeyEvent) {
if key.kind != KeyEventKind::Press {
return;
Expand Down
142 changes: 82 additions & 60 deletions src/test/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct AccuracyData {
pub struct Results {
pub timing: TimingData,
pub accuracy: AccuracyData,
pub missed_words: Vec<String>,
}

impl From<&Test> for Results {
Expand All @@ -79,67 +80,88 @@ impl From<&Test> for Results {
test.words.iter().flat_map(|w| w.events.iter()).collect();

Self {
timing: {
let mut timing = TimingData {
overall_cps: -1.0,
per_event: Vec::new(),
per_key: HashMap::new(),
};

// map of keys to a two-tuple (total time, clicks) for counting average
let mut keys: HashMap<KeyEvent, (f64, usize)> = HashMap::new();

for win in events.windows(2) {
let event_dur = win[1]
.time
.checked_duration_since(win[0].time)
.map(|d| d.as_secs_f64());

if let Some(event_dur) = event_dur {
timing.per_event.push(event_dur);

let key = keys.entry(win[1].key).or_insert((0.0, 0));
key.0 += event_dur;
key.1 += 1;
}
}
timing: Self::calc_timing(&events),
accuracy: Self::calc_accuracy(&events),
missed_words: Self::calc_missed_words(&test),
}
}

}

timing.per_key = keys
.into_iter()
.map(|(key, (total, count))| (key, total / count as f64))
.collect();

timing.overall_cps =
timing.per_event.len() as f64 / timing.per_event.iter().sum::<f64>();

timing
},
accuracy: {
let mut acc = AccuracyData {
overall: Fraction::new(0, 0),
per_key: HashMap::new(),
};

events
.iter()
.filter(|event| event.correct.is_some())
.for_each(|event| {
let key = acc
.per_key
.entry(event.key)
.or_insert_with(|| Fraction::new(0, 0));

acc.overall.denominator += 1;
key.denominator += 1;

if event.correct.unwrap() {
acc.overall.numerator += 1;
key.numerator += 1;
}
});

acc
},
impl Results {
glazari marked this conversation as resolved.
Show resolved Hide resolved
fn calc_timing(events: &Vec<&super::TestEvent>) -> TimingData {
let mut timing = TimingData {
overall_cps: -1.0,
per_event: Vec::new(),
per_key: HashMap::new(),
};

// map of keys to a two-tuple (total time, clicks) for counting average
let mut keys: HashMap<KeyEvent, (f64, usize)> = HashMap::new();

for win in events.windows(2) {
let event_dur = win[1]
.time
.checked_duration_since(win[0].time)
.map(|d| d.as_secs_f64());

if let Some(event_dur) = event_dur {
timing.per_event.push(event_dur);

let key = keys.entry(win[1].key).or_insert((0.0, 0));
key.0 += event_dur;
key.1 += 1;
}
}

timing.per_key = keys
.into_iter()
.map(|(key, (total, count))| (key, total / count as f64))
.collect();

timing.overall_cps =
timing.per_event.len() as f64 / timing.per_event.iter().sum::<f64>();

timing
}

fn calc_accuracy(events: &Vec<&super::TestEvent>) -> AccuracyData {
let mut acc = AccuracyData {
overall: Fraction::new(0, 0),
per_key: HashMap::new(),
};

events
.iter()
.filter(|event| event.correct.is_some())
.for_each(|event| {
let key = acc
.per_key
.entry(event.key)
.or_insert_with(|| Fraction::new(0, 0));

acc.overall.denominator += 1;
key.denominator += 1;

if event.correct.unwrap() {
acc.overall.numerator += 1;
key.numerator += 1;
}
});

acc
}

fn calc_missed_words(test: &Test) -> Vec<String> {
let is_missed_word_event = |event: &super::TestEvent| -> bool {
event.correct == Some(false) || event.correct.is_none()
};


test.words
.iter()
.filter(|word| word.events.iter().any(is_missed_word_event))
.map(|word| word.text.clone())
.collect()
}
}
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl ThemedWidget for &results::Results {
.split(res_chunks[0]);

let exit = Span::styled(
"Press 'q' to quit or 'r' for another test.",
"Press 'q' to quit or 'r' for another test or 'p' to practice missed words",
glazari marked this conversation as resolved.
Show resolved Hide resolved
theme.results_restart_prompt,
);
buf.set_span(chunks[1].x, chunks[1].y, &exit, chunks[1].width);
Expand Down