Suppress MultipleHandlers from Ctrl-C in confirm (#2903)

## Summary

Fixes #2900

## Test Plan

Tried reproducing the steps described in #2900,
but with `cargo run -- pip ...` and it didn't crash 😄.
This commit is contained in:
Sławomir Ehlert 2024-04-08 19:18:53 +02:00 committed by GitHub
parent 10dfd43af9
commit f1630a70f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,7 +6,8 @@ use console::{style, Key, Term};
/// This is a slimmed-down version of `dialoguer::Confirm`, with the post-confirmation report
/// enabled.
pub(crate) fn confirm(message: &str, term: &Term, default: bool) -> Result<bool> {
ctrlc::set_handler(move || {
// Set the Ctrl-C handler to exit the process.
let result = ctrlc::set_handler(move || {
let term = Term::stderr();
term.show_cursor().ok();
term.flush().ok();
@ -17,7 +18,16 @@ pub(crate) fn confirm(message: &str, term: &Term, default: bool) -> Result<bool>
} else {
130
});
})?;
});
match result {
Ok(()) => {}
Err(ctrlc::Error::MultipleHandlers) => {
// If multiple handlers were set, we assume that the existing handler is our
// confirmation handler, and continue.
}
Err(e) => return Err(e.into()),
}
let prompt = format!(
"{} {} {} {} {}",