summaryrefslogtreecommitdiff
path: root/plugins/timer/timer.plugin.zsh
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2019-02-15 10:51:57 -0700
committerTuowen Zhao <ztuowen@gmail.com>2019-02-15 10:51:57 -0700
commitfb141c2257f648cd29b64cbd3f2ca9123f6e427f (patch)
treea7e56fe7edad0e231aaa4ff0f6f74ce0bad1d368 /plugins/timer/timer.plugin.zsh
parent89c9e8a7bc177d4cff6ba8d28cf6b57a9a603095 (diff)
parent965a27aa69b49f4e447bcaae913e71a010f7d141 (diff)
downloadzsh-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.zsh29
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)