bgnotify.plugin.zsh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env zsh
  2. ## Setup
  3. [[ -o interactive ]] || return # don't load on non-interactive shells
  4. [[ -z "$SSH_CLIENT" && -z "$SSH_TTY" ]] || return # don't load on a SSH connection
  5. zmodload zsh/datetime # faster than `date`
  6. ## Zsh Hooks
  7. function bgnotify_begin {
  8. bgnotify_timestamp=$EPOCHSECONDS
  9. bgnotify_lastcmd="${1:-$2}"
  10. }
  11. function bgnotify_end {
  12. {
  13. local exit_status=$?
  14. local elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
  15. # check time elapsed
  16. [[ $bgnotify_timestamp -gt 0 ]] || return
  17. [[ $elapsed -ge $bgnotify_threshold ]] || return
  18. # check if Terminal app is not active
  19. [[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return
  20. printf '\a' # beep sound
  21. bgnotify_formatted "$exit_status" "$bgnotify_lastcmd" "$elapsed"
  22. } always {
  23. bgnotify_timestamp=0
  24. }
  25. }
  26. autoload -Uz add-zsh-hook
  27. add-zsh-hook preexec bgnotify_begin
  28. add-zsh-hook precmd bgnotify_end
  29. ## Functions
  30. # allow custom function override
  31. (( ${+functions[bgnotify_formatted]} )) || \
  32. function bgnotify_formatted {
  33. local exit_status=$1
  34. local cmd="$2"
  35. # humanly readable elapsed time
  36. local elapsed="$(( $3 % 60 ))s"
  37. (( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
  38. (( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed"
  39. if [[ $1 -eq 0 ]]; then
  40. bgnotify "#win (took $elapsed)" "$2"
  41. else
  42. bgnotify "#fail (took $elapsed)" "$2"
  43. fi
  44. }
  45. # for macOS, output is "app ID, window ID" (com.googlecode.iterm2, 116)
  46. function bgnotify_appid {
  47. if (( ${+commands[osascript]} )); then
  48. osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
  49. elif (( ${+commands[xprop]} )); then
  50. xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
  51. else
  52. echo $EPOCHSECONDS
  53. fi
  54. }
  55. function bgnotify {
  56. # $1: title, $2: message
  57. if (( ${+commands[terminal-notifier]} )); then # macOS
  58. local term_id="${bgnotify_termid%%,*}" # remove window id
  59. if [[ -z "$term_id" ]]; then
  60. case "$TERM_PROGRAM" in
  61. iTerm.app) term_id='com.googlecode.iterm2' ;;
  62. Apple_Terminal) term_id='com.apple.terminal' ;;
  63. esac
  64. fi
  65. if [[ -z "$term_id" ]]; then
  66. terminal-notifier -message "$2" -title "$1" &>/dev/null
  67. else
  68. terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" &>/dev/null
  69. fi
  70. elif (( ${+commands[growlnotify]} )); then # macOS growl
  71. growlnotify -m "$1" "$2"
  72. elif (( ${+commands[notify-send]} )); then # GNOME
  73. notify-send "$1" "$2"
  74. elif (( ${+commands[kdialog]} )); then # KDE
  75. kdialog --title "$1" --passivepopup "$2" 5
  76. elif (( ${+commands[notifu]} )); then # cygwin
  77. notifu /m "$2" /p "$1"
  78. fi
  79. }
  80. ## Defaults
  81. # notify if command took longer than 5s by default
  82. bgnotify_threshold=${bgnotify_threshold:-5}
  83. # bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
  84. bgnotify_termid="$(bgnotify_appid)"