Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: time command with shell builtins
- X-seq: zsh-users 28890
- From: Dominik Vogt <dominik.vogt@xxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: time command with shell builtins
- Date: Thu, 2 Feb 2023 20:15:00 +0100
- Archived-at: <https://zsh.org/users/28890>
- In-reply-to: <CAH+w=7avOPPgi-bJJBR-XjvUfapTcfDOhPJkMxFDHEOHPb63gw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: Zsh Users <zsh-users@xxxxxxx>
- References: <CAHYJk3QD8NbCqt0LdXshiVf6W3GQLnDzo6-1mj0qCJOTkHFGRA@mail.gmail.com> <Y87Ws8PrZY8W3Oao@localhost> <CAHYJk3Tu5POCU08-g1dr2p284p89Xak5XjLSV6J8gWdQnOXDeA@mail.gmail.com> <Y8+3h5OXqtpkyewM@localhost> <Y9BlzD/hb4bwHAmb@localhost> <CAH+w=7Z3Srhc02WEmBv_m6YiGUm4mPw2oxNqfTAcm6kqm+GE=w@mail.gmail.com> <Y9B7A8dWLiZNXKfW@localhost> <Y9Ko+7GqksX+LKww@localhost> <Y9v8kuT3/Qyzxtj7@localhost> <CAH+w=7avOPPgi-bJJBR-XjvUfapTcfDOhPJkMxFDHEOHPb63gw@mail.gmail.com>
- Reply-to: dominik.vogt@xxxxxx
- Ui-outboundreport: notjunk:1;M01:P0:p/KjMs2csMw=;1L9k8Ebyvb/NACeUjvz+gpJZsd/ QzhqyM5H0Z/e1goICZ8l9rZLTofFBLkvF2KxJYWqyWPyMLE9AdAWMpowRAiXYF2bBTNOPf1E8 /2drlOP4HRiT6iRXZ3UU67tniXyBS04S1qAQs+/GdRzhYBVip3r+JamP8ppUKcTEh7XmklCh6 jH08N6nggN0HRuMaAYaIDMc+NVSSxfs9FMc+btZQli8e3ABalDYB4Zi+rVAZx+HUJN4+oIqHF UoKJ3MnB/OguelQPPKuXPdMCIxIjqOMxy5iorDOGZQLKdgcZERzHMFpfy4xxmFWSXlyUEvVt6 HyKX1A/efA4VYmmf/KFQ0Vnro3vtYKDJIvTQIVjT+Uch74JzBGJ1OVE9+iWF4nPFfnb7bgLbT mdug3r+u2x1N7MutYXwmxizlfyx9aAZsIfrby7ZneUYBeCG6Z0GHIY9jgkmhkNd1xsXgS02zn vDwwX34gaJC9q4C/tfiY2spZQkUynU0uPheyjjsaH+enp1MxGuNBaClIZ898U/8JwdepLXMfu N90FFqH1IKgxnQsbSAcAKJpUb3Sb+bz4j8PrUZPcLc5rWgBLUOV/DXCi0BFGQIqiqyWjpFR0R zr8sBwPc36MbFUQnBKQdEDdPCaQtNFAhgKkaVB3KlhwhHfKw943LlYKK3J4CDLY8a3jnoqxbB TY8kkq3uKiTcYBCyi9zpendH8YKQuMz2WY3xxqd+6RZCqteQiJPTTYUhZwrCSwfX+Pwvp81Or +8Bf53xyp6WC2psEGbXucJ3DgWuHW2GjyVdN9mDqNcjDZgmeNpOHFvU3aVTA4ZSMbgfRkHicD XA2Sd7bFljLqUEyzDVPQ9NtaJDEOqQQZhtPHUsDSbAw/1mVBu3gBEGnLvoRlrLVQtRsfmz92O emumMEBjAK0jaB2Z/VGUJ6QIQqsbTJPXKMAUzPGiMTNBLkcDONnk69qBkiOuu5AvAiZd5v7l9 gRG13bZkwQ4SKcnVhZilMF18UmE=
On Thu, Feb 02, 2023 at 10:28:36AM -0800, Bart Schaefer wrote:
> On Thu, Feb 2, 2023 at 10:10 AM Dominik Vogt <dominik.vogt@xxxxxx> wrote:
> >
> > Well, is there a way to detect inside preexec() wether a command
> > is handled by the shell or some external binary?
>
> If you actually have the command name, you can use
> if (( $+commands[$thename] ))
>
> That won't work (or will be complicated) if the buffer that's about to
> be executed is a complex structure (braces, subshell parens,
> while/for/repeat/if/case/select/etc.).
Okay, that sounds good enough. The new solution works without a
blacklist. It may still print statistics with REPORTTIME and on
the prompt in some cases (e.g. "{ /usr/bin/foobar" }" etc.). But
I can live with that. Note that it now uses two slots in the
psvar array. In some scenarios with ctrl-c using psvar[2] for the
raw time and the string to print resulted in error messages
because the variable's value was just ' s'.
-- snip --
autoload -Uz add-zsh-hook
zmodload zsh/datetime
function preexec_recordtime() {
local CL=( $3 )
psvar[3]=''
if (( $+commands[${CL[1]}] )); then
psvar[2]=''
elif [[ -n $REPORTTIME ]]; then
psvar[2]="$EPOCHSECONDS"
else
psvar[2]=''
fi
}
precmd_reporttime () {
if [[ -n "$REPORTTIME" && -n "$psvar[2]" ]]; then
psvar[2]=$(( $EPOCHSECONDS - $psvar[2] ))
if (( $psvar[2] <= $REPORTTIME )); then
psvar[3]=''
else
psvar[3]=" $psvar[2]s"
fi
fi
}
add-zsh-hook preexec preexec_recordtime
add-zsh-hook precmd precmd_reporttime
PS1="...%3v..."
-- snip --
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
Messages sorted by:
Reverse Date,
Date,
Thread,
Author