refactor: make a with_progressbar function
This commit is contained in:
parent
0a12b87a2a
commit
2aaae9239c
2 changed files with 36 additions and 18 deletions
24
src/utils.rs
24
src/utils.rs
|
|
@ -1,5 +1,5 @@
|
|||
use std::{error::Error, future::Future};
|
||||
use indicatif::ProgressBar;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
|
||||
pub async fn with_spinner<F, Fut, T>(
|
||||
start_msg: &str,
|
||||
|
|
@ -21,3 +21,25 @@ where
|
|||
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn with_progressbar<F, Fut, T>(
|
||||
start_msg: &str,
|
||||
finish_msg: &str,
|
||||
len: u64,
|
||||
f: F,
|
||||
) -> Result<T, Box<dyn Error>>
|
||||
where
|
||||
F: FnOnce(ProgressBar) -> Fut,
|
||||
Fut: Future<Output = Result<T, Box<dyn Error>>>
|
||||
{
|
||||
let pb = ProgressBar::new(len);
|
||||
pb.set_style(ProgressStyle::default_bar().template(
|
||||
"{msg}\n{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})",
|
||||
)?);
|
||||
pb.set_message(start_msg.to_string());
|
||||
|
||||
let result = f(pb.clone()).await;
|
||||
pb.finish_with_message(finish_msg.to_string());
|
||||
|
||||
result
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue