use std::io::{self, Write};
pub struct ProgressBar {
total: u32,
width: usize,
}
impl ProgressBar {
pub fn new(total: u32) -> Self {
Self { total, width: 20 }
}
pub fn set_width(mut self, width: usize) -> Self {
self.width = width;
self
}
pub fn draw(&self, current: u32) {
let total = self.total.max(1);
let ratio = current.min(total) as f32 / total as f32;
let filled = (ratio * self.width as f32).round() as usize;
let empty = self.width - filled;
print!(
"\r[{}{}] {:.0}%",
"█".repeat(filled),
"░".repeat(empty),
ratio * 100.0
);
let _ = io::stdout().flush();
}
pub fn finish(&self) {
println!();
}
}