From cd6995277b7cb18e49d517465f94e39941b17039 Mon Sep 17 00:00:00 2001 From: Yauheni Saihak Date: Tue, 3 Jan 2023 11:34:13 +0300 Subject: [PATCH 1/3] add death mode --- src/main.rs | 10 ++++++++-- src/thok.rs | 19 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index b2d5eaa..96542da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,10 @@ pub struct Cli { /// language to pull words from #[clap(short = 'l', long, arg_enum, default_value_t = SupportedLanguage::English)] supported_language: SupportedLanguage, + + /// is death mode enabled + #[clap(short = 'd', long = "death-mode")] + death_mode: bool, } #[derive(Debug, Copy, Clone, ArgEnum, strum_macros::Display)] @@ -88,7 +92,7 @@ impl App { }; if cli.number_of_sentences.is_some() { Self { - thok: Thok::new(prompt, count, cli.number_of_secs.map(|ns| ns as f64)), + thok: Thok::new(prompt, count, cli.number_of_secs.map(|ns| ns as f64), cli.death_mode), cli: Some(cli), } } else { @@ -97,6 +101,7 @@ impl App { prompt, cli.number_of_words, cli.number_of_secs.map(|ns| ns as f64), + cli.death_mode, ), cli: Some(cli), } @@ -123,12 +128,13 @@ impl App { }, }; if cli.number_of_sentences.is_some() { - self.thok = Thok::new(prompt, count, cli.number_of_secs.map(|ns| ns as f64)); + self.thok = Thok::new(prompt, count, cli.number_of_secs.map(|ns| ns as f64), cli.death_mode); } else { self.thok = Thok::new( prompt, cli.number_of_words, cli.number_of_secs.map(|ns| ns as f64), + cli.death_mode, ); } } diff --git a/src/thok.rs b/src/thok.rs index c7ec39e..d6e44f0 100644 --- a/src/thok.rs +++ b/src/thok.rs @@ -32,13 +32,14 @@ pub struct Thok { pub seconds_remaining: Option, pub number_of_secs: Option, pub number_of_words: usize, + pub death_mode: bool, pub wpm: f64, pub accuracy: f64, pub std_dev: f64, } impl Thok { - pub fn new(prompt: String, number_of_words: usize, number_of_secs: Option) -> Self { + pub fn new(prompt: String, number_of_words: usize, number_of_secs: Option, death_mode: bool) -> Self { Self { prompt, input: vec![], @@ -48,6 +49,7 @@ impl Thok { started_at: None, number_of_secs, number_of_words, + death_mode, seconds_remaining: number_of_secs, wpm: 0.0, accuracy: 0.0, @@ -189,8 +191,19 @@ impl Thok { } pub fn has_finished(&self) -> bool { - (self.input.len() == self.prompt.len()) - || (self.seconds_remaining.is_some() && self.seconds_remaining.unwrap() <= 0.0) + let mut finished = (self.input.len() == self.prompt.len()) + || (self.seconds_remaining.is_some() && self.seconds_remaining.unwrap() <= 0.0); + + if self.death_mode { + let has_incorrect = self + .input + .clone() + .into_iter() + .any(|i| i.outcome == Outcome::Incorrect); + finished = finished || has_incorrect; + } + + return finished; } pub fn save_results(&self) -> io::Result<()> { From 00fa2918c2e694d05b391974a2fc5b2174bab189 Mon Sep 17 00:00:00 2001 From: Colby Thomas Date: Fri, 6 Jan 2023 01:03:38 -0700 Subject: [PATCH 2/3] WIP - small cleanup, add death emoji to test if failed in death mode --- src/thok.rs | 21 ++++++++++++--------- src/ui.rs | 24 +++++++++++++++++++----- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/thok.rs b/src/thok.rs index d6e44f0..bdc3724 100644 --- a/src/thok.rs +++ b/src/thok.rs @@ -39,7 +39,12 @@ pub struct Thok { } impl Thok { - pub fn new(prompt: String, number_of_words: usize, number_of_secs: Option, death_mode: bool) -> Self { + pub fn new( + prompt: String, + number_of_words: usize, + number_of_secs: Option, + death_mode: bool, + ) -> Self { Self { prompt, input: vec![], @@ -191,19 +196,17 @@ impl Thok { } pub fn has_finished(&self) -> bool { - let mut finished = (self.input.len() == self.prompt.len()) - || (self.seconds_remaining.is_some() && self.seconds_remaining.unwrap() <= 0.0); - - if self.death_mode { - let has_incorrect = self + let finished_prompt = self.input.len() == self.prompt.len(); + let out_of_time = + self.seconds_remaining.is_some() && self.seconds_remaining.unwrap() <= 0.0; + let is_fatal_error = self.death_mode + && self .input .clone() .into_iter() .any(|i| i.outcome == Outcome::Incorrect); - finished = finished || has_incorrect; - } - return finished; + finished_prompt || out_of_time || is_fatal_error } pub fn save_results(&self) -> io::Result<()> { diff --git a/src/ui.rs b/src/ui.rs index bdac4a7..a90d1df 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -135,7 +135,7 @@ impl Widget for &Thok { for ts in &self.wpm_coords { if ts.1 > highest_wpm { - highest_wpm = ts.1 as f64; + highest_wpm = ts.1; } } @@ -178,11 +178,25 @@ impl Widget for &Thok { chart.render(chunks[0], buf); + let bad_death = self.death_mode + && self + .input + .clone() + .into_iter() + .any(|i| i.outcome == Outcome::Incorrect); + let stats = Paragraph::new(Span::styled( - format!( - "{} wpm {}% acc {:.2} sd", - self.wpm, self.accuracy, self.std_dev - ), + if bad_death { + format!( + "💀 {} wpm {}% acc {:.2} sd 💀", + self.wpm, self.accuracy, self.std_dev + ) + } else { + format!( + "{} wpm {}% acc {:.2} sd", + self.wpm, self.accuracy, self.std_dev + ) + }, bold_style, )) .alignment(Alignment::Center); From 731a4cab8175347478ff5c092022d30876bfb518 Mon Sep 17 00:00:00 2001 From: Yauheni Saihak Date: Fri, 13 Jan 2023 15:34:19 +0300 Subject: [PATCH 3/3] show only an emoji on loose at death mode --- src/ui.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index a90d1df..8803a9a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -187,10 +187,7 @@ impl Widget for &Thok { let stats = Paragraph::new(Span::styled( if bad_death { - format!( - "💀 {} wpm {}% acc {:.2} sd 💀", - self.wpm, self.accuracy, self.std_dev - ) + format!("💀") } else { format!( "{} wpm {}% acc {:.2} sd",