Created
August 25, 2016 00:19
-
-
Save michaelvillar/ed466c6dc6ac0a8549ad4f9c75663c24 to your computer and use it in GitHub Desktop.
Get a OS X Notification when a slow CLI command finishes
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
# Based on http://frantic.im/notify-on-completion and https://gist.github.com/jamesmacaulay/860763 | |
# Notify on completion | |
function f_notifyme { | |
LAST_EXIT_CODE=$? | |
CMD=$(fc -ln -1) | |
terminal-notifier -title "$CMD" -message "Status code: $LAST_EXIT_CODE" & | |
} | |
save_preexec_time() { | |
export PREEXEC_CMD="$(history | tail -1 | sed 's/ *[0-9]* *//')" | |
export PREEXEC_TIME=$(date +'%s') | |
} | |
check_long_running_command() { | |
exitstatus=$? | |
stop=$(date +'%s') | |
start=${PREEXEC_TIME:-$stop} | |
let elapsed=$stop-$start | |
if [ $elapsed -gt 1 ]; then | |
f_notifyme | |
fi | |
PREEXEC_TIME= | |
PREEXEC_CMD= | |
} | |
autoload -Uz add-zsh-hook | |
add-zsh-hook preexec save_preexec_time | |
add-zsh-hook precmd check_long_running_command |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites:
brew install terminal-notifier