diff --git a/src/main.rs b/src/main.rs index e8a9149..87613f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,8 @@ use crate::refractr::Refractr; use clap::Parser; use std::path::PathBuf; use std::process; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; #[cfg(target_family = "windows")] use username; #[cfg(target_family = "unix")] @@ -84,6 +86,7 @@ fn main() -> Result<(), String> { strict: args.strict, unix: cfg!(unix), verbose: args.verbose, + run: Arc::new(AtomicBool::new(true)) }; // warn to avoid root/admin diff --git a/src/refractr.rs b/src/refractr.rs index 6464e08..b2157a6 100644 --- a/src/refractr.rs +++ b/src/refractr.rs @@ -29,6 +29,7 @@ pub struct Refractr { pub strict: bool, pub unix: bool, pub verbose: u8, + pub run: Arc } struct OpenedRepository { @@ -334,6 +335,11 @@ impl Refractr { } pub fn run(&self, cfgs: Vec) -> Result<(), ReturnData> { + let r = self.run.clone(); + ctrlc::set_handler(move || + r.store(true, Ordering::SeqCst) + ).expect("failed to set ^c handler"); + common::verbose(self.verbose, 3, format!("Starting main refractr loop")); let mut loop_repos = Vec::new(); let mut work_dirs = Vec::new(); @@ -499,6 +505,9 @@ impl Refractr { return Err(e); } } + if self.run.load(Ordering::SeqCst) { + common::error_quit(format!("exiting"), ExitCode::HaltError); + } } // end for