1
0
dotfiles/.config/zsh/lib/cmdtime.zsh

55 lines
1.0 KiB
Bash
Raw Normal View History

2021-03-21 18:25:10 +03:00
#!/bin/zsh
z-time() {
local a b elapsed result
2022-07-19 02:41:45 +03:00
2021-03-21 18:25:10 +03:00
a=${EPOCHREALTIME}
"$@"
result=$?
2022-07-19 02:41:45 +03:00
b=$(( EPOCHREALTIME - a ))
2021-03-21 18:25:10 +03:00
elapsed=$(z-ts-to-human "$b" 6)
echo 1>&2
echo "time took: ${elapsed}" 1>&2
2022-07-19 02:41:45 +03:00
2021-03-21 18:25:10 +03:00
return ${result}
}
if autoload -Uz add-zsh-hook ; then
typeset -gA ZSHU_PS
ZSHU_PS[cmd_threshold]=3
__z_cmdtime_precmd() {
2022-07-19 02:41:45 +03:00
local t x elapsed
t=${EPOCHREALTIME}
# t=${(%):-%D{%s.%9.}}
2021-03-21 18:25:10 +03:00
ZSHU_PS[elapsed]=''
(( ${+ZSHU_PS[cmd_ts]} )) || return
t=$(( t - ${ZSHU_PS[cmd_ts]} ))
unset "ZSHU_PS[cmd_ts]"
2022-07-19 02:41:45 +03:00
x=$(( ${ZSHU_PS[cmd_threshold]} + 0 ))
2021-03-21 18:25:10 +03:00
[ "$x" = '0' ] && return
x=$(( t - x ))
[ "${x:0:1}" = '-' ] && return
2022-07-19 02:41:45 +03:00
elapsed=$(z-ts-to-human "$t")
2021-03-21 18:25:10 +03:00
ZSHU_PS[elapsed]=" %f[%B%F{yellow}+${elapsed}%b%f] "
}
__z_cmdtime_preexec() {
ZSHU_PS[cmd_ts]=${EPOCHREALTIME}
2021-10-01 10:14:53 +03:00
# ZSHU_PS[cmd_ts]=${(%):-%D{%s.%9.}}
2021-03-21 18:25:10 +03:00
}
add-zsh-hook precmd __z_cmdtime_precmd
add-zsh-hook preexec __z_cmdtime_preexec
else
echo "cmd time measurement is disabled due to missing hook support" 1>&2
fi