diff --git a/src/main.rs b/src/main.rs index 6501435..6e37eae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,6 @@ enum Unit { /// Measure current with Otii Arc measurement tool #[derive(StructOpt)] struct Cli { - /// Give up measurement after this many minutes #[structopt(long, default_value = "20")] timeout_min: u32, @@ -39,7 +38,7 @@ struct Cli { /// Measure for this many seconds #[structopt(name = "measurement-time", long, default_value = "1")] measurement_time_sec: u32, - + /// Wait this many seconds until measurement starts #[structopt(name = "wait", long, default_value = "0")] wait_to_start_sec: u32, @@ -82,6 +81,10 @@ struct Cli { /// GPI2 needs to be true before measurement #[structopt(long = "wait-for-GPI2")] wait_for_gpi2: Option, + + /// Don't show a progress bar + #[structopt(long = "quiet")] + quiet: bool, } pub trait Tracer { @@ -271,13 +274,18 @@ impl Otii { // TODO Rewrite to use closure for the actual parsing to remove // code duplication with wait_for_gpi - fn get_average_current(&mut self, max_samples: u32) -> f64 { + fn get_average_current(&mut self, max_samples: u32, quiet: bool) -> f64 { let mut samples = 1; let mut last_sample = 0; let mut total_value = 0.0; let mut nr_values = 0; - let pb = indicatif::ProgressBar::new(u64::from(max_samples)); - pb.set_style(indicatif::ProgressStyle::default_bar().progress_chars("█▉▊▋▌▍▎▏ ")); + let pb = if quiet { + None + } else { + let pb = indicatif::ProgressBar::new(u64::from(max_samples)); + pb.set_style(indicatif::ProgressStyle::default_bar().progress_chars("█▉▊▋▌▍▎▏ ")); + Some(pb) + }; loop { if let Some(line) = self.read_line() { let mut data = line.trim().split(':'); @@ -313,17 +321,19 @@ impl Otii { }; nr_values += tmp_nrs; samples += 1; - pb.inc(1); - - pb.set_message(format!( - "{}/{}s", - indicatif::HumanDuration(std::time::Duration::from_secs(u64::from( - samples / SAMPLES_PER_SECOND - ))), - indicatif::HumanDuration(std::time::Duration::from_secs(u64::from( - max_samples - ))) - )); + if let Some(ref pb) = &pb { + pb.inc(1); + + pb.set_message(format!( + "{}/{}s", + indicatif::HumanDuration(std::time::Duration::from_secs(u64::from( + samples / SAMPLES_PER_SECOND + ))), + indicatif::HumanDuration(std::time::Duration::from_secs(u64::from( + max_samples + ))) + )); + } if samples > max_samples { break; @@ -331,7 +341,9 @@ impl Otii { } } } - pb.finish(); + if let Some(pb) = pb { + pb.finish(); + } total_value / f64::from(nr_values) } @@ -419,11 +431,13 @@ fn main() { // Handle timeout: let timer = timer::Timer::new(); // If _guard is dropped, the timer is cancelled - let _guard = - timer.schedule_with_delay(chrono::Duration::minutes(i64::from(args.timeout_min)), || { + let _guard = timer.schedule_with_delay( + chrono::Duration::minutes(i64::from(args.timeout_min)), + || { write_to_user("Measurement took too long time, aborting..."); std::process::exit(1); - }); + }, + ); let tracer = get_tracer(args.debug_filename); @@ -442,14 +456,15 @@ fn main() { otii.wait_for_gpi2(value); } // Flush some measurement. - otii.get_average_current(500 + args.wait_to_start_sec * 1000); + otii.get_average_current(500 + args.wait_to_start_sec * 1000, true); write_to_user(&format!( "Starting measurement for {} seconds", args.measurement_time_sec )); let start_measurement_time = std::time::Instant::now(); - let avg = otii.get_average_current(args.measurement_time_sec * SAMPLES_PER_SECOND) / mc_gain as f64; + let avg = otii.get_average_current(args.measurement_time_sec * SAMPLES_PER_SECOND, args.quiet) + / mc_gain as f64; write_to_user(&format!( "Done. Measurement took {} seconds", start_measurement_time.elapsed().as_secs()