timer.plugin.zsh 1019 B

1234567891011121314151617181920212223242526272829303132
  1. __timer_current_time() {
  2. perl -MTime::HiRes=time -e'print time'
  3. }
  4. __timer_format_duration() {
  5. local mins=$(printf '%.0f' $(($1 / 60)))
  6. local secs=$(printf "%.${TIMER_PRECISION:-1}f" $(($1 - 60 * mins)))
  7. local duration_str=$(echo "${mins}m${secs}s")
  8. local format="${TIMER_FORMAT:-/%d}"
  9. echo "${format//\%d/${duration_str#0m}}"
  10. }
  11. __timer_save_time_preexec() {
  12. __timer_cmd_start_time=$(__timer_current_time)
  13. }
  14. __timer_display_timer_precmd() {
  15. if [ -n "${__timer_cmd_start_time}" ]; then
  16. local cmd_end_time=$(__timer_current_time)
  17. local tdiff=$((cmd_end_time - __timer_cmd_start_time))
  18. unset __timer_cmd_start_time
  19. if [[ -z "${TIMER_THRESHOLD}" || ${tdiff} -ge "${TIMER_THRESHOLD}" ]]; then
  20. local tdiffstr=$(__timer_format_duration ${tdiff})
  21. local cols=$((COLUMNS - ${#tdiffstr} - 1))
  22. echo -e "\033[1A\033[${cols}C ${tdiffstr}"
  23. fi
  24. fi
  25. }
  26. autoload -U add-zsh-hook
  27. add-zsh-hook preexec __timer_save_time_preexec
  28. add-zsh-hook precmd __timer_display_timer_precmd