handle ctrl+c

This commit is contained in:
Crispy 2024-01-02 11:03:49 +01:00
parent e3ea2bd396
commit f88454b6b7
3 changed files with 156 additions and 21 deletions

106
Cargo.lock generated
View file

@ -8,6 +8,18 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.29"
@ -17,6 +29,33 @@ dependencies = [
"num-traits",
]
[[package]]
name = "ctrlc"
version = "3.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf"
dependencies = [
"nix",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "nix"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags",
"cfg-if",
"libc",
]
[[package]]
name = "num-traits"
version = "0.2.16"
@ -31,4 +70,71 @@ name = "wasted"
version = "0.1.0"
dependencies = [
"chrono",
"ctrlc",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View file

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
chrono = { version = "0.4.29", default-features = false }
ctrlc = "3.4.1"

View file

@ -3,12 +3,20 @@ use std::{
fs::{self, File},
io::Write,
process::{exit, Command},
time::{SystemTime, UNIX_EPOCH},
sync::mpsc::channel,
thread,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use chrono::NaiveDateTime;
fn main() {
let (send_ctrlc, recv_ctrlc) = channel();
ctrlc::set_handler(move || {
send_ctrlc.send(()).unwrap();
})
.expect("Error setting Ctrl-C handler");
let start_time = SystemTime::now();
let args: Vec<String> = env::args().collect();
@ -18,30 +26,50 @@ fn main() {
print_day();
return;
}
println!("{:?}", args);
let cmd = &args[1];
println!("{args:?}");
println!("Starting build");
let exit_status = Command::new(cmd).args(&args[2..]).status();
println!("\n");
let time_taken = start_time.elapsed().unwrap().as_millis() as i64;
println!("Build took {}", printable_time(time_taken));
let cmd = args[1].clone();
let compiler_thread = thread::spawn(move || Command::new(cmd).args(&args[2..]).status());
log_single(&start_time);
print_day();
println!();
let mut exit_status = None;
loop {
if let Ok(()) = recv_ctrlc.try_recv() {
println!("\nCompilation killed");
break;
}
if compiler_thread.is_finished() {
exit_status = Some(compiler_thread.join());
break;
}
thread::sleep(Duration::from_millis(10));
}
println!("{:?}", exit_status);
if let Some(status) = exit_status.ok().and_then(|s| s.code()) {
exit(status);
{
let time_taken = start_time.elapsed().unwrap().as_millis() as i64;
println!("\nBuild took {}", printable_time(time_taken));
log_single(start_time);
print_day();
println!();
if let Some(exit_status) = exit_status {
if let Ok(Ok(status)) = exit_status {
println!("exit status: {:?}", status.code());
exit(status.code().unwrap_or_default());
} else {
print!("invalid exit status: {exit_status:?}");
}
} else {
exit(1);
}
}
}
fn log_single(start: &SystemTime) -> Option<()> {
fn log_single(start: SystemTime) -> Option<()> {
let start_time = start.duration_since(UNIX_EPOCH).ok()?.as_millis();
let duration = start.elapsed().ok()?.as_millis();
let mut history = fs::read_to_string("compiler_history.txt").unwrap_or_default();
history.push_str(&format!("{}:{}\n", start_time, duration));
history.push_str(&format!("{start_time}:{duration}\n"));
let mut f = File::create("compiler_history.txt").unwrap();
f.write_all(history.as_bytes()).unwrap();
Some(())
@ -68,11 +96,11 @@ fn print_day() -> Option<()> {
wasted_today += duration;
}
if first_date.is_empty() {
first_date = format!("{:?}", date)[..10].to_owned();
first_date = format!("{date:?}")[..10].to_owned();
}
}
println!("Total wasted today: {}", printable_time(wasted_today));
println!("Since {}: {}", first_date, printable_time(wasted));
println!("Since {first_date}: {}", printable_time(wasted));
Some(())
}
@ -81,10 +109,10 @@ fn printable_time(ms: i64) -> String {
let mins = ms / (60 * 1000) % 60;
let hours = ms / (60 * 60 * 1000);
if hours > 0 {
format!("{}h {}m {}s", hours, mins, secs)
format!("{hours}h {mins}m {secs}s")
} else if mins > 0 {
format!("{}m {}s", mins, secs)
format!("{mins}m {secs}s")
} else {
format!("{}.{:03}s", secs, ms % 1000)
format!("{secs}.{:03}s", ms % 1000)
}
}