diff --git a/Cargo.lock b/Cargo.lock index 8906511..88b9991 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,33 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "chrono" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87d9d13be47a5b7c3907137f1290b0459a7f80efb26be8c52afb11963bccb02" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + [[package]] name = "wasted" version = "0.1.0" +dependencies = [ + "chrono", +] diff --git a/Cargo.toml b/Cargo.toml index 605d1d6..400ceaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = { version = "0.4.29", default-features = false } diff --git a/src/main.rs b/src/main.rs index 6317c24..d73bd25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,16 @@ -use std::{env, process::Command, time::SystemTime}; +use std::{ + env, + fs::{self, File}, + io::Write, + process::{exit, Command}, + time::{SystemTime, UNIX_EPOCH}, +}; + +use chrono::NaiveDateTime; fn main() { let start_time = SystemTime::now(); + let args: Vec = env::args().collect(); println!("{:?}", args); @@ -14,11 +23,43 @@ fn main() { println!("starting build"); let exit_status = Command::new(cmd).args(&args[2..]).status(); println!("\n"); - let time_taken = start_time.elapsed().unwrap(); + let time_taken = start_time.elapsed(); println!("Took {:?}", time_taken); + log(&start_time); + println!("{:?}", exit_status); if let Some(status) = exit_status.ok().and_then(|s| s.code()) { - std::process::exit(status); + exit(status); } } + +fn log(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)); + + let today = NaiveDateTime::from_timestamp_millis(start_time as i64)?; + let mut wasted = 0; + for line in history.lines() { + let (time, duration) = line.split_once(':')?; + let time: i64 = time.parse().ok()?; + let duration: i64 = duration.parse().ok()?; + let date = NaiveDateTime::from_timestamp_millis(time)?; + if date.date() == today.date() { + wasted += duration; + } + } + { + let sec = wasted / 1000 % 60; + let min = wasted / (60 * 1000) % 60; + let h = wasted / (60 * 60 * 1000); + println!("Total wasted today: {}h {}m {}s", h, min, sec); + } + { + let mut f = File::create("compiler_history.txt").unwrap(); + f.write_all(history.as_bytes()).unwrap(); + } + Some(()) +}