diff --git a/src/appstate.rs b/src/appstate.rs index 8223df8..26bf7aa 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -58,37 +58,34 @@ impl AppState { } fn with_progressbar(&self, drop_bar: bool, f: impl FnOnce(&ProgressBar)) { - let Some(upl) = self.inner.peek_uploading() else { - return; - }; - let mut slot = self.progress.borrow_mut(); - if slot.is_none() { - let bar = ProgressBar::no_length() - .with_style( - ProgressStyle::with_template(&format!( - concat!( - "{{bar:50.cyan/blue}} {{msg:.magenta}}: ", - "{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ", - "({{eta}})", - ), - style("/").magenta(), - )) - .expect("style template is not valid"), - ) - .with_message(upl.get_name().to_owned()); + let bar = &*slot.get_or_insert_with(|| { + ProgressBar::no_length().with_style( + ProgressStyle::with_template(&format!( + concat!( + "{{bar:50.cyan/blue}} {{msg:.magenta}}: ", + "{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ", + "({{eta}})", + ), + style("/").magenta(), + )) + .expect("style template is not valid"), + ) + }); - bar.enable_steady_tick(Duration::from_millis(100)); - *slot = Some(bar); + if let Some(upl) = self.inner.peek_uploading() { + if bar.length().is_none() { + bar.set_length(upl.get_size()); + bar.set_message(upl.get_name().to_owned()); + bar.enable_steady_tick(Duration::from_millis(100)); + } + + bar.set_position(upl.get_offset()); + // BUG in `indicatif` crate? + // `set_position` does not force an immediate redraw, so we also call `inc_length` here + bar.inc_length(0); } - let bar = slot.as_ref().expect("somehow the slot holds None"); - - bar.set_position(upl.get_offset()); - // BUG in `indicatif` crate? - // `set_position` does not force an immediate redraw, so we need to call `set_length` after - bar.set_length(upl.get_size()); - f(bar); if drop_bar { @@ -162,7 +159,6 @@ impl AppState { self.http .share_create(&args.get_uri(), &args.alias, args.get_share_request())?; - self.with_progressbar(true, ProgressBar::abandon); Ok(Self::new(self.http, CacheFile::from_args(args, share_id))) }