diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2019-02-15 10:51:57 -0700 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2019-02-15 10:51:57 -0700 |
commit | fb141c2257f648cd29b64cbd3f2ca9123f6e427f (patch) | |
tree | a7e56fe7edad0e231aaa4ff0f6f74ce0bad1d368 /plugins/timer/timer.plugin.zsh | |
parent | 89c9e8a7bc177d4cff6ba8d28cf6b57a9a603095 (diff) | |
parent | 965a27aa69b49f4e447bcaae913e71a010f7d141 (diff) | |
download | zsh-fb141c2257f648cd29b64cbd3f2ca9123f6e427f.tar.gz zsh-fb141c2257f648cd29b64cbd3f2ca9123f6e427f.tar.bz2 zsh-fb141c2257f648cd29b64cbd3f2ca9123f6e427f.zip |
Merge branch 'master' of https://github.com/robbyrussell/oh-my-zsh
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) |