Skip to content

Instantly share code, notes, and snippets.

@noqcks
Created May 26, 2022 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noqcks/fcb6d0a886a4a5a447c90377d792b5e1 to your computer and use it in GitHub Desktop.
Save noqcks/fcb6d0a886a4a5a447c90377d792b5e1 to your computer and use it in GitHub Desktop.
Add growl notifications when a long running command fails or succeeds
  1. Add this to your .rc file
preexec () {
   (( $#_elapsed > 1000 )) && set -A _elapsed $_elapsed[-1000,-1]
   typeset -ig _start=SECONDS
}

# Notify about long running commands
function precmd() {
    _result=$?
    # Update elapsed time
    set -A _elapsed $_elapsed $(( SECONDS-_start ))

    if (( ( $_elapsed[-1] > 20 ) )); then
      if (( ( $_result > 0 ) )); then
        terminal-notifier -message "Command failed: $(fc -l -n -1)" -title $(realpath --relative-base=$HOME/dd -s $(pwd)) -ignoreDnD
        say "command failed"
      else
        terminal-notifier -message "Command completed" -title $(realpath --relative-base=$HOME/dd -s $(pwd)) -ignoreDnD
        say "command completed"
      fi
    fi
}
  1. Install terminal-notifier
  2. Allow alerts from your terminal program (iTerm in my case) and terminal-notifier

image

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