From: Jan Larres <jan@majutsushi.net>
To: zsh-workers@zsh.org
Subject: Re: Exporting REPORTTIME data to ENVVARs instead of printing.
Date: Tue, 6 Oct 2015 13:41:15 +1300 [thread overview]
Message-ID: <muv5bc$o75$1@ger.gmane.org> (raw)
In-Reply-To: <151005113109.ZM19301@torch.brasslantern.com>
On 06/10/15 07:31, Bart Schaefer wrote:
> On Oct 5, 2:23pm, Slava Barinov wrote:
> } Subject: Exporting REPORTTIME data to ENVVARs instead of printing.
> }
> } REPORTTIME feature only can print timings to terminal but I have a
> } great prompt with much information embedded and still having some
> } space to add this info.
> }
> } My suggestion is to add possibility of exporting timings to envvars
> } instead of printing them to terminal.
>
> It would be more in keeping with similar features if the values were
> placed in an array or hash rather than in four separate variables.
> Or in a single scalar but formatted with $TIMEFMT.
>
> However, if the primary motivation is to include them in the prompt
> string, a prompt substitution escape might be more appropriate.
I recently wrote some functionality that reimplements REPORTTIME as a zsh
script to make it more flexible. It can show the duration of the last 10
commands together with their start and stop times and the exit code:
$ timerecords
0 13:36:14 - 13:36:14 0 echo foo
5 13:36:16 - 13:36:21 0 sleep 5
0 13:36:28 - 13:36:28 0 echo bar
$
It could easily be adapted to also provide the information in a way that could
be used in a prompt.
-Jan
zmodload zsh/datetime
typeset -g -a __last_cmds
typeset -g __last_cmd
typeset -g __last_cmd_time
_record_cmd() {
__last_cmd="$1"
__last_cmd_time=$(print -P "%D{%s}")
}
add-zsh-hook preexec _record_cmd
_process_cmd_time() {
[[ -z "$__last_cmd" ]] && return
local curtime=$(print -P "%D{%s}")
local timedelta=$(( curtime - __last_cmd_time ))
local hours=$(( timedelta / 3600 ))
local minutes=$(( timedelta / 60 % 60 ))
local seconds=$(( timedelta % 60 ))
local entry=""
if (( $hours == 0 )) && (( $minutes == 0 )); then
entry+="${(l:8:)seconds}"
elif (( $hours == 0 )); then
entry+="${(l:5:)minutes}:${(l:2::0:)seconds}"
else
entry+="${(l:2:)hours}:${(l:2::0:)minutes}:${(l:2::0:)seconds}"
fi
entry+=" $(strftime '%T' $__last_cmd_time) - $(strftime '%T' $curtime) $__last_cmd_exitstatus $__last_cmd"
__last_cmds+=( $entry )
(( ${#__last_cmds} > 10 )) && shift __last_cmds
}
add-zsh-hook precmd _process_cmd_time
_clear_last_cmd() {
__last_cmd=""
}
add-zsh-hook precmd _clear_last_cmd
timerecords() {
print -l ${__last_cmds}
}
prev parent reply other threads:[~2015-10-06 0:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-05 11:23 Slava Barinov
2015-10-05 18:31 ` Bart Schaefer
2015-10-06 0:41 ` Jan Larres [this message]
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='muv5bc$o75$1@ger.gmane.org' \
--to=jan@majutsushi.net \
--cc=zsh-workers@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).