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,
|
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!(
|
||||||
style("/").magenta(),
|
"{{msg:.yellow}}: {{bar:50.cyan/blue}} ",
|
||||||
))
|
"{{binary_bytes:.magenta}}{}{{binary_total_bytes:.magenta}} ",
|
||||||
.unwrap();
|
"({{eta}})",
|
||||||
|
),
|
||||||
|
style("/").magenta(),
|
||||||
|
))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.with_message(uploading.file_name().to_owned())
|
||||||
|
.with_position(uploading.offset());
|
||||||
|
|
||||||
ProgressBar::new(uploading.size())
|
bar.enable_steady_tick(Duration::from_millis(100));
|
||||||
.with_style(ps)
|
bar
|
||||||
.with_message(uploading.file_name().to_owned())
|
});
|
||||||
.with_position(uploading.offset())
|
|
||||||
};
|
|
||||||
pb.tick();
|
|
||||||
|
|
||||||
pb
|
|
||||||
});
|
|
||||||
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();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue