Skip to content

Commit

Permalink
add default dictionary to compiled binary (#31)
Browse files Browse the repository at this point in the history
* put default dictionary into rust codebase

* fix tests

* remove comment

* update docs
  • Loading branch information
radlinskii authored Oct 19, 2023
1 parent 0a0c4f1 commit af29592
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 48 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name = "donkeytype"
version = "0.1.0"
edition = "2021"
rust-version = "1.70"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ a _very_ minimalistic _cli_ typing test.

![gif demonstraiting how the program works](https://github.com/radlinskii/donkeytype/assets/26116041/4c2a1b6d-e70e-4631-8438-9259cc780a36)


## How it works

When the program is run you will see the expected input displayed at the top of your terminal window.
Expand All @@ -31,7 +30,6 @@ You can clone the repo, and run the main program with default configuration usin
cargo run
```


To view the history of results in a bar chart you can run:

```shell
Expand All @@ -40,7 +38,6 @@ cargo run -- history

<img width="1426" alt="picture demonstraiting bar chart with history data" src="https://github.com/radlinskii/donkeytype/assets/26116041/352c68fc-28a3-4ea2-8800-d74b8d759ddd">


To see all available options run:

```shell
Expand All @@ -58,14 +55,16 @@ 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 |
| `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 |
| 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.15` | boolean | ratio for putting uppercase letters in test |
| `dictionary_path` | `None` (builtin dictionary) | string | path to file with dictionary words to sample from while creating test's expected input |
| `save_results` | `true` | boolean | flag indicating if results should be saved to a file |
| `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.
> Same happens with `uppercase_ratio`.
Expand Down
31 changes: 17 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//!
//! 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 |
//! | `uppercase` | `false` | boolean | flag indicating if uppercase letters should be inserted in expected input |
//! | `uppercase_ratio` | `0.15` | 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 |
//! | `save_results` | `true` | boolean | flag indicating if results should be saved to a file |
//! | 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.15` | boolean | ratio for putting uppercase letters in test |
//! | `dictionary_path` | `None` (builtin dictionary) | string | path to file with dictionary words to sample from while creating test's expected input |
//! | `save_results` | `true` | boolean | flag indicating if results should be saved to a file |
//!
//! NOTE: If provided `numbers_ratio` is not between `0` to `1.0`, Default `numbers_ratio = 0.05` will be used.
//! NOTE: If provided `uppercase_ratio` is not between `0` to `1.0`, Default `numbers_ratio = 0.15` will be used.
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Config {
pub duration: Duration,
pub numbers: bool,
pub numbers_ratio: f64,
pub dictionary_path: PathBuf,
pub dictionary_path: Option<PathBuf>,
pub uppercase: bool,
pub uppercase_ratio: f64,
pub colors: ColorScheme,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Config {
duration: Duration::from_secs(30),
numbers: false,
numbers_ratio: 0.05,
dictionary_path: PathBuf::from("src/dict/words.txt"),
dictionary_path: None,
uppercase: false,
uppercase_ratio: 0.15,
colors: ColorScheme::default(),
Expand Down Expand Up @@ -159,7 +159,7 @@ fn augment_config_with_config_file(config: &mut Config, mut config_file: fs::Fil
}

if let Some(dictionary_path) = config_from_file.dictionary_path {
config.dictionary_path = PathBuf::from(dictionary_path);
config.dictionary_path = Some(PathBuf::from(dictionary_path));
}

if let Some(uppercase) = config_from_file.uppercase {
Expand Down Expand Up @@ -221,7 +221,7 @@ fn augment_config_with_args(config: &mut Config, args: Args) {
config.duration = Duration::from_secs(duration);
}
if let Some(dictionary_path) = args.dictionary_path {
config.dictionary_path = PathBuf::from(dictionary_path);
config.dictionary_path = Some(PathBuf::from(dictionary_path));
}
if let Some(uppercase_flag) = args.uppercase {
config.uppercase = uppercase_flag
Expand Down Expand Up @@ -338,7 +338,10 @@ mod tests {
assert_eq!(config.duration, Duration::from_secs(20));
assert_eq!(config.numbers, false);
assert_eq!(config.numbers_ratio, 0.05);
assert_eq!(config.dictionary_path, PathBuf::from("/etc/dict/words"));
assert_eq!(
config.dictionary_path,
Some(PathBuf::from("/etc/dict/words"))
);
assert_eq!(config.save_results, true);
}
}
6 changes: 4 additions & 2 deletions src/dict/words.txt → src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
the
//! module with words used to generate expected input
pub const WORDS: &str = "the
of
to
and
Expand Down Expand Up @@ -997,4 +999,4 @@ meant
quotient
teeth
shell
neck
neck";
16 changes: 10 additions & 6 deletions src/expected_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rand::{seq::SliceRandom, thread_rng, Rng};
use std::io::Read;

use crate::config::Config;
use crate::dictionary;
use crate::helpers::split_by_char_index;

/// Struct used by runner to hold generate the text used for validation and as a placeholder
Expand All @@ -28,11 +29,14 @@ impl ExpectedInput {
/// then replace some words with numbers if specified in config
/// then save one long string to memory
pub fn new(config: &Config) -> Result<Self, anyhow::Error> {
let mut file = std::fs::File::open(config.dictionary_path.clone())
.context("Unable to open dictionary file")?;
let mut str = String::new();
file.read_to_string(&mut str)
.context("Unable to read dictionary file")?;
let mut str = dictionary::WORDS.to_string();
if let Some(dictionary_path) = &config.dictionary_path {
str = String::from("");
let mut file =
std::fs::File::open(dictionary_path).context("Unable to open dictionary file")?;
file.read_to_string(&mut str)
.context("Unable to read dictionary file")?;
}

let mut rng = thread_rng();
let mut str_vec = str.split("\n").collect::<Vec<&str>>();
Expand Down Expand Up @@ -158,7 +162,7 @@ mod tests {
duration: Duration::from_secs(30),
numbers: false,
numbers_ratio: 0.05,
dictionary_path: config_file.path().to_path_buf(),
dictionary_path: Some(config_file.path().to_path_buf()),
uppercase: false,
uppercase_ratio: 0.45,
colors: ColorScheme::default(),
Expand Down
19 changes: 10 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
//!
//! 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 |
//! | `uppercase` | `false` | boolean | flag indicating if uppercase letters should be inserted in expected input |
//! | `uppercase_ratio` | `0.15` | 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 |
//! | `save_results` | `true` | boolean | flag indicating if results should be saved to a file |
//! | 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.15` | boolean | ratio for putting uppercase letters in test |
//! | `dictionary_path` | `None` (builtin dictionary) | string | path to file with dictionary words to sample from while creating test's expected input |
//! | `save_results` | `true` | boolean | flag indicating if results should be saved to a file |
//!
//! NOTE: If provided `numbers_ratio` is not between `0` to `1.0`, Default `numbers_ratio = 0.05` will be used.
//! NOTE: If provided `uppercase_ratio` is not between `0` to `1.0`, Default `numbers_ratio = 0.15` will be used.
Expand Down Expand Up @@ -69,6 +69,7 @@
mod args;
mod color_scheme;
mod config;
mod dictionary;
mod expected_input;
mod helpers;
mod runner;
Expand Down
15 changes: 10 additions & 5 deletions src/test_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ impl Stats {
impl TestResults {
/// creates TestResults object from Stats and Config
pub fn new(stats: Stats, config: Config, completed: bool) -> Self {
fn get_dictionary_path(dictionary_path: Option<PathBuf>) -> Option<String> {
if let Some(path) = dictionary_path {
if let Some(str) = path.to_str() {
return Some(str.to_string());
}
}
Some("default_dictionary".to_string())
}

TestResults {
local_datetime: Local::now(),
// stats
Expand All @@ -100,11 +109,7 @@ impl TestResults {
duration: Some(config.duration.as_secs()),
numbers: Some(config.numbers),
numbers_ratio: Some(config.numbers_ratio),
dictionary_path: if let Some(str) = config.dictionary_path.to_str() {
Some(str.to_string())
} else {
None
},
dictionary_path: get_dictionary_path(config.dictionary_path),
uppercase: Some(config.uppercase),
uppercase_ratio: Some(config.uppercase_ratio),

Expand Down

0 comments on commit af29592

Please sign in to comment.