Last active
December 22, 2021 23:41
-
-
Save MicahElliott/97df9ca799e49c0fcc0a981bf021f813 to your computer and use it in GitHub Desktop.
Sound a bell when long-running commands complete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original: https://gist.github.com/oknowton/8346801 | |
# Add to zplug: | |
# zplug MicahElliott/97df9ca799e49c0fcc0a981bf021f813, from:gist, as:plugin, use:zbell-long-cmd.zsh | |
# brew install terminal-notifier | |
# only do this if we're in an interactive shell | |
[[ -o interactive ]] || return | |
# get $EPOCHSECONDS. builtins are faster than date(1) | |
zmodload zsh/datetime || return | |
# make sure we can register hooks | |
autoload -Uz add-zsh-hook || return | |
# find notifier | |
case "$OSTYPE" in | |
linux*) notifier() { | |
if [[ $zbell_cmd_es != 0 ]]; then | |
notify-send $1 $2 -u critical | |
else | |
notify-send $1 $2 | |
fi | |
} ;; | |
darwin*) notifier() { terminal-notifier -sound default -title $1 -message $2 } ;; | |
esac | |
# initialize zbell_duration if not set | |
zbell_duration=60 | |
# initialize it because otherwise we compare a date and an empty string | |
# the first time we see the prompt. | |
zbell_timestamp=$EPOCHSECONDS | |
# right before we begin to execute something, store the time it started at | |
zbell_begin() { zbell_timestamp=$EPOCHSECONDS; zbell_lastcmd=$1 } | |
zbell_noise() { | |
notifier 'longrunning' "job is done: (es:$zbell_cmd_es) $zbell_lastcmd" $zbell_cmd_es | |
} | |
zbell_end() { | |
zbell_cmd_es=$? | |
ran_long=$(( $EPOCHSECONDS - $zbell_timestamp >= $zbell_duration )) | |
: print duration: $(( $EPOCHSECONDS - $zbell_timestamp )) | |
if [[ $zbell_last_timestamp == $zbell_timestamp ]]; then | |
: print 'that was a quick one' | |
return | |
fi | |
zbell_last_timestamp=$zbell_timestamp | |
if (( ran_long )); then | |
local zbell_cmd_duration | |
zbell_cmd_duration=$(( $EPOCHSECONDS - $zbell_timestamp )) | |
if [[ $zbell_cmd_duration -gt $zbell_duration ]]; then | |
zbell_noise | |
fi | |
zbell_noise | |
# notify-send "Job completed on $HOST:" "$zbell_lastcmd" | |
: print -n "\a" | |
else | |
: print 'hmm, maybe not long' | |
fi | |
} | |
# register the functions as hooks | |
add-zsh-hook preexec zbell_begin | |
add-zsh-hook precmd zbell_end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment