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

death mode #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
}
Expand All @@ -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,
);
}
}
Expand Down
22 changes: 19 additions & 3 deletions src/thok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ pub struct Thok {
pub seconds_remaining: Option<f64>,
pub number_of_secs: Option<f64>,
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<f64>) -> Self {
pub fn new(
prompt: String,
number_of_words: usize,
number_of_secs: Option<f64>,
death_mode: bool,
) -> Self {
Self {
prompt,
input: vec![],
Expand All @@ -48,6 +54,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,
Expand Down Expand Up @@ -189,8 +196,17 @@ 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 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_prompt || out_of_time || is_fatal_error
}

pub fn save_results(&self) -> io::Result<()> {
Expand Down
21 changes: 16 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -178,11 +178,22 @@ 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!("💀")
} else {
format!(
"{} wpm {}% acc {:.2} sd",
self.wpm, self.accuracy, self.std_dev
)
},
bold_style,
))
.alignment(Alignment::Center);
Expand Down