Skip to content

Commit

Permalink
Add Ctrl+C handler on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon01 committed Sep 29, 2024
1 parent 952ee25 commit de22a7f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() {
}
};

let _ = ctrlc::set();
let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {
cargo::ops::fix_exec_rustc(&gctx, &lock_addr).map_err(|e| CliError::from(e))
} else {
Expand All @@ -41,6 +42,29 @@ fn main() {
}
}

#[cfg(windows)]
mod ctrlc {
use super::ProcessError;
use anyhow::Result;
use cargo::util::TaskbarProgress;
use windows_sys::Win32::Foundation::{BOOL, FALSE, TRUE};
use windows_sys::Win32::System::Console::SetConsoleCtrlHandler;

unsafe extern "system" fn ctrlc_handler(_: u32) -> BOOL {
eprintln!("{}", TaskbarProgress::None);
std::process::exit(0);
}

pub fn set() -> Result<()> {
unsafe {
if SetConsoleCtrlHandler(Some(ctrlc_handler), TRUE) == FALSE {
return Err(ProcessError::new("Could not set Ctrl-C handler.", None, None).into());
}
}
Ok(())
}
}

fn setup_logger() -> Option<ChromeFlushGuard> {
use tracing_subscriber::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use self::into_url::IntoUrl;
pub use self::into_url_with_base::IntoUrlWithBase;
pub(crate) use self::io::LimitErrorReader;
pub use self::lockserver::{LockServer, LockServerClient, LockServerStarted};
pub use self::progress::{Progress, ProgressStyle};
pub use self::progress::{Progress, ProgressStyle, TaskbarProgress};
pub use self::queue::Queue;
pub use self::rustc::Rustc;
pub use self::semver_ext::{OptVersionReq, VersionExt};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct Format {
max_print: usize,
}

enum TaskbarProgress {
pub enum TaskbarProgress {
None,
Value(u8),
Error,
Expand Down

0 comments on commit de22a7f

Please sign in to comment.