bgnotify.plugin.zsh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 [[ $exit_status -eq 0 ]]; then
  40. bgnotify "#win (took $elapsed)" "$cmd"
  41. else
  42. bgnotify "#fail (took $elapsed)" "$cmd"
  43. fi
  44. }
  45. function bgnotify_appid {
  46. if (( ${+commands[osascript]} )); then
  47. # output is "app ID, window ID" (com.googlecode.iterm2, 116)
  48. osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
  49. elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )) && (( ${+commands[jq]} )); then # wayland+sway
  50. # output is "app_id, container id" (Alacritty, 1694)
  51. swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")'
  52. elif [[ -z $WAYLAND_DISPLAY ]] && [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then
  53. xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
  54. else
  55. echo $EPOCHSECONDS
  56. fi
  57. }
  58. function bgnotify {
  59. local title="$1"
  60. local message="$2"
  61. local icon="$3"
  62. if (( ${+commands[terminal-notifier]} )); then # macOS
  63. local term_id="${bgnotify_termid%%,*}" # remove window id
  64. if [[ -z "$term_id" ]]; then
  65. case "$TERM_PROGRAM" in
  66. iTerm.app) term_id='com.googlecode.iterm2' ;;
  67. Apple_Terminal) term_id='com.apple.terminal' ;;
  68. esac
  69. fi
  70. terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$term_id"} &>/dev/null
  71. elif (( ${+commands[growlnotify]} )); then # macOS growl
  72. growlnotify -m "$title" "$message"
  73. elif (( ${+commands[notify-send]} )); then
  74. notify-send "$title" "$message" ${=icon:+--icon "$icon"}
  75. elif (( ${+commands[kdialog]} )); then # KDE
  76. kdialog --title "$title" --passivepopup "$message" 5
  77. elif (( ${+commands[notifu]} )); then # cygwin
  78. notifu /m "$message" /p "$title" ${=icon:+/i "$icon"}
  79. fi
  80. }
  81. ## Defaults
  82. # notify if command took longer than 5s by default
  83. bgnotify_threshold=${bgnotify_threshold:-5}
  84. # bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
  85. bgnotify_termid="$(bgnotify_appid)"