use colored::*;
use std::io::{self, Write};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::thread;
use std::time::Duration;
pub struct Spinner {
running: Arc<AtomicBool>,
}
impl Spinner {
pub fn start(message: &str) -> Self {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let msg = message.to_string();
thread::spawn(move || {
let symbols = ["|", "/", "-", "\\"];
let mut i = 0;
while r.load(Ordering::SeqCst) {
print!("\r{} {}", symbols[i % 4].bright_cyan(), msg);
let _ = io::stdout().flush();
thread::sleep(Duration::from_millis(100));
i += 1;
}
print!("\r✔ {} \n", msg);
let _ = io::stdout().flush();
});
Self { running }
}
pub fn stop(&self) {
self.running.store(false, Ordering::SeqCst);
}
}