just/src/interrupt_handler.rs
Casey Rodarmor aefdcea7d0
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
  to be implicitly inherited by subprocesses, we now use
  `Command::current_dir` to set it explicitly. This feels much better,
  since we aren't dependent on the implicit state of the process's
  current directory.

- Subcommand execution is much improved.

- Added a ton of tests for config parsing, config execution, working
  dir, and search dir.

- Error messages are improved. Many more will be colored.

- The Config is now onwed, instead of borrowing from the arguments and
  the `clap::ArgMatches` object. This is a huge ergonomic improvement,
  especially in tests, and I don't think anyone will notice.

- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
  matching git, which I think is what most people will expect.

- Added a cute `tmptree!{}` macro, for creating temporary directories
  populated with directories and files for tests.

- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00

80 lines
1.6 KiB
Rust

use crate::common::*;
pub(crate) struct InterruptHandler {
blocks: u32,
interrupted: bool,
}
impl InterruptHandler {
pub(crate) fn install() -> Result<(), ctrlc::Error> {
ctrlc::set_handler(|| InterruptHandler::instance().interrupt())
}
pub(crate) fn instance() -> MutexGuard<'static, InterruptHandler> {
lazy_static! {
static ref INSTANCE: Mutex<InterruptHandler> = Mutex::new(InterruptHandler::new());
}
match INSTANCE.lock() {
Ok(guard) => guard,
Err(poison_error) => {
eprintln!(
"{}",
RuntimeError::Internal {
message: format!("interrupt handler mutex poisoned: {}", poison_error),
}
);
std::process::exit(EXIT_FAILURE);
}
}
}
fn new() -> InterruptHandler {
InterruptHandler {
blocks: 0,
interrupted: false,
}
}
fn interrupt(&mut self) {
self.interrupted = true;
if self.blocks > 0 {
return;
}
Self::exit();
}
fn exit() {
process::exit(130);
}
pub(crate) fn block(&mut self) {
self.blocks += 1;
}
pub(crate) fn unblock(&mut self) {
if self.blocks == 0 {
eprintln!(
"{}",
RuntimeError::Internal {
message: "attempted to unblock interrupt handler, but handler was not blocked"
.to_string(),
}
);
std::process::exit(EXIT_FAILURE);
}
self.blocks -= 1;
if self.interrupted {
Self::exit();
}
}
pub(crate) fn guard<T, F: FnOnce() -> T>(function: F) -> T {
let _guard = InterruptGuard::new();
function()
}
}