bgnotify.plugin.zsh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 0
  17. [[ $elapsed -ge $bgnotify_threshold ]] || return 0
  18. # check if Terminal app is not active
  19. [[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return 0
  20. bgnotify_formatted "$exit_status" "$bgnotify_lastcmd" "$elapsed"
  21. } always {
  22. bgnotify_timestamp=0
  23. }
  24. }
  25. autoload -Uz add-zsh-hook
  26. add-zsh-hook preexec bgnotify_begin
  27. add-zsh-hook precmd bgnotify_end
  28. ## Functions
  29. # allow custom function override
  30. (( ${+functions[bgnotify_formatted]} )) || \
  31. function bgnotify_formatted {
  32. local exit_status=$1
  33. local cmd="$2"
  34. # humanly readable elapsed time
  35. local elapsed="$(( $3 % 60 ))s"
  36. (( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
  37. (( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed"
  38. [[ $bgnotify_bell = true ]] && printf '\a' # beep sound
  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. osascript -e "tell application id \"$(bgnotify_programid)\" to get the {id, frontmost, id of front window, visible of front window}" 2>/dev/null
  48. elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )); then # wayland+sway
  49. local app_id=$(bgnotify_find_sway_appid)
  50. [[ -n "$app_id" ]] && echo "$app_id" || echo $EPOCHSECONDS
  51. elif [[ -z $WAYLAND_DISPLAY ]] && [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then
  52. xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
  53. else
  54. echo $EPOCHSECONDS
  55. fi
  56. }
  57. function bgnotify_find_sway_appid {
  58. # output is "app_id,container_id", for example "Alacritty,1694"
  59. # see example swaymsg output: https://github.com/ohmyzsh/ohmyzsh/files/13463939/output.json
  60. if (( ${+commands[jq]} )); then
  61. swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(",")'
  62. else
  63. swaymsg -t get_tree | awk '
  64. BEGIN { Id = ""; Appid = ""; FocusNesting = -1; Nesting = 0 }
  65. {
  66. # Enter a block
  67. if ($0 ~ /.*{$/) Nesting++
  68. # Exit a block. If Nesting is now less than FocusNesting, we have the data we are looking for
  69. if ($0 ~ /^[[:blank:]]*}.*/) { Nesting--; if (FocusNesting > 0 && Nesting < FocusNesting) exit 0 }
  70. # Save the Id, it is potentially what we are looking for
  71. if ($0 ~ /^[[:blank:]]*"id": [0-9]*,?$/) { sub(/^[[:blank:]]*"id": /, ""); sub(/,$/, ""); Id = $0 }
  72. # Save the Appid, it is potentially what we are looking for
  73. if ($0 ~ /^[[:blank:]]*"app_id": ".*",?$/) { sub(/^[[:blank:]]*"app_id": "/, ""); sub(/",$/, ""); Appid = $0 }
  74. # Window is focused, this nesting block contains the Id and Appid we want!
  75. if ($0 ~ /^[[:blank:]]*"focused": true,?$/) { FocusNesting = Nesting }
  76. }
  77. END {
  78. if (Appid != "" && Id != "" && FocusNesting != -1) print Appid "," Id
  79. else print ""
  80. }'
  81. fi
  82. }
  83. function bgnotify_programid {
  84. case "$TERM_PROGRAM" in
  85. iTerm.app) echo 'com.googlecode.iterm2' ;;
  86. Apple_Terminal) echo 'com.apple.terminal' ;;
  87. esac
  88. }
  89. function bgnotify {
  90. local title="$1"
  91. local message="$2"
  92. local icon="$3"
  93. if (( ${+commands[terminal-notifier]} )); then # macOS
  94. local term_id=$(bgnotify_programid)
  95. terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$term_id"} &>/dev/null
  96. elif (( ${+commands[growlnotify]} )); then # macOS growl
  97. growlnotify -m "$title" "$message"
  98. elif (( ${+commands[notify-send]} )); then
  99. notify-send "$title" "$message" ${=icon:+--icon "$icon"}
  100. elif (( ${+commands[kdialog]} )); then # KDE
  101. kdialog --title "$title" --passivepopup "$message" 5
  102. elif (( ${+commands[notifu]} )); then # cygwin
  103. notifu /m "$message" /p "$title" ${=icon:+/i "$icon"}
  104. fi
  105. }
  106. ## Defaults
  107. # enable terminal bell on notify by default
  108. bgnotify_bell=${bgnotify_bell:-true}
  109. # notify if command took longer than 5s by default
  110. bgnotify_threshold=${bgnotify_threshold:-5}
  111. # bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
  112. bgnotify_termid="$(bgnotify_appid)"