use crate::exit;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use crate::cmd::CmdLineRunner;
use console::Term;
use signal_hook::consts::SIGINT;
use signal_hook::iterator::Signals;
static EXIT: AtomicBool = AtomicBool::new(true);
static SHOW_CURSOR: AtomicBool = AtomicBool::new(false);
pub fn init() {
thread::spawn(move || {
let mut signals = Signals::new([SIGINT]).unwrap();
let _handle = signals.handle();
while let Some(_signal) = signals.forever().next() {
if SHOW_CURSOR.load(Ordering::Relaxed) {
let _ = Term::stderr().show_cursor();
}
CmdLineRunner::kill_all(nix::sys::signal::SIGINT);
if EXIT.swap(true, Ordering::Relaxed) {
debug!("Ctrl-C pressed, exiting...");
exit(1);
} else {
warn!("Ctrl-C pressed, please wait for tasks to finish or press Ctrl-C again to force exit");
}
}
});
}
pub fn exit_on_ctrl_c(do_exit: bool) {
EXIT.store(do_exit, Ordering::Relaxed);
}
pub fn show_cursor_after_ctrl_c() {
SHOW_CURSOR.store(true, Ordering::Relaxed);
}