use kdam::{Animation, Bar, BarExt};
use std::sync::{Arc, Mutex};
pub struct ProgressBarConfig {
pub total: usize,
pub desc: &'static str,
pub animation: Animation,
}
impl ProgressBarConfig {
pub fn new(total: usize, desc: &'static str, animation: Animation) -> Self {
Self {
total,
desc,
animation,
}
}
}
pub fn create_progress_bar(config: ProgressBarConfig) -> Arc<Mutex<Bar>> {
Arc::new(Mutex::new(kdam::tqdm!(
total = config.total,
desc = config.desc,
animation = config.animation
)))
}
pub fn update_progress_bar(pb: Option<&Arc<Mutex<Bar>>>) {
if let Some(pb) = pb {
if let Ok(mut pb) = pb.try_lock() {
let _ = pb.update(1);
}
}
}
#[macro_export]
macro_rules! with_progress {
($pb:expr, $func:expr) => {{
let result = $func;
if $pb.is_some() {
$crate::engine::progress::update_progress_bar($pb.as_ref());
}
result
}};
}