Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sound a bell when long-running commands complete
# 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
# 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() { terminal-notifier -message "job is done: $zbell_lastcmd" -sound default }
zbell_end() {
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
You can’t perform that action at this time.