a04cf078801db637c2ecdcc95c491fa9fb217ceb

Author
Marc Cornellà <hello@mcornella.com>
Committer
Carlo <carlosalag@protonmail.com>
Date

Message

refactor(bgnotify): clean up and reorganize code

🗸 Standardize code style
🗸 Organize code sections
🗸 Optimize calls for window ID

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
  2index 9c4a62cd76f6b3a6329f53f87a7908ae9e205fea..ed2653aa8c74909b3360590eb56477095e92dc39 100644
  3--- a/plugins/bgnotify/bgnotify.plugin.zsh
  4+++ b/plugins/bgnotify/bgnotify.plugin.zsh
  5@@ -1,43 +1,78 @@
  6 #!/usr/bin/env zsh
  7 
  8-## setup ##
  9+## Setup
 10 
 11-[[ -o interactive ]] || return #interactive only!
 12-zmodload zsh/datetime || { print "can't load zsh/datetime"; return } # faster than date()
 13-autoload -Uz add-zsh-hook || { print "can't add zsh hook!"; return }
 14+[[ -o interactive ]] || return # don't load on non-interactive shells
 15+[[ -z "$SSH_CLIENT" && -z "$SSH_TTY" ]] || return # don't load on a SSH connection
 16 
 17-(( ${+bgnotify_threshold} )) || bgnotify_threshold=5 #default 10 seconds
 18+zmodload zsh/datetime # faster than `date`
 19 
 20 
 21-## definitions ##
 22+## Zsh Hooks
 23 
 24-if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom function override
 25-  function bgnotify_formatted { ## args: (exit_status, command, elapsed_seconds)
 26-    elapsed="$(( $3 % 60 ))s"
 27-    (( $3 >= 60 )) && elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
 28-    (( $3 >= 3600 )) && elapsed="$(( $3 / 3600 ))h $elapsed"
 29-    [ $1 -eq 0 ] && bgnotify "#win (took $elapsed)" "$2" || bgnotify "#fail (took $elapsed)" "$2"
 30+function bgnotify_begin {
 31+  bgnotify_timestamp=$EPOCHSECONDS
 32+  bgnotify_lastcmd="${1:-$2}"
 33+}
 34+
 35+function bgnotify_end {
 36+  {
 37+    local exit_status=$?
 38+    local elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
 39+
 40+    # check time elapsed
 41+    [[ $bgnotify_timestamp -gt 0 ]] || return
 42+    [[ $elapsed -ge $bgnotify_threshold ]] || return
 43+
 44+    # check if Terminal app is not active
 45+    [[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return
 46+
 47+    printf '\a' # beep sound
 48+    bgnotify_formatted "$exit_status" "$bgnotify_lastcmd" "$elapsed"
 49+  } always {
 50+    bgnotify_timestamp=0
 51   }
 52-fi
 53+}
 54+
 55+autoload -Uz add-zsh-hook
 56+add-zsh-hook preexec bgnotify_begin
 57+add-zsh-hook precmd bgnotify_end
 58+
 59+
 60+## Functions
 61+
 62+# allow custom function override
 63+(( ${+functions[bgnotify_formatted]} )) || \
 64+function bgnotify_formatted {
 65+  local exit_status=$1
 66+  local cmd="$2"
 67 
 68+  # humanly readable elapsed time
 69+  local elapsed="$(( $3 % 60 ))s"
 70+  (( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
 71+  (( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed"
 72+
 73+  if [[ $1 -eq 0 ]]; then
 74+    bgnotify "#win (took $elapsed)" "$2"
 75+  else
 76+    bgnotify "#fail (took $elapsed)" "$2"
 77+  fi
 78 }
 79 
 80-function currentAppId {
 81+# for macOS, output is "app ID, window ID" (com.googlecode.iterm2, 116)
 82+function bgnotify_appid {
 83   if (( ${+commands[osascript]} )); then
 84-    # output: com.googlecode.iterm2, 116
 85     osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
 86-  elif (( ${+commands[notify-send]} || ${+commands[kdialog]} )); then
 87-    xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"
 88+  elif (( ${+commands[xprop]} )); then
 89+    xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
 90   else
 91     echo $EPOCHSECONDS
 92   fi
 93 }
 94 
 95-# currentAppId is expensive (more on macOS!) and it will remain the same until the shell is close
 96-bgnotify_termid=$(currentAppId)
 97-
 98-bgnotify () { ## args: (title, subtitle)
 99-  if hash terminal-notifier 2>/dev/null; then #osx
100+function bgnotify {
101+  # $1: title, $2: message
102+  if (( ${+commands[terminal-notifier]} )); then # macOS
103     local term_id="${bgnotify_termid%%,*}" # remove window id
104     if [[ -z "$term_id" ]]; then