Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Last active January 22, 2025 17:35
Show Gist options
  • Save baroquebobcat/8921fb917d9cdec93e32 to your computer and use it in GitHub Desktop.
Save baroquebobcat/8921fb917d9cdec93e32 to your computer and use it in GitHub Desktop.
export NOTIFY_TIME=10
function timer_start {
previous_command=$this_command
if [ "$BASH_COMMAND" != "timer_stop" ]
then
this_command=$BASH_COMMAND
fi
timer=${timer:-$SECONDS}
}
function timer_stop {
timer_show=$(($SECONDS - $timer))
unset timer
# for commands that take > NOTIFY_TIME seconds, notify me
if [ $timer_show -gt $NOTIFY_TIME ]; then
if [ $? -eq 0 ]; then
status="Succeeded"
else
status="Failed"
fi
prompt_location="${PWD/#$HOME/\~} \$"
title="$prompt_location $(echo $previous_command | awk '{print $1}') $status"
terminal-notifier -title "$title" -message "$this_command" -sender "$prompt_location"
fi
}
trap 'timer_start' DEBUG
PROMPT_COMMAND=timer_stop
# https://www.reddit.com/r/zsh/comments/46lf65/ohmyzsh_how_can_i_see_how_much_time_the_last/
function preexec() {
timer=${timer:-$SECONDS}
}
function precmd() {
if [ $timer ]; then
timer_show=$(($SECONDS - $timer))
timer_show=$(printf '%.*f\n' 3 $timer_show)
export RPROMPT="[%F{$hcolor}%?%F{$dcolor}] : %F{$hcolor}${timer_show}s %F{$dcolor}"
unset timer
fi
}
# https://github.com/popstas/zsh-command-time
# REPORTTIME
# If nonnegative, commands whose combined user and system execution times (measured in seconds) are greater than this value have timing statistics printed for them. Output is suppressed for commands executed within the line editor, including completion; commands explicitly marked with the time keyword still cause the summary to be printed in this case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment