2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) struct InterruptHandler {
|
2021-09-16 06:44:40 -07:00
|
|
|
blocks: u32,
|
2018-08-27 16:03:52 -07:00
|
|
|
interrupted: bool,
|
2021-09-16 06:44:40 -07:00
|
|
|
verbosity: Verbosity,
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InterruptHandler {
|
2021-03-25 16:51:29 -07:00
|
|
|
pub(crate) fn install(verbosity: Verbosity) -> Result<(), ctrlc::Error> {
|
|
|
|
let mut instance = Self::instance();
|
|
|
|
instance.verbosity = verbosity;
|
2020-01-15 02:16:13 -08:00
|
|
|
ctrlc::set_handler(|| Self::instance().interrupt())
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn instance() -> MutexGuard<'static, Self> {
|
2022-12-15 16:53:21 -08:00
|
|
|
static INSTANCE: Mutex<InterruptHandler> = Mutex::new(InterruptHandler::new());
|
2018-08-27 16:03:52 -07:00
|
|
|
|
|
|
|
match INSTANCE.lock() {
|
|
|
|
Ok(guard) => guard,
|
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
|
|
|
Err(poison_error) => {
|
2021-07-28 18:06:57 -07:00
|
|
|
eprintln!(
|
|
|
|
"{}",
|
|
|
|
Error::Internal {
|
2022-12-15 16:53:21 -08:00
|
|
|
message: format!("interrupt handler mutex poisoned: {poison_error}"),
|
2021-07-28 18:06:57 -07:00
|
|
|
}
|
|
|
|
.color_display(Color::auto().stderr())
|
|
|
|
);
|
2023-10-16 20:07:09 -07:00
|
|
|
process::exit(EXIT_FAILURE);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:53:21 -08:00
|
|
|
const fn new() -> Self {
|
2020-01-15 02:16:13 -08:00
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
blocks: 0,
|
2018-08-27 16:03:52 -07:00
|
|
|
interrupted: false,
|
2021-09-16 06:44:40 -07:00
|
|
|
verbosity: Verbosity::default(),
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn interrupt(&mut self) {
|
|
|
|
self.interrupted = true;
|
|
|
|
|
|
|
|
if self.blocks > 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit() {
|
|
|
|
process::exit(130);
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn block(&mut self) {
|
2018-08-27 16:03:52 -07:00
|
|
|
self.blocks += 1;
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn unblock(&mut self) {
|
2018-08-27 16:03:52 -07:00
|
|
|
if self.blocks == 0 {
|
2021-03-25 16:51:29 -07:00
|
|
|
if self.verbosity.loud() {
|
2021-07-28 18:06:57 -07:00
|
|
|
eprintln!(
|
|
|
|
"{}",
|
|
|
|
Error::Internal {
|
|
|
|
message: "attempted to unblock interrupt handler, but handler was not blocked"
|
|
|
|
.to_owned(),
|
|
|
|
}
|
|
|
|
.color_display(Color::auto().stderr())
|
|
|
|
);
|
2021-03-25 16:51:29 -07:00
|
|
|
}
|
2023-10-16 20:07:09 -07:00
|
|
|
process::exit(EXIT_FAILURE);
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
self.blocks -= 1;
|
|
|
|
|
|
|
|
if self.interrupted {
|
|
|
|
Self::exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn guard<T, F: FnOnce() -> T>(function: F) -> T {
|
2018-08-27 16:03:52 -07:00
|
|
|
let _guard = InterruptGuard::new();
|
|
|
|
function()
|
|
|
|
}
|
|
|
|
}
|