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 --- src/history.rs | 28 ++++++++++++++++++++++++++-- src/util.rs | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'src') 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