use crossbeam::channel::{bounded, Receiver};
use std::sync::atomic::Ordering::Relaxed;
use std::{
sync::atomic::AtomicBool,
thread::{self, sleep},
time::Duration,
};
use crate::cli_pretty_printing::countdown_until_program_ends;
static PAUSED: AtomicBool = AtomicBool::new(false);
pub fn start(duration: u32) -> Receiver<()> {
let (sender, recv) = bounded(1);
thread::spawn(move || {
let mut time_spent = 0;
while time_spent < duration {
if !PAUSED.load(Relaxed) {
sleep(Duration::from_secs(1));
time_spent += 1;
countdown_until_program_ends(time_spent, duration);
}
}
sender.send(()).expect("Timer should send succesfully");
});
recv
}
pub fn pause() {
PAUSED.store(true, Relaxed);
}
pub fn resume() {
PAUSED.store(false, Relaxed);
}