refactor: make a with_progressbar function

This commit is contained in:
RafayAhmad7548 2026-08-31 00:08:48 +05:00
parent 0a12b87a2a
commit 2aaae9239c
Signed by: RafayAhmad
SSH key fingerprint: SHA256:WURX8viobA1uawb4dWM3LqYrY+XPcZcXhAXAlrYdhtE
2 changed files with 36 additions and 18 deletions

View file

@ -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
}