Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: time command with shell builtins



On Tue, Jan 24, 2023 at 2:48 AM Dominik Vogt <dominik.vogt@xxxxxx> wrote:
>
> [...]  This is what I use now:
>
> -- snip --
> autoload -Uz add-zsh-hook
> zmodload zsh/datetime
>
> function preexec_recordtime() {
>         typeset -g _zsh_time
>         _zsh_time="$EPOCHSECONDS"
> }
> add-zsh-hook preexec preexec_recordtime

Unless you're already using psvar for something else, I would suggest

function preexec_recordtime() {
  if [[ -n $REPORTTIME ]]; then
    psvar[1]="$EPOCHSECONDS"
  else
    psvar[1]=''
  fi
}
precmd_reporttime () {
  if [[ -n $REPORTTIME && -n $psvar[1] ]]
  then
    psvar[1]=$(( $EPOCHSECONDS - $psvar[1] ))
    if (( $psvar[1] <= $REPORTTIME ))
    then
      psvar[1]=''
    else
      psvar[1]=" $psvar[1]s "
    fi
  fi
}
add-zsh-hook preexec preexec_recordtime
add-zsh-hook precmd precmd_reporttime

PS1="<...>%1v<...>"

Out of curiosity, why are you doing math on $EPOCHSECONDS rather than
just use $SECONDS?




Messages sorted by: Reverse Date, Date, Thread, Author