progress bar refactoring
This commit is contained in:
parent
8537684656
commit
faea74241d
1 changed files with 23 additions and 25 deletions
|
|
@ -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}})",
|
||||
// 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();
|
||||
|
||||
ProgressBar::new(uploading.size())
|
||||
.with_style(ps)
|
||||
.unwrap(),
|
||||
)
|
||||
.with_message(uploading.file_name().to_owned())
|
||||
.with_position(uploading.offset())
|
||||
};
|
||||
pb.tick();
|
||||
.with_position(uploading.offset());
|
||||
|
||||
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) {
|
||||
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();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue