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

Re: Exporting REPORTTIME data to ENVVARs instead of printing.



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}
}



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