Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: time command with shell builtins
- X-seq: zsh-users 28778
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: dominik.vogt@xxxxxx, zsh-users@xxxxxxx
- Subject: Re: time command with shell builtins
- Date: Wed, 25 Jan 2023 08:43:50 +0100
- Archived-at: <https://zsh.org/users/28778>
- In-reply-to: <CAH+w=7bRzfF=GS6q6XpFiE6jwDBo3yh32hMZ_knvZK6aLGAnGA@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <Y86OR0DCpwfSYCXU@localhost> <CAN=4vMqSxEr-K1xg+fGc9kh2qfiZ31fy7hoDBRNHXXF6pH8eEg@mail.gmail.com> <Y86ZkYooJ9Kf4xCE@localhost> <CAHYJk3QD8NbCqt0LdXshiVf6W3GQLnDzo6-1mj0qCJOTkHFGRA@mail.gmail.com> <Y87Ws8PrZY8W3Oao@localhost> <CAHYJk3Tu5POCU08-g1dr2p284p89Xak5XjLSV6J8gWdQnOXDeA@mail.gmail.com> <Y8+3h5OXqtpkyewM@localhost> <CAH+w=7bRzfF=GS6q6XpFiE6jwDBo3yh32hMZ_knvZK6aLGAnGA@mail.gmail.com>
On 1/25/23, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> 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?
I also do this in precmd/exec, so I figured I may as well use the same
thing for all of it, also if I assign to SECONDS interactively maybe
weird things happen wrt float formatting etc,
function pre{exec,cmd}_datechange() {
local newdate
newdate=$(strftime %x $EPOCHSECONDS)
if [[ $_zsh_date != $newdate ]]; then
printf "Date changed from %s to %s\n" $_zsh_date $newdate
_zsh_date=$newdate
fi
}
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author