From 96d10e2147b59adc25e9c3b90ac6f31935495ef3 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Fri, 13 Nov 2015 23:42:21 -0500 Subject: [PATCH 1/9] calculating command's execution time --- plugins/timer/timer.plugin.zsh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 plugins/timer/timer.plugin.zsh diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh new file mode 100644 index 000000000..aa4573b9f --- /dev/null +++ b/plugins/timer/timer.plugin.zsh @@ -0,0 +1,18 @@ +preexec() { + __timer_cmd_start_time=$(date '+%s') +} + +precmd() { + if [ -n "${__timer_cmd_start_time}" ]; then + local cmd_end_time=$(date '+%s') + local tdiff=$((${cmd_end_time} - ${__timer_cmd_start_time})) + unset __timer_cmd_start_time + local tdiffstr='/' + if (( tdiff >= 60 )); then + tdiffstr+="$((tdiff / 60))m" + fi + tdiffstr+="$((tdiff % 60))s" + local cols=$(($COLUMNS - ${#tdiffstr} - 1)) + echo -e "\033[1A\033[${cols}C ${tdiffstr}" + fi +} From 1b8f05a3d395c107d81724445cac7fcd599942d8 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Sat, 14 Nov 2015 23:13:44 -0500 Subject: [PATCH 2/9] simplified time string calculation --- plugins/timer/timer.plugin.zsh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index aa4573b9f..f73d2ab53 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -7,12 +7,8 @@ precmd() { local cmd_end_time=$(date '+%s') local tdiff=$((${cmd_end_time} - ${__timer_cmd_start_time})) unset __timer_cmd_start_time - local tdiffstr='/' - if (( tdiff >= 60 )); then - tdiffstr+="$((tdiff / 60))m" - fi - tdiffstr+="$((tdiff % 60))s" - local cols=$(($COLUMNS - ${#tdiffstr} - 1)) - echo -e "\033[1A\033[${cols}C ${tdiffstr}" + local tdiffstr="$((tdiff / 60))m$((tdiff % 60))s" + local cols=$(($COLUMNS - ${#tdiffstr#0m} - 2)) + echo -e "\033[1A\033[${cols}C \`${tdiffstr#0m}" fi } From 120e8620af6e1d7d01159614186403c7a816457d Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Sat, 14 Nov 2015 23:48:26 -0500 Subject: [PATCH 3/9] cleaning up --- plugins/timer/timer.plugin.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index f73d2ab53..ee2cb66c1 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -3,12 +3,12 @@ preexec() { } precmd() { - if [ -n "${__timer_cmd_start_time}" ]; then + if [ -n "$__timer_cmd_start_time" ]; then local cmd_end_time=$(date '+%s') - local tdiff=$((${cmd_end_time} - ${__timer_cmd_start_time})) + local tdiff=$((cmd_end_time - __timer_cmd_start_time)) unset __timer_cmd_start_time local tdiffstr="$((tdiff / 60))m$((tdiff % 60))s" - local cols=$(($COLUMNS - ${#tdiffstr#0m} - 2)) + local cols=$((COLUMNS - ${#tdiffstr#0m} - 2)) echo -e "\033[1A\033[${cols}C \`${tdiffstr#0m}" fi } From d4c74690b61b74e317410df3b80b784891ab55ed Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Sun, 15 Nov 2015 22:08:16 -0500 Subject: [PATCH 4/9] increased timer's pecision --- plugins/timer/timer.plugin.zsh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index ee2cb66c1..729dd3ee2 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -1,14 +1,25 @@ +__timer_current_time() { + perl -MTime::HiRes=time -e'print time' +} + +__timer_format_duration() { + local mins=$(printf '%.0f' $(($1 / 60))) + local secs=$(printf '%.1f' $(($1 - 60 * mins))) + local duration_str=$(echo "${mins}m${secs}s") + echo "\`${duration_str#0m}" +} + preexec() { - __timer_cmd_start_time=$(date '+%s') + __timer_cmd_start_time=$(__timer_current_time) } precmd() { - if [ -n "$__timer_cmd_start_time" ]; then - local cmd_end_time=$(date '+%s') + if [ -n "${__timer_cmd_start_time}" ]; then + local cmd_end_time=$(__timer_current_time) local tdiff=$((cmd_end_time - __timer_cmd_start_time)) unset __timer_cmd_start_time - local tdiffstr="$((tdiff / 60))m$((tdiff % 60))s" - local cols=$((COLUMNS - ${#tdiffstr#0m} - 2)) - echo -e "\033[1A\033[${cols}C \`${tdiffstr#0m}" + local tdiffstr=$(__timer_format_duration ${tdiff}) + local cols=$((COLUMNS - ${#tdiffstr} - 1)) + echo -e "\033[1A\033[${cols}C ${tdiffstr}" fi } From 111dd018b9b821fad2c7899b31aa46b5c0aaa218 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Mon, 16 Nov 2015 22:20:26 -0500 Subject: [PATCH 5/9] allow changes in display format --- plugins/timer/timer.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index 729dd3ee2..33481ea4e 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -4,9 +4,9 @@ __timer_current_time() { __timer_format_duration() { local mins=$(printf '%.0f' $(($1 / 60))) - local secs=$(printf '%.1f' $(($1 - 60 * mins))) + local secs=$(printf "%.${TIMER_PRECISION:-1}f" $(($1 - 60 * mins))) local duration_str=$(echo "${mins}m${secs}s") - echo "\`${duration_str#0m}" + echo "${TIMER_SYMBOL:-\`}${duration_str#0m}" } preexec() { From 96148d2275b848dbfb976a58967535067def4210 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Tue, 17 Nov 2015 20:50:30 -0500 Subject: [PATCH 6/9] customizable timer format --- plugins/timer/timer.plugin.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index 33481ea4e..f7f039b78 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -6,7 +6,8 @@ __timer_format_duration() { local mins=$(printf '%.0f' $(($1 / 60))) local secs=$(printf "%.${TIMER_PRECISION:-1}f" $(($1 - 60 * mins))) local duration_str=$(echo "${mins}m${secs}s") - echo "${TIMER_SYMBOL:-\`}${duration_str#0m}" + local format="${TIMER_FORMAT:-/%d}" + echo "${format//\%d/${duration_str#0m}}" } preexec() { From de8d6841b00903f2873a8b009faf7002bfbc1273 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Wed, 18 Nov 2015 20:33:38 -0500 Subject: [PATCH 7/9] added pre_functions --- plugins/timer/timer.plugin.zsh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index f7f039b78..231134e7d 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -10,11 +10,11 @@ __timer_format_duration() { echo "${format//\%d/${duration_str#0m}}" } -preexec() { +__timer_save_time_preexec() { __timer_cmd_start_time=$(__timer_current_time) } -precmd() { +__timer_display_timer_precmd() { if [ -n "${__timer_cmd_start_time}" ]; then local cmd_end_time=$(__timer_current_time) local tdiff=$((cmd_end_time - __timer_cmd_start_time)) @@ -24,3 +24,6 @@ precmd() { echo -e "\033[1A\033[${cols}C ${tdiffstr}" fi } + +preexec_functions+=(__timer_save_time_preexec) +precmd_functions+=(__timer_display_timer_precmd) From d615961430aca9668cfb056aef6024c4ed41eff8 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Wed, 18 Nov 2015 21:09:18 -0500 Subject: [PATCH 8/9] readme file --- plugins/timer/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 plugins/timer/README.md diff --git a/plugins/timer/README.md b/plugins/timer/README.md new file mode 100644 index 000000000..19072cb24 --- /dev/null +++ b/plugins/timer/README.md @@ -0,0 +1,18 @@ +This plugin allows to display command's execution time in a very nonintrusive way. + +Timer can be tuned by these two variables: +* `TIMER_PRECISION` allows to control number of decimal places (default `1`) +* `TIMER_FORMAT` allows to adjust display format (default `'/%d'`) + +Sample session: + + me@here:~$ sleep 1 /1.0s + me@here:~$ sleep 73 /1m13.0s + me@here:~$ TIMER_FORMAT='[%d]'; TIMER_PRECISION=2 [0.00s] + me@here:~$ head -c50 < /dev/urandom | hexdump + 0000000 b2 16 20 f0 29 1f 61 2d 8a 29 20 8c 8c 39 5a ab + 0000010 21 47 0e f9 ee a4 76 46 71 9e 4f 6b a4 c4 51 cb + 0000020 f9 1f 7e b9 6f 2c ae dd cf 40 6d 64 a8 fb d3 db + 0000030 09 37 + 0000032 [0.02s] + From 7553bcb4185b9d584a40b41cf15501e43041fe57 Mon Sep 17 00:00:00 2001 From: Robert Strack Date: Wed, 18 Nov 2015 21:27:29 -0500 Subject: [PATCH 9/9] minor corrections in the readme file --- plugins/timer/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/timer/README.md b/plugins/timer/README.md index 19072cb24..321307e59 100644 --- a/plugins/timer/README.md +++ b/plugins/timer/README.md @@ -8,11 +8,10 @@ Sample session: me@here:~$ sleep 1 /1.0s me@here:~$ sleep 73 /1m13.0s - me@here:~$ TIMER_FORMAT='[%d]'; TIMER_PRECISION=2 [0.00s] + me@here:~$ TIMER_FORMAT='[%d]'; TIMER_PRECISION=2 [0.00s] me@here:~$ head -c50 < /dev/urandom | hexdump 0000000 b2 16 20 f0 29 1f 61 2d 8a 29 20 8c 8c 39 5a ab 0000010 21 47 0e f9 ee a4 76 46 71 9e 4f 6b a4 c4 51 cb 0000020 f9 1f 7e b9 6f 2c ae dd cf 40 6d 64 a8 fb d3 db 0000030 09 37 0000032 [0.02s] -