Skip to content

Commit

Permalink
handle Ctrl-C explicitly in confirm prompt
Browse files Browse the repository at this point in the history
Replace `uv-console`'s use of `ctrlc::set_handler` to exit the prompt with an explicit
matching on the user inputting Ctrl-C.
  • Loading branch information
ericmarkmartin committed Feb 22, 2025
1 parent efc8d94 commit 6583e12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/uv-console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ doctest = false
workspace = true

[dependencies]
ctrlc = { workspace = true }
console = { workspace = true }
37 changes: 13 additions & 24 deletions crates/uv-console/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ use std::{cmp::Ordering, iter};
/// This is a slimmed-down version of `dialoguer::Confirm`, with the post-confirmation report
/// enabled.
pub fn confirm(message: &str, term: &Term, default: bool) -> std::io::Result<bool> {
// 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();

#[allow(clippy::exit, clippy::cast_possible_wrap)]
std::process::exit(if cfg!(windows) {
0xC000_013A_u32 as i32
} 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(err) => return Err(std::io::Error::new(std::io::ErrorKind::Other, err)),
}

let prompt = format!(
"{} {} {} {} {}",
style("?".to_string()).for_stderr().yellow(),
Expand All @@ -47,11 +24,23 @@ pub fn confirm(message: &str, term: &Term, default: bool) -> std::io::Result<boo
// Match continuously on every keystroke, and do not wait for user to hit the
// `Enter` key.
let response = loop {
let input = term.read_key()?;
let input = term.read_key_raw()?;
match input {
Key::Char('y' | 'Y') => break true,
Key::Char('n' | 'N') => break false,
Key::Enter => break default,
Key::CtrlC => {
term.show_cursor()?;
term.write_str("\n")?;
term.flush()?;

#[allow(clippy::exit, clippy::cast_possible_wrap)]
std::process::exit(if cfg!(windows) {
0xC000_013A_u32 as i32
} else {
130
});
}
_ => {}
};
};
Expand Down

0 comments on commit 6583e12

Please sign in to comment.