Skip to content

Commit

Permalink
Add a Ctrl+C handler to the confirm workflow (#1202)
Browse files Browse the repository at this point in the history
Fixes an issue whereby exiting the confirmation prompt can lead to your
cursor disappearing: console-rs/dialoguer#294.

See:
https://github.com/mitsuhiko/rye/blob/b839a2c5b7e648e9e5d836a8bf5f703c9807d615/rye/src/main.rs#L36-L48.
  • Loading branch information
charliermarsh authored Jan 31, 2024
1 parent 262f29b commit b2f1bba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ clap = { version = "4.4.13" }
configparser = { version = "3.0.4" }
console = { version = "0.15.8", default-features = false }
csv = { version = "1.3.0" }
ctrlc = { version = "3.4.2" }
dashmap = { version = "5.5.3" }
data-encoding = { version = "2.5.0" }
derivative = { version = "2.2.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/puffin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ anyhow = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true, features = ["derive"] }
console = { workspace = true }
ctrlc = { workspace = true }
dunce = { workspace = true }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
Expand Down
13 changes: 13 additions & 0 deletions crates/puffin/src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ 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 || {
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
});
})?;

let prompt = format!(
"{} {} {} {} {}",
style("?".to_string()).for_stderr().yellow(),
Expand Down

0 comments on commit b2f1bba

Please sign in to comment.