From 7a8b7201a188197438eda55b8d47b2c0ee447a84 Mon Sep 17 00:00:00 2001 From: Ben Hoskings Date: Wed, 25 Nov 2009 02:28:42 +1100 Subject: [PATCH] Write formatted duration to ENV_DURATION after commands that run for > 1s wall time. --- reader.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/reader.c b/reader.c index b7792e767..426f06c91 100644 --- a/reader.c +++ b/reader.c @@ -2162,11 +2162,42 @@ int reader_get_cursor_pos() return data->buff_pos; } +#define ENV_CMD_DURATION L"CMD_DURATION" + +void set_env_cmd_duration(struct timeval *after, struct timeval *before) +{ + time_t secs = after->tv_sec - before->tv_sec; + suseconds_t usecs = after->tv_usec - before->tv_usec; + wchar_t buf[16]; + + if (after->tv_usec < before->tv_usec) { + usecs += 1000000; + secs -= 1; + } + + if (secs < 1) { + env_remove( ENV_CMD_DURATION, 0 ); + } else { + if (secs < 10) { // 10 secs + swprintf(buf, 16, L"%lu.%02us", secs, usecs / 10000); + } else if (secs < 60) { // 1 min + swprintf(buf, 16, L"%lu.%01us", secs, usecs / 100000); + } else if (secs < 600) { // 10 mins + swprintf(buf, 16, L"%lum %lu.%01us", secs / 60, secs % 60, usecs / 100000); + } else if (secs < 5400) { // 1.5 hours + swprintf(buf, 16, L"%lum %lus", secs / 60, secs % 60); + } else { + swprintf(buf, 16, L"%.1fh", secs / 3600.0f); + } + env_set( ENV_CMD_DURATION, buf, ENV_EXPORT ); + } +} void reader_run_command( const wchar_t *cmd ) { wchar_t *ft; + struct timeval time_before, time_after; ft= tok_first( cmd ); @@ -2178,9 +2209,14 @@ void reader_run_command( const wchar_t *cmd ) term_donate(); + gettimeofday(&time_before, NULL); + eval( cmd, 0, TOP ); job_reap( 1 ); + gettimeofday(&time_after, NULL); + set_env_cmd_duration(&time_after, &time_before); + term_steal(); env_set( L"_", program_name, ENV_GLOBAL );