Skip to content

Instantly share code, notes, and snippets.

@tyzbit
Last active April 15, 2023 23:27
Show Gist options
  • Save tyzbit/5f7913f95699fb6f37d7c726ff7a5232 to your computer and use it in GitHub Desktop.
Save tyzbit/5f7913f95699fb6f37d7c726ff7a5232 to your computer and use it in GitHub Desktop.
zsh RPROMPT with execution time and current time on the right hand side
# refreshes the prompt every $TMOUT seconds
TMOUT=15
function preexec() {
timer=$(($(date +%s%0N)/1000000))
}
function precmd() {
if [ $timer ]; then
now=$(($(date +%s%0N)/1000000))
calculated=$(( $now-$timer ))
hours=$(( $calculated/1000/3600 ))
min=$(( $calculated/1000/60 ))
sec=$(( ($calculated/1000)%60 ))
if [[ "${calculated}" -le 1000 ]]; then
elapsed="%F{magenta}${calculated}ms"
elif [[ "${calculated}" -le 60000 ]]; then
elapsed="%F{green}$(( ${calculated} / 1000 ))s"
else
if [ "$hours" -gt 0 ]; then
min="$(( $min%60 ))"
elapsed="%F{red}${hours}h${min}m${sec}s"
else
elapsed="%F{yellow}${min}m${sec}s"
fi
fi
export RPROMPT="[${elapsed}%f] [%F{green}%D{%Y/%m/%d}%f|%F{cyan}%D{%H:%M}%f]"
unset timer
fi
}
function TRAPALRM() {
case "$WIDGET" in
expand-or-complete|self-insert|up-line-or-beginning-search|down-line-or-beginning-search|backward-delete-char|.history-incremental-search-backward|.history-incremental-search-forward|fzf-history-widget)
:
;;
*)
zle reset-prompt
;;
esac
}
@tyzbit
Copy link
Author

tyzbit commented Aug 26, 2020

For macs: install coreutils and then change all dates to gdate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment