# Description This PR introduces a new `Signals` struct to replace our adhoc passing around of `ctrlc: Option<Arc<AtomicBool>>`. Doing so has a few benefits: - We can better enforce when/where resetting or triggering an interrupt is allowed. - Consolidates `nu_utils::ctrl_c::was_pressed` and other ad-hoc re-implementations into a single place: `Signals::check`. - This allows us to add other types of signals later if we want. E.g., exiting or suspension. - Similarly, we can more easily change the underlying implementation if we need to in the future. - Places that used to have a `ctrlc` of `None` now use `Signals::empty()`, so we can double check these usages for correctness in the future.
15 lines
434 B
Rust
15 lines
434 B
Rust
use nu_protocol::{engine::EngineState, Signals};
|
|
use std::sync::{
|
|
atomic::{AtomicBool, Ordering},
|
|
Arc,
|
|
};
|
|
|
|
pub(crate) fn ctrlc_protection(engine_state: &mut EngineState) {
|
|
let interrupt = Arc::new(AtomicBool::new(false));
|
|
engine_state.set_signals(Signals::new(interrupt.clone()));
|
|
ctrlc::set_handler(move || {
|
|
interrupt.store(true, Ordering::Relaxed);
|
|
})
|
|
.expect("Error setting Ctrl-C handler");
|
|
}
|