diff --git a/src/appstate.rs b/src/appstate.rs index bbe762e..9038d0b 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -3,6 +3,7 @@ use std::{ fs, io::{self, Write}, path::{Path, PathBuf}, + time::Duration, }; use console::style; @@ -119,34 +120,31 @@ impl AppState { debug!("{uploading} chunk {chunk_size}"); - let pb = match self.progress { - Some(ref pb) => pb, - None => { - self.progress = Some({ - let pb = { - let ps = ProgressStyle::with_template(&format!( - "{{msg:.yellow}}: {{bar:50.cyan/blue}} {{binary_bytes:.magenta}}{}{{binary_total_bytes:.magenta}} ({{elapsed}})", - style("/").magenta(), - )) - .unwrap(); + // Initialize or fetch the existing ProgressBar in one call: + let bar = &*self.progress.get_or_insert_with(|| { + // Create a new bar with style + let bar = ProgressBar::new(uploading.size()) + .with_style( + ProgressStyle::with_template(&format!( + concat!( + "{{msg:.yellow}}: {{bar:50.cyan/blue}} ", + "{{binary_bytes:.magenta}}{}{{binary_total_bytes:.magenta}} ", + "({{eta}})", + ), + style("/").magenta(), + )) + .unwrap(), + ) + .with_message(uploading.file_name().to_owned()) + .with_position(uploading.offset()); - ProgressBar::new(uploading.size()) - .with_style(ps) - .with_message(uploading.file_name().to_owned()) - .with_position(uploading.offset()) - }; - pb.tick(); - - pb - }); - self.progress.as_ref().unwrap() - } - }; - pb.tick(); + bar.enable_steady_tick(Duration::from_millis(100)); + bar + }); match uploading.upload_chunk(http, &self.alias, chunk_size) { ChunkState::Ok(upl) => { - pb.set_position(upl.offset()); + bar.set_position(upl.offset()); self.files.push_front(FileState::U(upl)); Ok(Some(())) } @@ -156,7 +154,7 @@ impl AppState { } ChunkState::Finished(path) => { debug!("Finished {:?}!", path.display()); - pb.finish(); + bar.finish(); self.progress = None; self.share.notify(http, &self.alias).unwrap();