#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]
use std::io::Write;
use std::sync::Arc;
use colored::Colorize;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
pub struct ProgressManager {
multi: Arc<MultiProgress>,
enabled: bool,
}
impl ProgressManager {
#[must_use]
pub fn new(enabled: bool) -> Self {
Self {
multi: Arc::new(MultiProgress::new()),
enabled,
}
}
#[must_use]
pub fn create_file_bar(&self, label: &str, total: u64) -> ProgressBar {
if !self.enabled {
return ProgressBar::hidden();
}
let pb = self.multi.add(ProgressBar::new(total));
pb.set_style(
ProgressStyle::default_bar()
.template(" {prefix:<30} [{bar:25.green/dim}] {pos}/{len} files")
.expect("Invalid progress bar template")
.progress_chars("━━─"),
);
pb.set_prefix(label.to_string());
pb
}
#[allow(clippy::unused_self)]
pub fn print_result(&self, label: &str, result: &str, is_success: bool) {
if is_success {
println!("{} {:<30} {}", "✓".green(), label, result.dimmed());
} else {
println!("{} {:<30} {}", "•".dimmed(), label, result.dimmed());
}
let _ = std::io::stdout().flush();
}
#[allow(clippy::unused_self)]
pub fn print_result_with_count(&self, label: &str, result: &str, file_count: u64) {
println!(
"{} {:<30} {} ({} files)",
"✓".green(),
label,
result.dimmed(),
file_count
);
let _ = std::io::stdout().flush();
}
#[must_use]
pub fn create_scanning_bar(&self, total: u64) -> ProgressBar {
if !self.enabled {
return ProgressBar::hidden();
}
let pb = self.multi.add(ProgressBar::new(total));
pb.set_style(
ProgressStyle::default_bar()
.template(" Scanning [{bar:20.green/dim}] {pos}/{len} {msg}")
.expect("Invalid progress bar template")
.progress_chars("━━─"),
);
pb
}
pub fn clear(&self) {
self.multi.clear().ok();
}
}