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

54 lines
929 B
Bash
Raw Permalink 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=$?
2024-01-27 00:10:58 +03:00
a=$[ EPOCHREALTIME - a ]
2024-01-16 02:55:39 +03:00
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-27 00:10:58 +03:00
ZSHU_PS[cmd_threshold]=3
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-27 00:10:58 +03:00
t=$[ x - ZSHU[cmd_ts] ]
2024-01-16 02:55:39 +03:00
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]}
2024-01-27 00:10:58 +03:00
x=$[ x + 0 ] || x=0
2024-01-16 02:55:39 +03:00
[ "$x" = 0 ] && return
2021-03-21 18:25:10 +03:00
2024-01-27 00:10:58 +03:00
x=$[ t - x ]
2021-03-21 18:25:10 +03:00
[ "${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