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

54 lines
940 B
Bash
Raw Normal View History

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