From 153d4c4161fa8fba350dcc51ab14b08635c53e99 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 13 Nov 2021 04:31:50 -0500 Subject: track timing information for commands --- Cargo.lock | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/history.rs | 28 ++++++++++++++++++++++++++-- src/util.rs | 18 ++++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1edc916..7b8e735 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -199,6 +199,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + [[package]] name = "concurrent-queue" version = "1.2.2" @@ -377,6 +390,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-std", + "chrono", "futures-lite", "libc", "nix", @@ -402,6 +416,25 @@ dependencies = [ "memoffset", ] +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -579,6 +612,17 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + [[package]] name = "unicode-width" version = "0.1.9" @@ -650,6 +694,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasm-bindgen" version = "0.2.78" diff --git a/Cargo.toml b/Cargo.toml index e5cb362..649f174 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ license = "MIT" [dependencies] anyhow = "1.0.45" async-std = { version = "1.10.0", features = ["unstable"] } +chrono = "0.4.19" futures-lite = "1.12.0" libc = "0.2.107" nix = "0.23.0" diff --git a/src/history.rs b/src/history.rs index 09299ad..3615ec9 100644 --- a/src/history.rs +++ b/src/history.rs @@ -69,8 +69,10 @@ impl History { // XXX not sure if this is safe - are we sure // the child exited? let status = child.status().await.unwrap(); - task_entry.lock_arc().await.exit_status = - Some(status); + let mut entry = task_entry.lock_arc().await; + entry.exit_status = Some(status); + entry.end_instant = + Some(std::time::Instant::now()); task_action .send(crate::action::Action::UpdateFocus( crate::state::Focus::Readline, @@ -237,6 +239,22 @@ impl History { } out.write_str(&entry.cmd); out.reset_attributes(); + let time = if let Some(end_instant) = entry.end_instant { + format!( + "[{} ({})]", + entry.start_time.time().format("%H:%M:%S"), + crate::util::format_duration( + end_instant - entry.start_instant + ) + ) + } else { + format!("[{}]", entry.start_time.time().format("%H:%M:%S")) + }; + out.move_to( + (self.size.0 as usize - used_lines).try_into().unwrap(), + (self.size.1 as usize - time.len() - 1).try_into().unwrap(), + ); + out.write_str(&time); if last_row > 5 { out.write(b"\r\n"); out.set_bgcolor(textmode::color::RED); @@ -298,6 +316,9 @@ struct HistoryEntry { input: async_std::channel::Sender>, resize: async_std::channel::Sender<(u16, u16)>, exit_status: Option, + start_time: chrono::DateTime, + start_instant: std::time::Instant, + end_instant: Option, } impl HistoryEntry { @@ -315,6 +336,9 @@ impl HistoryEntry { input, resize, exit_status: None, + start_time: chrono::Local::now(), + start_instant: std::time::Instant::now(), + end_instant: None, } } diff --git a/src/util.rs b/src/util.rs index d792b91..649888c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,3 +3,21 @@ pub type Mutex = async_std::sync::Arc>; pub fn mutex(t: T) -> Mutex { async_std::sync::Arc::new(async_std::sync::Mutex::new(t)) } + +pub fn format_duration(dur: std::time::Duration) -> String { + let secs = dur.as_secs(); + let nanos = dur.subsec_nanos(); + if secs > 60 { + let mins = secs / 60; + let secs = secs - mins * 60; + format!("{}m{}s", mins, secs) + } else if secs > 0 { + format!("{}.{:03}s", secs, nanos / 1_000_000) + } else if nanos >= 1_000_000 { + format!("{}ms", nanos / 1_000_000) + } else if nanos >= 1_000 { + format!("{}us", nanos / 1_000) + } else { + format!("{}ns", nanos) + } +} -- cgit v1.2.3-54-g00ecf