A small typing test program in Rust
Commit d29b02a0114e
tarball3 files changed+10-12
@@ -53,7 +53,6 @@ pub struct TypingTest {
5353
status: Status,5454
errors: usize,5555
typing: TextCombine,56
- words: usize,5756
timer: Timer,5857
}5958
@@ -64,7 +63,6 @@ impl Default for TypingTest {
6463
Self {6564
status: Running,6665
errors: 0,67
- words: 0,6866
typing: TextCombine::new(),6967
timer: Timer::new(Duration::from_secs(30))7068
}@@ -193,9 +191,6 @@ impl TypingTest {
193191
&& self.typing.get_err_s().len() == 0 194192
{195193
self.typing.pos_increment();196
- if c == ' ' {197
- self.words += 1;198
- }199194
} else if c != ' ' {200195
if curr != ' ' {201196
self.typing.err_n_increment();@@ -289,7 +284,7 @@ impl TypingTest {
289284
},290285
" // Mistakes ".into(),291286
self.errors.to_string().into(),292
- (" ").into()287
+ " ".into(),293288
]);294289
let bottom = Line::from(vec![295290
" Exit ".into(),@@ -335,7 +330,7 @@ impl TypingTest {
335330
336331
337332
fn get_wpm(&self) -> f64 {338
- (((self.typing.get_pos() + 1) as f64 / 5.0) 333
+ (((self.typing.get_pos()) as f64 / 5.0) 339334
/ self.timer.elapsed().as_secs_f64())340335
* 60.0341336
}@@ -11,9 +11,8 @@ pub struct TextCombine {
1111
1212
impl TextCombine {1313
pub fn new() -> Self {14
- let words: Vec<String> = include_str!("../assets/wordlist.txt")14
+ let words: Vec<&str> = include_str!("../assets/wordlist.txt")1515
.lines()16
- .map(str::to_owned)1716
.collect();1817
1918
@@ -26,8 +25,8 @@ impl TextCombine {
2625
2726
let text = (0..60)2827
.filter_map(|_| words.choose(&mut rng))29
- .cloned()30
- .collect::<Vec<_>>()28
+ .copied()29
+ .collect::<Vec<&str>>()3130
.join(" ");3231
3332
let len = text.chars().count();@@ -27,7 +27,11 @@ impl Timer {
2727
2828
pub fn elapsed (&self) -> Duration {2929
match self.start {30
- Some(time) => time.elapsed(),30
+ Some(time) => if time.elapsed() < self.duration {31
+ time.elapsed()32
+ } else {33
+ self.duration34
+ },3135
None => Duration::ZERO,3236
}3337
}