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,
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();