use crate::measure::Observer;
use std::io::{IsTerminal, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
const LOG_EVERY: Duration = Duration::from_secs(30);
const TICK: Duration = Duration::from_secs(1);
pub struct Bar {
state: Arc<Mutex<State>>,
stop: Arc<AtomicBool>,
ticker: Option<JoinHandle<()>>,
}
impl Bar {
pub fn new(label: &str, names: Vec<String>) -> Self {
let state = Arc::new(Mutex::new(State::new(label, names)));
let stop = Arc::new(AtomicBool::new(false));
let ticker = {
let (state, stop) = (Arc::clone(&state), Arc::clone(&stop));
std::thread::spawn(move || {
while !stop.load(Ordering::Relaxed) {
std::thread::park_timeout(TICK);
if !stop.load(Ordering::Relaxed) {
lock(&state).tick();
}
}
})
};
Bar {
state,
stop,
ticker: Some(ticker),
}
}
pub fn finish(&mut self) {
self.stop.store(true, Ordering::Relaxed);
if let Some(t) = self.ticker.take() {
t.thread().unpark();
t.join().ok();
}
lock(&self.state).clear();
}
}
impl Drop for Bar {
fn drop(&mut self) {
self.finish();
}
}
fn lock(state: &Mutex<State>) -> std::sync::MutexGuard<'_, State> {
state.lock().unwrap_or_else(PoisonError::into_inner)
}
impl Observer for Bar {
fn planned(&mut self, remaining: &[u64]) {
lock(&self.state).planned(remaining);
}
fn started(&mut self, subject: usize) {
lock(&self.state).started(subject);
}
fn finished(&mut self, subject: usize, elapsed: Duration) {
lock(&self.state).finished(subject, elapsed);
}
fn dropped(&mut self, subject: usize) {
lock(&self.state).dropped(subject);
}
}
struct State {
label: String,
names: Vec<String>,
remaining: Vec<u64>,
spent: Vec<(Duration, u32)>,
done: u64,
current: Option<usize>,
started: Instant,
tty: bool,
last_log: Instant,
last_tenth: u64,
drawn: bool,
}
impl State {
fn new(label: &str, names: Vec<String>) -> Self {
let n = names.len();
let now = Instant::now();
State {
label: label.to_string(),
names,
remaining: vec![0; n],
spent: vec![(Duration::ZERO, 0); n],
done: 0,
current: None,
started: now,
tty: std::io::stderr().is_terminal(),
last_log: now,
last_tenth: 0,
drawn: false,
}
}
fn total(&self) -> u64 {
self.done + self.remaining.iter().sum::<u64>()
}
fn eta(&self) -> Option<Duration> {
let (all, n) = self
.spent
.iter()
.fold((Duration::ZERO, 0u32), |(t, c), &(d, k)| (t + d, c + k));
if n == 0 {
return None;
}
let fallback = all / n;
let secs: f64 = self
.remaining
.iter()
.zip(&self.spent)
.map(|(&left, &(d, k))| {
let each = if k == 0 { fallback } else { d / k };
each.as_secs_f64() * left as f64
})
.sum();
Some(Duration::from_secs_f64(secs))
}
fn line(&self, width: usize) -> String {
let total = self.total().max(1);
let pct = self.done * 100 / total;
let eta = if self.remaining.iter().all(|&r| r == 0) && self.done > 0 {
"done".to_string()
} else {
self.eta()
.map_or_else(|| "estimating".to_string(), |d| format!("~{} left", fmt(d)))
};
let who = self.current.map_or("", |i| self.names[i].as_str());
let tail = format!(
" {}/{} {pct:>3}% {} elapsed, {eta} {who}",
self.done,
total,
fmt(self.started.elapsed())
);
if !self.tty {
return format!(" {}:{tail}", self.label);
}
let room = width.saturating_sub(self.label.len() + tail.len() + 6);
let cells = room.clamp(10, 30);
let filled = (cells as u64 * self.done / total) as usize;
let line = format!(
" {} [{}{}]{tail}",
self.label,
"#".repeat(filled),
"-".repeat(cells - filled)
);
line.chars().take(width.saturating_sub(1)).collect()
}
fn draw(&mut self, force: bool) {
if self.tty {
let width = std::env::var("COLUMNS")
.ok()
.and_then(|c| c.parse().ok())
.unwrap_or(80);
eprint!("\r\x1b[2K{}", self.line(width));
std::io::stderr().flush().ok();
self.drawn = true;
return;
}
let tenth = self.done * 10 / self.total().max(1);
if force || tenth > self.last_tenth || self.last_log.elapsed() >= LOG_EVERY {
eprintln!("{}", self.line(usize::MAX));
self.last_tenth = tenth;
self.last_log = Instant::now();
}
}
fn tick(&mut self) {
if self.total() == 0 {
return;
}
if self.tty {
if self.drawn {
self.draw(false);
}
} else if self.last_log.elapsed() >= LOG_EVERY {
self.draw(true);
}
}
fn clear(&mut self) {
if self.tty && self.drawn {
eprint!("\r\x1b[2K");
std::io::stderr().flush().ok();
self.drawn = false;
}
}
}
impl Observer for State {
fn planned(&mut self, remaining: &[u64]) {
self.remaining = remaining.to_vec();
self.draw(false);
}
fn started(&mut self, subject: usize) {
self.current = Some(subject);
if self.tty {
self.draw(false);
}
}
fn finished(&mut self, subject: usize, elapsed: Duration) {
self.remaining[subject] = self.remaining[subject].saturating_sub(1);
let (d, k) = &mut self.spent[subject];
*d += elapsed;
*k += 1;
self.done += 1;
self.current = None;
let last = self.remaining.iter().all(|&r| r == 0);
self.draw(last);
}
fn dropped(&mut self, subject: usize) {
self.remaining[subject] = 0;
self.current = None;
self.draw(false);
}
}
pub fn fmt(d: Duration) -> String {
let s = d.as_secs();
match s {
0 => format!("{}ms", d.as_millis()),
1..60 => format!("{s}s"),
60..3600 => format!("{}m{:02}s", s / 60, s % 60),
_ => format!("{}h{:02}m", s / 3600, (s % 3600) / 60),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn bar(names: &[&str]) -> State {
let mut b = State::new("b", names.iter().map(|s| s.to_string()).collect());
b.tty = false;
b.last_log = Instant::now();
b
}
#[test]
fn durations_read_naturally() {
assert_eq!(fmt(Duration::from_millis(850)), "850ms");
assert_eq!(fmt(Duration::from_secs(42)), "42s");
assert_eq!(fmt(Duration::from_secs(185)), "3m05s");
assert_eq!(fmt(Duration::from_secs(3720)), "1h02m");
}
#[test]
fn the_estimate_is_per_subject() {
let mut b = bar(&["fast", "slow"]);
b.planned(&[11, 6]);
b.finished(0, Duration::from_millis(100));
b.finished(1, Duration::from_secs(20));
let eta = b.eta().unwrap().as_secs_f64();
assert!((eta - 101.0).abs() < 0.01, "{eta}");
}
#[test]
fn an_untimed_subject_is_guessed_at_the_average() {
let mut b = bar(&["a", "b"]);
b.planned(&[2, 3]);
assert_eq!(b.eta(), None, "nothing timed yet");
b.finished(0, Duration::from_secs(2));
assert_eq!(b.eta(), Some(Duration::from_secs(8)));
}
#[test]
fn a_dropped_subject_leaves_the_estimate() {
let mut b = bar(&["a", "b"]);
b.planned(&[2, 5]);
b.finished(0, Duration::from_secs(1));
b.finished(1, Duration::from_secs(10));
b.dropped(1);
assert_eq!(b.eta(), Some(Duration::from_secs(1)));
assert_eq!(b.total(), 3);
}
#[test]
fn the_bar_fits_the_terminal() {
let mut b = bar(&["aube"]);
b.tty = true;
b.planned(&[40]);
for _ in 0..10 {
b.finished(0, Duration::from_millis(250));
}
let line = b.line(80);
assert!(line.chars().count() < 80, "{line}");
assert!(line.contains("10/40") && line.contains("25%"), "{line}");
assert!(line.contains("~7s left"), "{line}");
}
#[test]
fn a_log_hears_from_a_long_sample_before_it_finishes() {
let mut b = bar(&["slow"]);
b.planned(&[5]);
b.started(0);
let before = b.last_log;
b.tick();
assert_eq!(b.last_log, before, "too soon to log again");
b.last_log = Instant::now() - LOG_EVERY;
b.tick();
assert!(b.last_log > before, "the ticker logged mid-sample");
}
#[test]
fn the_ticker_stops_when_finished() {
let mut b = Bar::new("b", vec!["a".into()]);
b.planned(&[1]);
b.finished(0, Duration::from_millis(1));
b.finish();
assert!(b.ticker.is_none());
}
}