use std::{io::IsTerminal, time::Duration};
use indicatif::{ProgressBar, ProgressStyle};
pub struct Progress(ProgressBar);
impl Progress {
pub fn new(message: &'static str, enabled: bool) -> Self {
let bar = if enabled && std::io::stderr().is_terminal() {
ProgressBar::new_spinner().with_style(
ProgressStyle::with_template("{spinner:.cyan} {msg}")
.expect("valid progress template")
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]),
)
} else {
ProgressBar::hidden()
};
bar.set_message(message);
if !bar.is_hidden() {
bar.enable_steady_tick(Duration::from_millis(80));
bar.tick();
}
Self(bar)
}
pub fn message(&self, message: impl Into<String>) {
self.0.set_message(message.into());
}
pub fn warn(&self, message: impl std::fmt::Display) {
self.0.suspend(|| eprintln!("Warning: {message}"));
}
}
impl Drop for Progress {
fn drop(&mut self) {
self.0.finish_and_clear();
}
}