From fd6e49f6d282ac021b8c99f40f80d18b2f6594e1 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 14 Mar 2025 11:46:49 -0500 Subject: [PATCH] Starts work on a progress bar --- Cargo.toml | 2 + src/main.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 03a5591..0a3f1df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ clap = { version = "4.5.31", features = ["derive"] } console = "0.15.11" indicatif = "0.17.11" regex = "1.11.1" +serde = { version = "1.0.219", features = ["derive"] } +toml = "0.8.20" xshell = "0.2.6" [workspace.package] diff --git a/src/main.rs b/src/main.rs index 02fe132..c6a5913 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,9 @@ // TODO: Testing and helper structs/methods // TODO: Better error handling use console::style; -use indicatif::ProgressBar; +use indicatif::{ProgressBar, ProgressState, ProgressStyle}; +use serde::{Deserialize, Serialize}; +use std::io::Read; use std::time::{Duration, Instant}; use std::{fs::File, io::Write, process::Command}; @@ -11,6 +13,64 @@ use clap::Parser; use regex::Regex; use xshell::Shell; +#[derive(Serialize, Deserialize, Default)] +/// Informations on the last run +/// +/// The informations are "split" between non-skip and skip instead of a +/// hash/identifier list storing many different kind of profiles +/// +/// A more thorough information struct would likely be useful however +/// this should the trick for now +struct LastRuns { + /// Duration of the last run with full clean + last_run_clean: Option, + /// Duration of the last run without a full clean + last_run_incremental: Option, +} + +#[derive(Serialize, Deserialize)] +/// The stats of a run, useful to print more helpful progress info and debug infos +struct RunStats { + /// Duration of the last run without a full clean + duration_skip: Duration, + /// Status of the last run + run_status: RunStatus, +} + +/// Current run info, including the last run to save +struct CurrentRunStats { + /// The last run information, to be updated and saved again + last_runs_save: LastRuns, + /// Current run type + run_type: RunType, + /// The informations on the current run + current_run: RunStats +} + +#[derive(Serialize, Deserialize)] +/// A (very) simple run status +enum RunStatus { + /// The last run was a succes + Succes, + /// The tests failed to run + Failure, +} + +enum RunType{ + Incremental, + Clean, +} + +impl CurrentRunStats{ + /// Generate a new run with the current context + fn from_last_run_with_context(){} + + /// Update the last run to save it + fn into_save_runs(){} + + /// Get the last run of the same type + fn get_last_run(){} +} #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct CLIArgs { @@ -75,6 +135,8 @@ struct CLIArgs { fn main() -> anyhow::Result<()> { let args = CLIArgs::parse(); + let last_run_status = load_status(&args); + // let current_run_status= CurrentRun::from(&last_run_status); let new_progress_bar = || { ProgressBar::new_spinner() // .with_style( @@ -96,16 +158,44 @@ fn main() -> anyhow::Result<()> { match (args.no_coverage, args.no_test) { (true, true) => (), (true, false) => { - unimplemented!("Please run with tarpaulin") + unimplemented!("Please run with tarpaulin for now") // run_tests_only(&sh, pb_model.clone(), &args)?; } (false, true) => run_coverage(&sh, new_progress_bar(), &args)?, // The coverage know wether to write the badge test with the cli args (false, false) => run_coverage(&sh, new_progress_bar(), &args)?, } + if let Err(e) = save_status(&last_run_status) { + println!("Couldn't save the run configuration: {e}"); + } Ok(()) } +/// Load the run status from disk +fn load_status(args: &CLIArgs) -> LastRuns { + let _ = args; // Always try to load from disk + if let Ok(mut f) = File::open(".run_status.toml") { + let mut content = String::new(); + if f.read_to_string(&mut content).is_ok() { + if let Ok(deserde) = toml::from_str(&content) { + return deserde; + } + } + } + LastRuns::default() +} + +/// Save the status on disk +fn save_status(status: &LastRuns) -> anyhow::Result<()> { + let mut f = File::create(".run_status.toml")?; + f.write_all( + toml::to_string_pretty(status) + .context("Serializing Last Run info")? + .as_bytes(), + ) + .context("Writing config to disk") +} + /// Run the coverage functions // FORMAT: Compiling strsim v0.11.1 // FORMAT: Finished `test` profile [unoptimized + debuginfo] target(s) in 6.87s @@ -120,6 +210,23 @@ fn run_coverage(_sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyh style("[Coverage]").green().bold() )); pb.enable_steady_tick(Duration::from_millis(100)); + let last_duration = Duration::from_secs(2).as_secs_f64(); + let personnal_progress_bar = ProgressBar::new(20); // Update over lines parsed + personnal_progress_bar + .set_style(ProgressStyle::with_template( + "{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] (lines: {pos}/{len}) (speed: {per_sec}) (eta: {eta})", + ) + .unwrap().with_key("eta", + move |state: &ProgressState, w: &mut dyn std::fmt::Write| { + write!( + w, + "{:.1}s", + (last_duration - state.elapsed().as_secs_f64()).max(0.) + ) + .unwrap() + + })); + personnal_progress_bar.enable_steady_tick(Duration::from_millis(100)); // TODO: Update the status with basic parsing from the output (compiling (), linking (), running tests (), etc..) // cmd!(sh, "cargo tarpaulin").quiet().ignore_stdout().run()?;