bgnotify.plugin.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. currentAppId () {
  17. if (( $+commands[osascript] )); then
  18. osascript -e 'tell application (path to frontmost application as text) to id' 2>/dev/null
  19. fi
  20. }
  21. currentWindowId () {
  22. if hash osascript 2>/dev/null; then #osx
  23. osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0"
  24. elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu!
  25. xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"
  26. else
  27. echo $EPOCHSECONDS #fallback for windows
  28. fi
  29. }
  30. bgnotify () { ## args: (title, subtitle)
  31. if hash terminal-notifier 2>/dev/null; then #osx
  32. local term_id="$bgnotify_appid"
  33. if [[ -z "$term_id" ]]; then
  34. case "$TERM_PROGRAM" in
  35. iTerm.app) term_id='com.googlecode.iterm2' ;;
  36. Apple_Terminal) term_id='com.apple.terminal' ;;
  37. esac
  38. fi
  39. ## now call terminal-notifier, (hopefully with $term_id!)
  40. if [[ -z "$term_id" ]]; then
  41. terminal-notifier -message "$2" -title "$1" >/dev/null
  42. else
  43. terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null
  44. fi
  45. elif hash growlnotify 2>/dev/null; then #osx growl
  46. growlnotify -m "$1" "$2"
  47. elif hash notify-send 2>/dev/null; then #ubuntu gnome!
  48. notify-send "$1" "$2"
  49. elif hash kdialog 2>/dev/null; then #ubuntu kde!
  50. kdialog --title "$1" --passivepopup "$2" 5
  51. elif hash notifu 2>/dev/null; then #cygwyn support!
  52. notifu /m "$2" /p "$1"
  53. fi
  54. }
  55. ## Zsh hooks ##
  56. bgnotify_begin() {
  57. bgnotify_timestamp=$EPOCHSECONDS
  58. bgnotify_lastcmd="${1:-$2}"
  59. bgnotify_appid="$(currentAppId)"
  60. bgnotify_windowid=$(currentWindowId)
  61. }
  62. bgnotify_end() {
  63. didexit=$?
  64. elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
  65. past_threshold=$(( elapsed >= bgnotify_threshold ))
  66. if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then
  67. if [[ $(currentAppId) != "$bgnotify_appid" || $(currentWindowId) != "$bgnotify_windowid" ]]; then
  68. print -n "\a"
  69. bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"
  70. fi
  71. fi
  72. bgnotify_timestamp=0 #reset it to 0!
  73. }
  74. ## only enable if a local (non-ssh) connection
  75. if [ -z "$SSH_CLIENT" ] && [ -z "$SSH_TTY" ]; then
  76. add-zsh-hook preexec bgnotify_begin
  77. add-zsh-hook precmd bgnotify_end
  78. fi