use std::fs::File;
use std::path::Path;
pub use indicatif::ProgressBar;
use indicatif::{ProgressBarIter, ProgressDrawTarget};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Unit {
Bytes,
Transcripts,
}
pub trait Progress: Sync {
fn bar(&self, step: &str, total: u64, unit: Unit) -> ProgressBar;
}
pub struct NoProgress;
impl Progress for NoProgress {
fn bar(&self, _step: &str, total: u64, _unit: Unit) -> ProgressBar {
hidden_bar(total)
}
}
pub fn hidden_bar(total: u64) -> ProgressBar {
ProgressBar::with_draw_target(Some(total), ProgressDrawTarget::hidden())
}
pub fn open_with_progress(
path: &Path,
progress: &dyn Progress,
) -> std::io::Result<ProgressBarIter<File>> {
let file = File::open(path)?;
let name = path.file_name().unwrap_or_default().to_string_lossy();
let bar = progress.bar(
&format!("Loading {name}"),
file.metadata()?.len(),
Unit::Bytes,
);
Ok(bar.wrap_read(file))
}