Skip to content

Commit

Permalink
docs: update new options docs (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
radlinskii authored Oct 8, 2023
1 parent a6be657 commit 8edc6f4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 52 deletions.
51 changes: 34 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ a _very_ minimalistic _cli_ typing test.

## How it works

When run the program you will see the expected input displayed at the top of your terminal window.
When the program is run you will see the expected input displayed at the top of your terminal window.
This text is a placeholder, and this is the input that you should write when the test is started.
Now you should write this text as fast as you can.
If you make a mistake you can press `backspace` to delete a single character,
or press `backspace` while holding `Option`/`Ctrl` to delete a whole word.

On the bottom-right corner there is a help message saying that to start the test you need to press `'e'` (enter the test) or leave by pressing `'q'`
On the bottom-right corner is a help message saying that to start the test you need to press `'e'` (enter the test) or leave by pressing `'q'`
When test is running you can see how much time you have left in bottom-left corner.

You can pause the test by pressing <Esc>, to resume it press `'e'` again.
Expand Down Expand Up @@ -38,36 +41,50 @@ Configuration will grow when more features are added (_different modes_, _differ

Default config looks like this:

| name | default value | type in JSON | description |
| ----------------- | ---------------------- | ------------ | -------------------------------------------------------------------- |
| `duration` | `30` | number | duration of the test in seconds |
| `numbers` | `false` | boolean | flag indicating if numbers should be inserted in expected input |
| `numbers_ratio` | `0.05` if numbers=TRUE | number | ratio for putting numbers in the test |
| `dictionary_path` | `"src/dict/words.txt"` | string | dictionary words to sample from while creating test's expected input |
| name | default value | type in JSON | description |
| ----------------- | ---------------------- | ------------ | ------------------------------------------------------------------------- |
| `duration` | `30` | number | duration of the test in seconds |
| `numbers` | `false` | boolean | flag indicating if numbers should be inserted in expected input |
| `numbers_ratio` | `0.05` if numbers=TRUE | number | ratio for putting numbers in the test |
| `uppercase` | `false` | boolean | flag indicating if uppercase letters should be inserted in expected input |
| `uppercase_ratio` | `0.25` | boolean | ratio for putting uppercase letters in test |
| `dictionary_path` | `"src/dict/words.txt"` | string | dictionary words to sample from while creating test's expected input |

`NOTE: If provided numbers_ratio is not between 0 to 1.0, Default numbers_ratio = 0.05 will be used.`
> NOTE: If provided `numbers_ratio` is not between `0` to `1.0`, Default `numbers_ratio = 0.05` will be used.
> Same happens with `uppercase_ratio`.
You can provide this config as options when running the program like so:

```shell
cargo run -- --duration 60 --dictionary-path "/usr/share/dict/words" --numbers true
cargo run -- --duration 60 --numbers true --uppercase true
```

or put them in a config file in `~/.config/donkeytype/donkeytype-config.json`:
To get all the available options run

```shell
cargo run -- --help
```

You can also put all the options inside config file in `~/.config/donkeytype/donkeytype-config.json`:

```json
{
"duration": 60,
"dictionary_path": "/usr/share/dict/words",
"numbers": false
"numbers": true,
"numbers_ratio": 0.1,
"uppercase": true,
"uppercase_ratio": 0.3
"colors": {
"correct_match_fg": "green",
"correct_match_bg": "white",
"incorrect_match_fg": "#ff00ff"
"incorrect_match_bg": "#0f000f"
}
}
```

To get all the available options run

```shell
cargo run -- --help
```
> Providing config in a file also supports passing custom color values.
## Development

Expand Down
11 changes: 8 additions & 3 deletions src/color_scheme.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//! Module with ColorScheme struct used to define what colors should be used
//! for different elements in test.
//! Default is `green` foreground for correct characters,
//! and `red` background for incorrect.

use ratatui::style::Color;

/// Struct used in config for defining colors used in test.
#[derive(Debug)]
pub struct ColorScheme {
pub correct_match_fg: Color,
Expand All @@ -9,12 +15,11 @@ pub struct ColorScheme {
}

impl ColorScheme {

pub fn default() -> Self {
Self {
correct_match_fg: Color::Green,
correct_match_bg: Color::Black,
incorrect_match_fg: Color::Gray,
correct_match_bg: Color::Reset,
incorrect_match_fg: Color::Reset,
incorrect_match_bg: Color::Red,
}
}
Expand Down
68 changes: 41 additions & 27 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
//!
//! Default options of configuration are:
//!
//! | name | default value | type in JSON | description |
//! | ----------------- | ---------------------- | ------------ | -------------------------------------------------------------------- |
//! | `duration` | `30` | number | duration of the test in seconds |
//! | `numbers` | `false` | boolean | flag indicating if numbers should be inserted in expected input |
//! | `numbers_ratio` | `0.05` if numbers=TRUE | number | ratio for putting numbers in the test |
//! | `dictionary_path` | `"src/dict/words.txt"` | string | dictionary words to sample from while creating test's expected input |
//! | name | default value | type in JSON | description |
//! | ----------------- | ---------------------- | ------------ | ------------------------------------------------------------------------- |
//! | `duration` | `30` | number | duration of the test in seconds |
//! | `numbers` | `false` | boolean | flag indicating if numbers should be inserted in expected input |
//! | `numbers_ratio` | `0.05` if numbers=TRUE | number | ratio for putting numbers in the test |
//! | `uppercase` | `false` | boolean | flag indicating if uppercase letters should be inserted in expected input |
//! | `uppercase_ratio` | `0.25` | boolean | ratio for putting uppercase letters in test |
//! | `dictionary_path` | `"src/dict/words.txt"` | string | dictionary words to sample from while creating test's expected input |
//!
//! `NOTE: If provided numbers_ratio is not between 0 to 1.0, Default numbers_ratio = 0.05 will be used.`
//! NOTE: If provided `numbers_ratio` is not between `0` to `1.0`, Default `numbers_ratio = 0.05` will be used.
//!
//!
//! Configuration will grow when more features are added (_different modes_, _different languages_, _configuring colors_).
//!
//! You can provide this config as options when running the program like so:
//!
//! ```shell
//! cargo run -- --duration 60 --dictionary-path "/usr/share/dict/words" --numbers true --numbers-ratio 0.1
//! cargo run -- --duration 60 --dictionary-path "/usr/share/dict/words" --numbers true
//! --numbers-ratio 0.1 --uppercase true --uppercase-ratio 0.3
//! ```
//!
//! or put them in a config file in `~/.config/donkeytype/donkeytype-config.json`:
Expand All @@ -27,8 +30,19 @@
//! "duration": 60,
//! "dictionary_path": "/usr/share/dict/words",
//! "numbers": true,
//! "numbers_ratio": 0.1
//! "numbers_ratio": 0.1,
//! "uppercase": true,
//! "uppercase_ratio": 0.3
//! "colors": {
//! "correct_match_fg": "green",
//! "correct_match_bg": "white",
//! "incorrect_match_fg": "#ff00ff"
//! "incorrect_match_bg": "#0f000f"
//! }
//! }
//!
//! Providing config in a file also supports passing custom color values.
//!
//! ```

use anyhow::{Context, Result};
Expand All @@ -37,7 +51,6 @@ use serde::{Deserialize, Serialize};
use std::{fs, io::Read, path::PathBuf, time::Duration};

use crate::color_scheme::ColorScheme;

use crate::Args;

/// Main program configuration
Expand All @@ -49,7 +62,7 @@ pub struct Config {
pub dictionary_path: PathBuf,
pub uppercase: bool,
pub uppercase_ratio: f64,
pub color_config: ColorScheme,
pub colors: ColorScheme,
}

/// Used by `serde` crate to parse config file into a rust struct
Expand All @@ -61,9 +74,10 @@ struct ConfigFile {
pub dictionary_path: Option<String>,
pub uppercase: Option<bool>,
pub uppercase_ratio: Option<f64>,
pub color_config: Option<ConfigFileColorScheme>,
pub colors: Option<ConfigFileColorScheme>,
}

/// Struct used be `serde` crate to parse colors config from config file
#[derive(Deserialize, Serialize, Debug)]
struct ConfigFileColorScheme {
pub correct_match_fg: Option<String>,
Expand All @@ -82,8 +96,8 @@ impl Config {
numbers_ratio: 0.05,
dictionary_path: PathBuf::from("src/dict/words.txt"),
uppercase: false,
uppercase_ratio: 0.45,
color_config: ColorScheme::default(),
uppercase_ratio: 0.25,
colors: ColorScheme::default(),
}
}

Expand Down Expand Up @@ -150,21 +164,21 @@ fn augment_config_with_config_file(config: &mut Config, mut config_file: fs::Fil
}
}

if let Some(color_config) = config_from_file.color_config {
if let Some(correct_match_fg) = color_config.correct_match_fg {
config.color_config.correct_match_fg = correct_match_fg.parse().unwrap();
if let Some(colors) = config_from_file.colors {
if let Some(correct_match_fg) = colors.correct_match_fg {
config.colors.correct_match_fg = correct_match_fg.parse().unwrap();
}

if let Some(correct_match_bg) = color_config.correct_match_bg {
config.color_config.correct_match_bg = correct_match_bg.parse().unwrap();
if let Some(correct_match_bg) = colors.correct_match_bg {
config.colors.correct_match_bg = correct_match_bg.parse().unwrap();
}

if let Some(incorrect_match_fg) = color_config.incorrect_match_fg {
config.color_config.incorrect_match_fg = incorrect_match_fg.parse().unwrap();
if let Some(incorrect_match_fg) = colors.incorrect_match_fg {
config.colors.incorrect_match_fg = incorrect_match_fg.parse().unwrap();
}

if let Some(incorrect_match_bg) = color_config.incorrect_match_bg {
config.color_config.incorrect_match_bg = incorrect_match_bg.parse().unwrap();
if let Some(incorrect_match_bg) = colors.incorrect_match_bg {
config.colors.incorrect_match_bg = incorrect_match_bg.parse().unwrap();
}
}
}
Expand Down Expand Up @@ -232,7 +246,7 @@ mod tests {
numbers_ratio: None,
dictionary_path: None,
uppercase: None,
uppercase_ratio: None
uppercase_ratio: None,
};
let config = Config::new(args, PathBuf::new()).expect("Unable to create config");

Expand All @@ -254,7 +268,7 @@ mod tests {
numbers_ratio: None,
dictionary_path: None,
uppercase: None,
uppercase_ratio: None
uppercase_ratio: None,
};
let config =
Config::new(args, config_file.path().to_path_buf()).expect("Unable to create config");
Expand All @@ -272,7 +286,7 @@ mod tests {
numbers_ratio: None,
dictionary_path: None,
uppercase: None,
uppercase_ratio: None
uppercase_ratio: None,
};
let config = Config::new(args, PathBuf::new()).expect("Unable to create config");

Expand All @@ -294,7 +308,7 @@ mod tests {
numbers_ratio: None,
dictionary_path: Some(String::from("/etc/dict/words")),
uppercase: None,
uppercase_ratio: None
uppercase_ratio: None,
};
let config =
Config::new(args, config_file.path().to_path_buf()).expect("Unable to create config");
Expand Down
2 changes: 1 addition & 1 deletion src/expected_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod tests {
dictionary_path: config_file.path().to_path_buf(),
uppercase: false,
uppercase_ratio: 0.45,
color_config: ColorScheme::default(),
colors: ColorScheme::default(),
};

let expected_input = ExpectedInput::new(&config).expect("unable to create expected input");
Expand Down
8 changes: 4 additions & 4 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ impl Runner {
let input = Paragraph::new(expected_input_char.to_string()).style(
match input_char == expected_input_char {
true => Style::default()
.bg(self.config.color_config.correct_match_bg)
.fg(self.config.color_config.correct_match_fg),
.bg(self.config.colors.correct_match_bg)
.fg(self.config.colors.correct_match_fg),
false => Style::default()
.bg(self.config.color_config.incorrect_match_bg)
.fg(self.config.color_config.incorrect_match_fg),
.bg(self.config.colors.incorrect_match_bg)
.fg(self.config.colors.incorrect_match_fg),
},
);
frame.render_widget(
Expand Down

0 comments on commit 8edc6f4

Please sign in to comment.