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 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
19 changes: 18 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,23 @@ 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;
}
// repeat each missed word 5 times
let mut practice_words: Vec<String> = (result.missed_words)
.iter()
.flat_map(|w| vec![w.clone(); 5])
.collect();
practice_words.shuffle(&mut thread_rng());
state = State::Test(Test::new(practice_words));
}
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
kind: KeyEventKind::Press,
Expand Down
139 changes: 78 additions & 61 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,83 @@ 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.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
},
timing: calc_timing(&events),
accuracy: calc_accuracy(&events),
missed_words: calc_missed_words(&test),
}
}
}

fn calc_timing(events: &[&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: &[&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()
}
11 changes: 7 additions & 4 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@ impl ThemedWidget for &results::Results {
.constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)])
.split(res_chunks[0]);

let exit = Span::styled(
"Press 'q' to quit or 'r' for another test.",
theme.results_restart_prompt,
);
let msg = if self.missed_words.is_empty() {
"Press 'q' to quit or 'r' for another test"
} else {
"Press 'q' to quit, 'r' for another test or 'p' to practice missed words"
};

let exit = Span::styled(msg, theme.results_restart_prompt);
buf.set_span(chunks[1].x, chunks[1].y, &exit, chunks[1].width);

// Sections
Expand Down
Loading