diff options
author | Robby Russell <robby@planetargon.com> | 2019-02-13 11:24:49 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-13 11:24:49 -0800 |
commit | e8318e4abe23211c5c8f08993db42ef917821577 (patch) | |
tree | e549848524d1227125effe65a2e7ac205cd1909e /plugins/timer/timer.plugin.zsh | |
parent | 6cec86203a1afc918034470184007c0a23ad1260 (diff) | |
parent | 7553bcb4185b9d584a40b41cf15501e43041fe57 (diff) | |
download | zsh-e8318e4abe23211c5c8f08993db42ef917821577.tar.gz zsh-e8318e4abe23211c5c8f08993db42ef917821577.tar.bz2 zsh-e8318e4abe23211c5c8f08993db42ef917821577.zip |
Merge pull request #4627 from strackr/timer_plugin
Adding Timer plugin
Diffstat (limited to 'plugins/timer/timer.plugin.zsh')
-rw-r--r-- | plugins/timer/timer.plugin.zsh | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh new file mode 100644 index 000000000..231134e7d --- /dev/null +++ b/plugins/timer/timer.plugin.zsh @@ -0,0 +1,29 @@ +__timer_current_time() { + perl -MTime::HiRes=time -e'print time' +} + +__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") + local format="${TIMER_FORMAT:-/%d}" + echo "${format//\%d/${duration_str#0m}}" +} + +__timer_save_time_preexec() { + __timer_cmd_start_time=$(__timer_current_time) +} + +__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)) + unset __timer_cmd_start_time + local tdiffstr=$(__timer_format_duration ${tdiff}) + local cols=$((COLUMNS - ${#tdiffstr} - 1)) + echo -e "\033[1A\033[${cols}C ${tdiffstr}" + fi +} + +preexec_functions+=(__timer_save_time_preexec) +precmd_functions+=(__timer_display_timer_precmd) |