From: Ian Jackson Date: Sun, 15 May 2022 23:57:02 +0000 (+0100) Subject: otter cli: Use new Nest progress during bundle uploading X-Git-Tag: otter-1.1.0~89 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=be0bd38034c19a0a806b643de67dfe0ff7501975;p=otter.git otter cli: Use new Nest progress during bundle uploading This lets us show progress of zipfile preparation. Signed-off-by: Ian Jackson --- diff --git a/cli/forgame.rs b/cli/forgame.rs index 3aaea0b6..7153417f 100644 --- a/cli/forgame.rs +++ b/cli/forgame.rs @@ -89,8 +89,13 @@ mod reset_game { } if args.bundles_only || args.bundles.len() != 0 { + let n_bundles = args.bundles.len(); + let progress = ma.progressbar()?; + let mut progress = termprogress::Nest::with_total + (n_bundles as f32, progress); + let local = args.bundles.into_iter().map(|file| { - BundleForUpload::prepare(file) + BundleForUpload::prepare(file, &mut progress) }).collect::,_>>()?; let resp = chan.cmd(&MgmtCommand::ListBundles { game: ma.instance() })?; @@ -142,11 +147,8 @@ mod reset_game { if bundles_only { clear_game(&ma, &mut chan)?; } - let progress = ma.progressbar()?; - let n_bundles = local.len(); - let mut progress = termprogress::Nest::new(progress); for (i, bundle) in local.into_iter().enumerate() { - progress.start_phase((i as f32)/(n_bundles as f32), + progress.start_phase(PROGFRAC_UPLOAD, format!("{}/{}", i, n_bundles)); bundle.upload(&ma, &mut chan, &mut progress)?; } diff --git a/cli/usebundles.rs b/cli/usebundles.rs index 83b70367..0f61bd76 100644 --- a/cli/usebundles.rs +++ b/cli/usebundles.rs @@ -6,6 +6,10 @@ use super::*; //---------- upload-bundle ---------- +pub const PROGFRAC_PREP: f32 = 0.20; +pub const PROGFRAC_HASH: f32 = 0.10; +pub const PROGFRAC_UPLOAD: f32 = 0.70; + #[derive(Debug)] pub struct BundleForUpload { pub file: String, @@ -17,7 +21,9 @@ pub struct BundleForUpload { impl BundleForUpload { #[throws(AE)] - pub fn prepare(file: String) -> Self { + pub fn prepare(file: String, progress: &mut termprogress::Nest) -> Self { + progress.start_phase(PROGFRAC_PREP, default()); + let mut walk = WalkDir::new(&file) .same_file_system(true) .follow_links(true) @@ -34,14 +40,17 @@ impl BundleForUpload { }; if ! peek.file_type().is_dir() { let f = File::open(&file).context("open")?; - Self::prepare_open_file(&file, f)? + Self::prepare_open_file(&file, progress, f)? } else { - Self::prepare_from_dir(&file, walk)? + Self::prepare_from_dir(&file, progress, walk)? } } #[throws(AE)] - pub fn prepare_open_file(file: &str, f: File) -> Self { + pub fn prepare_open_file(file: &str, progress: &mut termprogress::Nest, + f: File) -> Self { + progress.start_phase(PROGFRAC_HASH, default()); + let size = f .metadata().context("fstat bundle file")? .len() @@ -75,7 +84,8 @@ impl BundleForUpload { } #[throws(AE)] - pub fn prepare_from_dir(dir: &str, walk: W) -> Self + pub fn prepare_from_dir(dir: &str, progress: &mut termprogress::Nest, + walk: W) -> Self where W: Iterator> { let zipfile = tempfile::tempfile().context("create tmp zipfile")?; @@ -129,7 +139,7 @@ impl BundleForUpload { .map_err(|e| e.into_error()).context("flush zipfile")?; zipfile.rewind().context("rewind zipfile")?; - Self::prepare_open_file(dir, zipfile)? + Self::prepare_open_file(dir,progress, zipfile)? } } @@ -153,9 +163,12 @@ mod upload_bundle { fn call(SCCA{ mut out, ma, args,.. }:SCCA) { let args = parse_args::(args, &subargs, &ok_id, None); let mut chan = ma.access_game()?; - let mut progress = ma.progressbar()?; - let for_upload = BundleForUpload::prepare(args.bundle_file)?; - let bundle = for_upload.upload(&ma, &mut chan, &mut *progress)?; + let progress = ma.progressbar()?; + let mut progress = termprogress::Nest::new(progress); + + let for_upload = BundleForUpload::prepare(args.bundle_file, + &mut progress)?; + let bundle = for_upload.upload(&ma, &mut chan, &mut progress)?; writeln!(out, "{}", bundle)?; } diff --git a/support/termprogress.rs b/support/termprogress.rs index 5082cd44..c618b468 100644 --- a/support/termprogress.rs +++ b/support/termprogress.rs @@ -162,6 +162,7 @@ impl Drop for TermReporter { } pub struct Nest { + outer_total: f32, outer_phase_base: f32, outer_phase_size: f32, desc_prefix: String, @@ -169,10 +170,19 @@ pub struct Nest { } impl Nest { - /// Assumes that every inner phase is of the same length as the first pub fn new(actual_reporter: Box) -> Self { Nest { actual_reporter, + outer_total: 1., + outer_phase_base: 0., + outer_phase_size: 0., + desc_prefix: default(), + } } + + pub fn with_total(outer_total: f32, actual_reporter: Box) + -> Self { Nest { + actual_reporter, + outer_total, outer_phase_base: 0., outer_phase_size: 0., desc_prefix: default(), @@ -193,8 +203,8 @@ impl Reporter for Nest { fn report(&mut self, inner_pi: &ProgressInfo<'_>) { let inner_frac = inner_pi.phase.value.fraction(); let outer_frac = - self.outer_phase_size * inner_frac + - self.outer_phase_base; + (self.outer_phase_size * inner_frac + + self.outer_phase_base) / self.outer_total; let desc = if self.desc_prefix != "" { format!("{} {}", &self.desc_prefix, inner_pi.phase.desc).into()