summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-11-13 04:31:50 -0500
committerJesse Luehrs <doy@tozt.net>2021-11-13 04:31:50 -0500
commit153d4c4161fa8fba350dcc51ab14b08635c53e99 (patch)
tree1c587518bf112e27286f7f3e76274c980dc31856 /src
parent7c0da0cd97536a9c412d91524971f00af87d1bdc (diff)
downloadnbsh-153d4c4161fa8fba350dcc51ab14b08635c53e99.tar.gz
nbsh-153d4c4161fa8fba350dcc51ab14b08635c53e99.zip
track timing information for commands
Diffstat (limited to 'src')
-rw-r--r--src/history.rs28
-rw-r--r--src/util.rs18
2 files changed, 44 insertions, 2 deletions
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<Vec<u8>>,
resize: async_std::channel::Sender<(u16, u16)>,
exit_status: Option<async_std::process::ExitStatus>,
+ start_time: chrono::DateTime<chrono::Local>,
+ start_instant: std::time::Instant,
+ end_instant: Option<std::time::Instant>,
}
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<T> = async_std::sync::Arc<async_std::sync::Mutex<T>>;
pub fn mutex<T>(t: T) -> Mutex<T> {
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)
+ }
+}