use std::time::{Duration, Instant};
fn duration_brief(d: Duration) -> String {
let secs = d.as_secs();
if secs >= 120 {
format!("{} min", secs / 60)
} else {
format!("{secs} sec")
}
}
pub fn estimate_remaining(start: &Instant, done: usize, total: usize) -> String {
let elapsed = start.elapsed();
if total == 0 || done == 0 || elapsed.is_zero() || done > total {
"??".into()
} else {
let done = done as f64;
let total = total as f64;
let estimate = Duration::from_secs_f64(elapsed.as_secs_f64() * (total / done - 1.0));
duration_brief(estimate)
}
}
pub fn percent_done(done: usize, total: usize) -> String {
if total == 0 || done > total {
"??%".into()
} else {
format!("{:.1}%", done as f64 * 100.0 / total as f64)
}
}