progress bar refactoring

This commit is contained in:
Jörn-Michael Miehe 2025-06-05 21:36:41 +00:00
parent 8537684656
commit faea74241d

View file

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