ff3e0d530cacaccdbc36f2c0acb6ede9118a7da9

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #4266 from t413/master

Bring bgnotify plugin up to date with t413/zsh-background-notify

Diff

 1diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
 2index b70b42f984540b2d58e9a6720e57234ec5ae21d6..459f5214e62c07d62d65dbd98901ea2c4dc82bcc 100755
 3--- a/plugins/bgnotify/bgnotify.plugin.zsh
 4+++ b/plugins/bgnotify/bgnotify.plugin.zsh
 5@@ -11,31 +11,38 @@ autoload -Uz add-zsh-hook || { print "can't add zsh hook!"; return }
 6 
 7 ## definitions ##
 8 
 9-if ! (type bgnotify_formatted | grep -q 'function'); then
10-  function bgnotify_formatted {
11-    ## exit_status, command, elapsed_time
12-    [ $1 -eq 0 ] && title="#win (took $3 s)" || title="#fail (took $3 s)"
13-    bgnotify "$title" "$2"
14+if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom function override
15+  function bgnotify_formatted { ## args: (exit_status, command, elapsed_seconds)
16+    elapsed="$(( $3 % 60 ))s"
17+    (( $3 >= 60 )) && elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
18+    (( $3 >= 3600 )) && elapsed="$(( $3 / 3600 ))h $elapsed"
19+    [ $1 -eq 0 ] && bgnotify "#win (took $elapsed)" "$2" || bgnotify "#fail (took $elapsed)" "$2"
20   }
21 fi
22 
23 currentWindowId () {
24   if hash osascript 2>/dev/null; then #osx
25     osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0"
26-  elif hash notify-send 2>/dev/null; then #ubuntu!
27-    xprop -root | awk '/NET_ACTIVE_WINDOW/ { print $5; exit }'
28+  elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu!
29+    xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"
30   else
31     echo $EPOCHSECONDS #fallback for windows
32   fi
33 }
34 
35-bgnotify () {
36+bgnotify () { ## args: (title, subtitle)
37   if hash terminal-notifier 2>/dev/null; then #osx
38-    terminal-notifier -message "$2" -title "$1"
39+    [[ "$TERM_PROGRAM" == 'iTerm.app' ]] && term_id='com.googlecode.iterm2';
40+    [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] && term_id='com.apple.terminal';
41+    ## now call terminal-notifier, (hopefully with $term_id!)
42+    [ -z "$term_id" ] && terminal-notifier -message "$2" -title "$1" >/dev/null ||
43+    terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null
44   elif hash growlnotify 2>/dev/null; then #osx growl
45     growlnotify -m "$1" "$2"
46-  elif hash notify-send 2>/dev/null; then #ubuntu!
47+  elif hash notify-send 2>/dev/null; then #ubuntu gnome!
48     notify-send "$1" "$2"
49+  elif hash kdialog 2>/dev/null; then #ubuntu kde!
50+    kdialog  -title "$1" --passivepopup  "$2" 5
51   elif hash notifu 2>/dev/null; then #cygwyn support!
52     notifu /m "$2" /p "$1"
53   fi
54@@ -46,7 +53,7 @@ bgnotify () {
55 
56 bgnotify_begin() {
57   bgnotify_timestamp=$EPOCHSECONDS
58-  bgnotify_lastcmd=$1
59+  bgnotify_lastcmd="$1"
60   bgnotify_windowid=$(currentWindowId)
61 }
62 
63@@ -63,5 +70,8 @@ bgnotify_end() {
64   bgnotify_timestamp=0 #reset it to 0!
65 }
66 
67-add-zsh-hook preexec bgnotify_begin
68-add-zsh-hook precmd bgnotify_end
69+## only enable if a local (non-ssh) connection
70+if [ -z "$SSH_CLIENT" ] && [ -z "$SSH_TTY" ]; then
71+  add-zsh-hook preexec bgnotify_begin
72+  add-zsh-hook precmd bgnotify_end
73+fi