Created
February 1, 2017 04:20
-
-
Save oysta/dede7e67059d95e79cfc4b77eea8c4fa to your computer and use it in GitHub Desktop.
macOS notification and terminal bell on completion of long running zsh process
This file contains hidden or 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
#!/usr/bin/env zsh | |
# This script prints a bell character and displays a notification when a command finishes | |
# if it has been running for longer than $zbell_duration seconds. | |
# If there are programs that you know run long that you don't | |
# want to bell after, then add them to $zbell_ignore. | |
# | |
# It requires the terminal-notifier ruby gem (gem install terminal-notifier) | |
# | |
# Written by Jean-Philippe Ouellet <[email protected]> | |
# Made available under the ISC license. | |
# | |
# Some modifications by Christopher Owen <[email protected]> | |
# 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} )) || zbell_duration=15 | |
# initialize zbell_ignore if not set | |
(( ${+zbell_ignore} )) || zbell_ignore=($EDITOR $PAGER ping) | |
# initialize it because otherwise we compare a date and an empty string | |
# the first time we see the prompt. it's fine to have lastcmd empty on the | |
# initial run because it evaluates to an empty string, and splitting an | |
# empty string just results in an empty array. | |
zbell_timestamp=$EPOCHSECONDS | |
# right before we begin to execute something, store the time it started at | |
zbell_begin() { | |
zbell_timestamp=$EPOCHSECONDS | |
zbell_lastcmd=$1 | |
} | |
# when it finishes, if it's been running longer than $zbell_duration, | |
# and we dont have an ignored command in the line, then print a bell. | |
zbell_end() { | |
proc_stat="$?" | |
has_ignored_cmd=0 | |
if [[ $proc_stat = "130" ]]; then | |
has_ignored_cmd=1 | |
fi | |
elapsed_time=$(( $EPOCHSECONDS - $zbell_timestamp )) | |
ran_long=$(( $elapsed_time >= $zbell_duration )) | |
for cmd in ${(s:;:)zbell_lastcmd//|/;}; do | |
words=(${(z)cmd}) | |
util=${words[1]} | |
if (( ${zbell_ignore[(i)$util]} <= ${#zbell_ignore} )); then | |
has_ignored_cmd=1 | |
break | |
fi | |
done | |
if (( ! $has_ignored_cmd )) && (( ran_long )); then | |
display_elapsed=$(date -u -r ${elapsed_time} +%T) | |
terminal-notifier -title znote -activate com.apple.terminal -group "$TERM_SESSION_ID" -message "${util} completed (⏱ ${display_elapsed})" | |
print -n "\a" | |
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