]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/timer/timer.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / timer / timer.plugin.zsh
1 zmodload zsh/datetime
2
3 __timer_current_time() {
4   zmodload zsh/datetime
5   echo $EPOCHREALTIME
6 }
7
8 __timer_format_duration() {
9   local mins=$(printf '%.0f' $(($1 / 60)))
10   local secs=$(printf "%.${TIMER_PRECISION:-1}f" $(($1 - 60 * mins)))
11   local duration_str=$(echo "${mins}m${secs}s")
12   local format="${TIMER_FORMAT:-/%d}"
13   echo "${format//\%d/${duration_str#0m}}"
14 }
15
16 __timer_save_time_preexec() {
17   __timer_cmd_start_time=$(__timer_current_time)
18 }
19
20 __timer_display_timer_precmd() {
21   if [ -n "${__timer_cmd_start_time}" ]; then
22     local cmd_end_time=$(__timer_current_time)
23     local tdiff=$((cmd_end_time - __timer_cmd_start_time))
24     unset __timer_cmd_start_time
25     if [[ -z "${TIMER_THRESHOLD}" || ${tdiff} -ge "${TIMER_THRESHOLD}" ]]; then
26         local tdiffstr=$(__timer_format_duration ${tdiff})
27         local cols=$((COLUMNS - ${#tdiffstr} - 1))
28         echo -e "\033[1A\033[${cols}C ${tdiffstr}"
29     fi
30   fi
31 }
32
33 autoload -U add-zsh-hook
34 add-zsh-hook preexec __timer_save_time_preexec
35 add-zsh-hook precmd __timer_display_timer_precmd