use std::io::{stdout, Write};
use std::sync::mpsc::{self, SyncSender, TryRecvError};
use std::{thread, time};
const PRINT_PULSE_MILLISEC: u64 = 500;
pub struct ProgressIndicator {
tx: SyncSender<()>,
}
impl ProgressIndicator {
pub fn start(msg: Option<String>) -> Self {
let (tx, rx) = mpsc::sync_channel(0);
thread::spawn(move || {
match msg {
Some(m) => print!("{}", m),
None => print!("Processing"),
};
loop {
match rx.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
print!("\r");
stdout().flush().unwrap();
break;
}
Err(TryRecvError::Empty) => {
print!(".");
stdout().flush().unwrap();
}
}
thread::sleep(time::Duration::from_millis(PRINT_PULSE_MILLISEC));
}
});
ProgressIndicator { tx }
}
pub fn stop(&self) {
self.tx.send(()).unwrap();
}
}