bgnotify.plugin.zsh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. [[ $bgnotify_bell = true ]] && 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]} )); then # wayland+sway
  50. local app_id=$(find_sway_appid)
  51. [[ -n "$app_id" ]] && echo "$app_id" || echo $EPOCHSECONDS
  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 find_sway_appid {
  59. # output is "app_id,container_id", for example "Alacritty,1694"
  60. # see example swaymsg output: https://github.com/ohmyzsh/ohmyzsh/files/13463939/output.json
  61. if (( ${+commands[jq]} )); then
  62. swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(",")'
  63. else
  64. swaymsg -t get_tree | awk '
  65. BEGIN { Id = ""; Appid = ""; FocusNesting = -1; Nesting = 0 }
  66. {
  67. # Enter a block
  68. if ($0 ~ /.*{$/) Nesting++
  69. # Exit a block. If Nesting is now less than FocusNesting, we have the data we are looking for
  70. if ($0 ~ /^[[:blank:]]*}.*/) { Nesting--; if (FocusNesting > 0 && Nesting < FocusNesting) exit 0 }
  71. # Save the Id, it is potentially what we are looking for
  72. if ($0 ~ /^[[:blank:]]*"id": [0-9]*,?$/) { sub(/^[[:blank:]]*"id": /, ""); sub(/,$/, ""); Id = $0 }
  73. # Save the Appid, it is potentially what we are looking for
  74. if ($0 ~ /^[[:blank:]]*"app_id": ".*",?$/) { sub(/^[[:blank:]]*"app_id": "/, ""); sub(/",$/, ""); Appid = $0 }
  75. # Window is focused, this nesting block contains the Id and Appid we want!
  76. if ($0 ~ /^[[:blank:]]*"focused": true,?$/) { FocusNesting = Nesting }
  77. }
  78. END {
  79. if (Appid != "" && Id != "" && FocusNesting != -1) print Appid "," Id
  80. else print ""
  81. }'
  82. fi
  83. }
  84. function find_term_id {
  85. local term_id="${bgnotify_termid%%,*}" # remove window id
  86. if [[ -z "$term_id" ]]; then
  87. case "$TERM_PROGRAM" in
  88. iTerm.app) term_id='com.googlecode.iterm2' ;;
  89. Apple_Terminal) term_id='com.apple.terminal' ;;
  90. esac
  91. fi
  92. echo "$term_id"
  93. }
  94. function bgnotify {
  95. local title="$1"
  96. local message="$2"
  97. local icon="$3"
  98. if (( ${+commands[terminal-notifier]} )); then # macOS
  99. local term_id=$(find_term_id)
  100. terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$term_id"} &>/dev/null
  101. elif (( ${+commands[growlnotify]} )); then # macOS growl
  102. growlnotify -m "$title" "$message"
  103. elif (( ${+commands[notify-send]} )); then
  104. notify-send "$title" "$message" ${=icon:+--icon "$icon"}
  105. elif (( ${+commands[kdialog]} )); then # KDE
  106. kdialog --title "$title" --passivepopup "$message" 5
  107. elif (( ${+commands[notifu]} )); then # cygwin
  108. notifu /m "$message" /p "$title" ${=icon:+/i "$icon"}
  109. fi
  110. }
  111. ## Defaults
  112. # enable terminal bell on notify by default
  113. bgnotify_bell=${bgnotify_bell:-true}
  114. # notify if command took longer than 5s by default
  115. bgnotify_threshold=${bgnotify_threshold:-5}
  116. # bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
  117. bgnotify_termid="$(bgnotify_appid)"