Skip to content

Instantly share code, notes, and snippets.

@jedahan
Created October 29, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedahan/ec895cea2a3c42d408553b292bb8ae72 to your computer and use it in GitHub Desktop.
Save jedahan/ec895cea2a3c42d408553b292bb8ae72 to your computer and use it in GitHub Desktop.
basic() { echo '>'}
# hooktest - can we add-zsh-hook inside a hook?
_ROOT=${0:A:h}
_SEPARATOR=" "
_PROMPT=(timer basic)
# join outputs of functions
_wrap() {
local -a outputs
for cmd in ${(P)1}; do
(( $+functions[$cmd] )) || source ${_ROOT}/${cmd}.zsh
(( $+functions[$cmd] )) && output="$(eval "$cmd")"
(( $status )) || outputs+="$output"
done
echo ${(ps.$_SEPARATOR.)outputs}$_SEPARATOR
}
# Show left prompt
_prompt() { PROMPT=$(_wrap _PROMPT) }
add-zsh-hook precmd _prompt
# timer - show the elapsed time for long running commands
zmodload zsh/datetime || return # required for `$EPOCHSECONDS`
typeset -g _command_timestamp
typeset -g _command_exec_time
# Begin to track the EPOCHSECONDS since this command is executed
_set_command_timestamp() { _command_timestamp=$EPOCHSECONDS }
add-zsh-hook preexec _set_command_timestamp
# Stores the exec time of the last command if set threshold was exceeded
_check_command_exec_time() {
integer elapsed
# Clear command_exec_time to avoid double rendering
_command_exec_time=
# Set command_exec_time if elapsed time is above _PATIENCE
(( elapsed = EPOCHSECONDS - ${_command_timestamp:-$EPOCHSECONDS} ))
(( elapsed > ${_PATIENCE:=0} )) && _command_exec_time=$elapsed
# Clear timestamp
_command_timestamp=
}
add-zsh-hook precmd _check_command_exec_time
timer() { echo $_command_exec_time }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment