Skip to content

Instantly share code, notes, and snippets.

@ninjabiscuit
Last active August 29, 2015 14:06
Show Gist options
  • Save ninjabiscuit/20c224243c2af4cf467e to your computer and use it in GitHub Desktop.
Save ninjabiscuit/20c224243c2af4cf467e to your computer and use it in GitHub Desktop.
preexec () {
# Note the date when the command started, in unix time.
CMD_START_DATE=$(date +%s)
# Store the command that we're running.
CMD_NAME=$1
}
precmd () {
# Proceed only if we've ran a command in the current shell.
if ! [[ -z $CMD_START_DATE ]]; then
# Note current date in unix time
CMD_END_DATE=$(date +%s)
# Store the difference between the last command start date vs. current date.
CMD_ELAPSED_TIME=$(($CMD_END_DATE - $CMD_START_DATE))
# Store an arbitrary threshold, in seconds.
CMD_NOTIFY_THRESHOLD=30
if [[ $CMD_ELAPSED_TIME -gt $CMD_NOTIFY_THRESHOLD ]]; then
# play notification if the elapsed time (in seconds) is greater than threshold
afplay ~/.dotfiles/media/program-complete.wav -v 2
# Send a notification
growlnotify -t "Program complete" -m "\"$CMD_NAME\" finished in $CMD_ELAPSED_TIME seconds."
fi
fi
}
@ninjabiscuit
Copy link
Author

Generate an alert when long running tasks have finished executing in your terminal. This example uses a "program complete" sound bite from the LCARS computer on Star Trek TNG. It also uses Growlnotify to set a desktop notification. Props to Grrrlpower for the original version of this script.

Installation

  • Place these functions in your .zshrc file
  • Download the audio file (and make sure the path to it is correct on line 19)
  • Install Growlnotify or update line 21 to use your notification program of choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment