Skip to content

Commit

Permalink
feat: add subcommand for showing history of results
Browse files Browse the repository at this point in the history
  • Loading branch information
radlinskii committed Oct 17, 2023
1 parent c9f5f61 commit c4d75ed
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 132 deletions.
17 changes: 17 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ pub struct Args {
/// indicates if test results should be saved
#[arg(long)]
pub save_results: Option<bool>,

/// Add subcommands here
#[command(subcommand)]
pub history: Option<SubCommand>,
}

#[derive(Parser, Debug, Clone)]
pub enum SubCommand {
#[command(about = "Show previous test results in a bar chart.")]
History(HistorySubcommandArgs),
}

#[derive(Parser, Debug, Clone)]
pub struct HistorySubcommandArgs {
// Define subcommand-specific arguments here
// #[arg(short, long)]
// pub show_date: Option<bool>,
}
73 changes: 41 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ use crossterm::{
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use test_results::{read_previous_results, render_results};

use args::Args;
use config::Config;
Expand All @@ -99,51 +100,57 @@ use runner::Runner;
/// - restores terminal configuration
/// - if test was completed, prints the results and saves them.
fn main() -> anyhow::Result<()> {
let config_file_path = dirs::home_dir()
.context("Unable to get home directory")?
.join(".config")
.join("donkeytype")
.join("donkeytype-config.json");

let args = Args::parse();
let config = Config::new(args, config_file_path).context("Unable to create config")?;
let expected_input = ExpectedInput::new(&config).context("Unable to create expected input")?;

let mut terminal = configure_terminal().context("Unable to configure terminal")?;

let mut app = Runner::new(config, expected_input);
let res = app.run(&mut terminal);
let res = match &args.history {
Some(_) => {
let records = read_previous_results().context("Unable to read history results")?;
render_results(&mut terminal, &records).context("Unable to render history results")?;
restore_terminal(&mut terminal).context("Unable to restore terminal")?;
Ok(())
}
None => {
let config_file_path = dirs::home_dir()
.context("Unable to get home directory")?
.join(".config")
.join("donkeytype")
.join("donkeytype-config.json");
let config = Config::new(args, config_file_path).context("Unable to create config")?;
let expected_input =
ExpectedInput::new(&config).context("Unable to create expected input")?;

let mut app = Runner::new(config, expected_input);
let test_results = app
.run(&mut terminal)
.context("Error while running the test")?;

match res {
Ok(test_results) => {
if test_results.completed {
if let Err(err) = test_results.render_results(&mut terminal) {
eprintln!("{:?}", err);

restore_terminal(terminal).context("Unable to restore terminal")?;
return Err(err);
}
test_results
.render(&mut terminal)
.context("Unable to render test results")?;
if test_results.save {
if let Err(err) = test_results.save_to_file() {
eprintln!("{:?}", err);

restore_terminal(terminal).context("Unable to restore terminal")?;
return Err(err);
}
test_results
.save_to_file()
.context("Unable to save results to file")?;
}
restore_terminal(&mut terminal).context("Unable to restore terminal")?;
} else {
restore_terminal(&mut terminal).context("Unable to restore terminal")?;
println!("Test not finished.");
}

restore_terminal(terminal).context("Unable to restore terminal")?;
Ok(())
}
Err(err) => {
println!("Error: {:?}", err);

restore_terminal(terminal).context("Unable to restore terminal")?;
};

Err(err)
match res {
Err(err) => {
restore_terminal(&mut terminal).context("Unable to restore terminal")?;
eprintln!("{:?}", err);
return Err(err);
}
Ok(_) => Ok(()),
}
}

Expand Down Expand Up @@ -171,7 +178,7 @@ fn configure_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>, anyhow

/// restores terminal window configuration
fn restore_terminal(
mut terminal: Terminal<CrosstermBackend<io::Stdout>>,
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<(), anyhow::Error> {
disable_raw_mode().context("Unable to disable raw mode")?;
if matches!(supports_keyboard_enhancement(), Ok(true)) {
Expand Down Expand Up @@ -253,6 +260,7 @@ mod tests {
uppercase_ratio: None,
numbers_ratio: None,
save_results: None,
history: None,
};

let (config, expected_input, mut terminal) = setup_terminal(args)?;
Expand Down Expand Up @@ -286,6 +294,7 @@ mod tests {
numbers: None,
numbers_ratio: None,
save_results: None,
history: None,
};

let (config, expected_input, mut terminal) = setup_terminal(args)?;
Expand Down
Loading

0 comments on commit c4d75ed

Please sign in to comment.