我想测量一个程序在 zsh 中运行所花费的时间。根据时差,我想打印出所花费的时间。我为此编辑了接受行挂钩函数:
my-accept-line () {
CMD="$BUFFER"
CMDSTART=$(date +%s)
zle accept-line
CMDRUNTIME=$((($(date +%s)-${CMDSTART})))
if [[ $CMDRUNTIME -ge 100 ]]; then
CMDRUNTIME_min=$(($CMDRUNTIME/60))
echo "Last command ran for $CMDRUNTIME_min minutes."
fi
}
# create a widget from `my-accept-line' with the same name
zle -N my-accept-line
# rebind Enter, usually this is `^M'
bindkey '^M' my-accept-line
但是,问题是“zle accept-line”似乎在后台运行。因此,我总是将 CMDRUNTIME 设为 0。是否有解决方法?