Skip to content

Commit

Permalink
support configurable color scheme (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kritikash18 authored Oct 7, 2023
1 parent d5955dd commit 35e89a8
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 15 deletions.
71 changes: 69 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
tempfile = "3.8.0"
tui = "0.19.0"
ratatui = "0.23"

[dev-dependencies]
assert_cmd = "2.0.12"
assert_cmd = "2.0.12"
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ pub struct Args {
/// uppercase-ratio argument
#[arg(long)]
pub uppercase_ratio: Option<f64>,

}
21 changes: 21 additions & 0 deletions src/color_scheme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use ratatui::style::Color;

#[derive(Debug)]
pub struct ColorScheme {
pub correct_match_fg: Color,
pub correct_match_bg: Color,
pub incorrect_match_fg: Color,
pub incorrect_match_bg: Color,
}

impl ColorScheme {

pub fn default() -> Self {
Self {
correct_match_fg: Color::Green,
correct_match_bg: Color::Black,
incorrect_match_fg: Color::Gray,
incorrect_match_bg: Color::Red,
}
}
}
39 changes: 35 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! | `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 |
//!
//!
//! `NOTE: If provided numbers_ratio is not between 0 to 1.0, Default numbers_ratio = 0.05 will be used.`
//!
//!
Expand Down Expand Up @@ -36,6 +36,8 @@ use mockall::*;
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 @@ -47,6 +49,7 @@ pub struct Config {
pub dictionary_path: PathBuf,
pub uppercase: bool,
pub uppercase_ratio: f64,
pub color_config: ColorScheme,
}

/// Used by `serde` crate to parse config file into a rust struct
Expand All @@ -57,7 +60,16 @@ struct ConfigFile {
pub numbers_ratio: Option<f64>,
pub dictionary_path: Option<String>,
pub uppercase: Option<bool>,
pub uppercase_ratio: Option<f64>
pub uppercase_ratio: Option<f64>,
pub color_config: Option<ConfigFileColorScheme>,
}

#[derive(Deserialize, Serialize, Debug)]
struct ConfigFileColorScheme {
pub correct_match_fg: Option<String>,
pub correct_match_bg: Option<String>,
pub incorrect_match_fg: Option<String>,
pub incorrect_match_bg: Option<String>,
}

#[automock]
Expand All @@ -70,7 +82,8 @@ impl Config {
numbers_ratio: 0.05,
dictionary_path: PathBuf::from("src/dict/words.txt"),
uppercase: false,
uppercase_ratio: 0.45
uppercase_ratio: 0.45,
color_config: ColorScheme::default(),
}
}

Expand Down Expand Up @@ -136,6 +149,24 @@ fn augment_config_with_config_file(config: &mut Config, mut config_file: fs::Fil
config.uppercase_ratio = uppercase_ratio
}
}

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(correct_match_bg) = color_config.correct_match_bg {
config.color_config.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_bg) = color_config.incorrect_match_bg {
config.color_config.incorrect_match_bg = incorrect_match_bg.parse().unwrap();
}
}
}

Ok(())
Expand All @@ -159,7 +190,7 @@ fn augment_config_with_args(config: &mut Config, args: Args) {
if numbers_ratio >= 0.0 && numbers_ratio <= 1.0 {
config.numbers_ratio = numbers_ratio
}
}
}
if let Some(duration) = args.duration {
config.duration = Duration::from_secs(duration);
}
Expand Down
6 changes: 4 additions & 2 deletions src/expected_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn create_uppercase_words (string_vec: &mut Vec<String>, pos: usize, uppercase_r
v[0] = v[0].to_uppercase().nth(0).unwrap();
let s: String = v.into_iter().collect();
string_vec[i] = s;
}
}
}
}

Expand All @@ -125,6 +125,7 @@ impl ExpectedInputInterface for ExpectedInput {
#[cfg(test)]
mod tests {
use std::{io::Write, time::Duration};
use crate::color_scheme::ColorScheme;

use super::*;

Expand All @@ -148,7 +149,8 @@ mod tests {
numbers_ratio: 0.05,
dictionary_path: config_file.path().to_path_buf(),
uppercase: false,
uppercase_ratio: 0.45
uppercase_ratio: 0.45,
color_config: ColorScheme::default(),
};

let expected_input = ExpectedInput::new(&config).expect("unable to create expected input");
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

mod args;
mod config;
mod color_scheme;
mod expected_input;
mod runner;

Expand All @@ -63,7 +64,7 @@ use crossterm::{
},
};
use std::io;
use tui::{backend::CrosstermBackend, Terminal};
use ratatui::{backend::CrosstermBackend, Terminal};

use args::Args;
use config::Config;
Expand Down Expand Up @@ -170,7 +171,7 @@ mod tests {

use anyhow::{Context, Result};
use predicates::Predicate;
use tui::{backend::TestBackend, buffer::Buffer, Frame, Terminal};
use ratatui::{backend::TestBackend, buffer::Buffer, Frame, Terminal};

use crate::{
args::Args,
Expand Down
7 changes: 4 additions & 3 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use anyhow::{Context, Result};
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use mockall::automock;
use std::time::{Duration, Instant};
use tui::{

use ratatui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style},
Expand Down Expand Up @@ -307,8 +308,8 @@ impl Runner {
{
let input = Paragraph::new(expected_input_char.to_string()).style(
match input_char == expected_input_char {
true => Style::default().fg(Color::Green),
false => Style::default().bg(Color::Red).fg(Color::Gray),
true => Style::default().bg(self.config.color_config.correct_match_bg).fg(self.config.color_config.correct_match_fg),
false => Style::default().bg(self.config.color_config.incorrect_match_bg).fg(self.config.color_config.incorrect_match_fg),
},
);
frame.render_widget(
Expand Down

0 comments on commit 35e89a8

Please sign in to comment.