Fix: Better wpm formula

This commit is contained in:
zuma 2025-07-01 17:03:17 +02:00
parent 718b4c49ff
commit a67efb0d84

View file

@ -13,7 +13,6 @@ pub struct Typer {
should_display_result: bool, should_display_result: bool,
words_to_write: WordList, words_to_write: WordList,
chars_written: Vec<char>, chars_written: Vec<char>,
current_num_of_words: u16,
wpm: u16, wpm: u16,
errors: u16, errors: u16,
start_timestamp: Option<SystemTime>, start_timestamp: Option<SystemTime>,
@ -27,7 +26,6 @@ impl Typer {
should_display_result: false, should_display_result: false,
words_to_write: WordList::new(50), words_to_write: WordList::new(50),
chars_written: Vec::new() as Vec<char>, chars_written: Vec::new() as Vec<char>,
current_num_of_words: 0,
wpm: 0, wpm: 0,
errors: 0, errors: 0,
start_timestamp: None start_timestamp: None
@ -60,9 +58,7 @@ impl Typer {
self.should_quit = true; self.should_quit = true;
} }
Backspace => { Backspace => {
if self.chars_written.pop().unwrap() == ' ' && self.current_num_of_words > 0 { _ = self.chars_written.pop();
self.current_num_of_words -= 1;
}
} }
_ => { _ => {
if code.as_char().is_some() { if code.as_char().is_some() {
@ -79,16 +75,12 @@ impl Typer {
self.errors += 1; self.errors += 1;
} }
// Increase the number of written words on space.
if c == ' ' {
self.current_num_of_words += 1;
}
// Update WPM // Update WPM
match self.start_timestamp.unwrap().elapsed() { match self.start_timestamp.unwrap().elapsed() {
Ok(elapsed) => { Ok(elapsed) => {
if elapsed.as_secs() != 0 { if elapsed.as_secs() != 0 {
self.wpm = (f32::from(self.current_num_of_words) / (elapsed.as_secs() as f32 / 60_f32)) as u16; // WPM formula = (chars / 5) / mins, According to reddit https://www.reddit.com/r/typing/comments/1h014p7/comment/lz0rsl2/
self.wpm = ((self.chars_written.len() as f32 / 5_f32) / (elapsed.as_secs() as f32 / 60_f32)) as u16;
} }
} }
Err(_e) => { Err(_e) => {