bgnotify.plugin.zsh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env zsh
  2. ## setup ##
  3. [[ -o interactive ]] || return #interactive only!
  4. zmodload zsh/datetime || { print "can't load zsh/datetime"; return } # faster than date()
  5. autoload -Uz add-zsh-hook || { print "can't add zsh hook!"; return }
  6. (( ${+bgnotify_threshold} )) || bgnotify_threshold=5 #default 10 seconds
  7. ## definitions ##
  8. if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom function override
  9. function bgnotify_formatted { ## args: (exit_status, command, elapsed_seconds)
  10. elapsed="$(( $3 % 60 ))s"
  11. (( $3 >= 60 )) && elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
  12. (( $3 >= 3600 )) && elapsed="$(( $3 / 3600 ))h $elapsed"
  13. [ $1 -eq 0 ] && bgnotify "#win (took $elapsed)" "$2" || bgnotify "#fail (took $elapsed)" "$2"
  14. }
  15. fi
  16. currentWindowId () {
  17. if hash osascript 2>/dev/null; then #osx
  18. osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0"
  19. elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu!
  20. xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"
  21. else
  22. echo $EPOCHSECONDS #fallback for windows
  23. fi
  24. }
  25. bgnotify () { ## args: (title, subtitle)
  26. if hash terminal-notifier 2>/dev/null; then #osx
  27. [[ "$TERM_PROGRAM" == 'iTerm.app' ]] && term_id='com.googlecode.iterm2';
  28. [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] && term_id='com.apple.terminal';
  29. ## now call terminal-notifier, (hopefully with $term_id!)
  30. [ -z "$term_id" ] && terminal-notifier -message "$2" -title "$1" >/dev/null ||
  31. terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null
  32. elif hash growlnotify 2>/dev/null; then #osx growl
  33. growlnotify -m "$1" "$2"
  34. elif hash notify-send 2>/dev/null; then #ubuntu gnome!
  35. notify-send "$1" "$2"
  36. elif hash kdialog 2>/dev/null; then #ubuntu kde!
  37. kdialog -title "$1" --passivepopup "$2" 5
  38. elif hash notifu 2>/dev/null; then #cygwyn support!
  39. notifu /m "$2" /p "$1"
  40. fi
  41. }
  42. ## Zsh hooks ##
  43. bgnotify_begin() {
  44. bgnotify_timestamp=$EPOCHSECONDS
  45. bgnotify_lastcmd="$1"
  46. bgnotify_windowid=$(currentWindowId)
  47. }
  48. bgnotify_end() {
  49. didexit=$?
  50. elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
  51. past_threshold=$(( elapsed >= bgnotify_threshold ))
  52. if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then
  53. if [ $(currentWindowId) != "$bgnotify_windowid" ]; then
  54. print -n "\a"
  55. bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"
  56. fi
  57. fi
  58. bgnotify_timestamp=0 #reset it to 0!
  59. }
  60. ## only enable if a local (non-ssh) connection
  61. if [ -z "$SSH_CLIENT" ] && [ -z "$SSH_TTY" ]; then
  62. add-zsh-hook preexec bgnotify_begin
  63. add-zsh-hook precmd bgnotify_end
  64. fi