zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: dominik.vogt@gmx.de, zsh-users@zsh.org
Subject: Re: time command with shell builtins
Date: Wed, 25 Jan 2023 08:43:50 +0100	[thread overview]
Message-ID: <CAHYJk3TUHNHMN_hFsO3qt4P5jiwxjPFUBKkY+9JpcVVvFH0CjA@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7bRzfF=GS6q6XpFiE6jwDBo3yh32hMZ_knvZK6aLGAnGA@mail.gmail.com>

On 1/25/23, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Tue, Jan 24, 2023 at 2:48 AM Dominik Vogt <dominik.vogt@gmx.de> 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


  reply	other threads:[~2023-01-25  7:44 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-23 13:40 Dominik Vogt
2023-01-23 13:42 ` Roman Perepelitsa
2023-01-23 14:17   ` zeurkous
2023-01-23 14:23     ` Roman Perepelitsa
2023-01-23 14:40       ` zeurkous
2023-01-23 14:28   ` Dominik Vogt
2023-01-23 14:46     ` zeurkous
2023-01-23 16:31     ` Bart Schaefer
2023-01-23 18:31     ` Mikael Magnusson
2023-01-23 18:49       ` Dominik Vogt
2023-01-24  9:32         ` Mikael Magnusson
2023-01-24 10:48           ` Dominik Vogt
2023-01-24 23:12             ` Dominik Vogt
2023-01-24 23:36               ` Bart Schaefer
     [not found]                 ` <Y9B7A8dWLiZNXKfW@localhost>
2023-01-26 16:23                   ` Dominik Vogt
2023-01-26 16:56                     ` Bart Schaefer
2023-01-26 17:26                       ` Dominik Vogt
2023-01-26 17:40                         ` Bart Schaefer
2023-02-02 18:10                     ` Dominik Vogt
2023-02-02 18:28                       ` Bart Schaefer
2023-02-02 19:15                         ` Dominik Vogt
2023-02-02 19:15                           ` Dominik Vogt
2023-02-02 19:31                           ` Bart Schaefer
2023-01-24 23:32             ` Bart Schaefer
2023-01-25  7:43               ` Mikael Magnusson [this message]
2023-01-25 12:58                 ` Dominik Vogt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHYJk3TUHNHMN_hFsO3qt4P5jiwxjPFUBKkY+9JpcVVvFH0CjA@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=dominik.vogt@gmx.de \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).