e16d3e45d30fbf981f503b4d078d84c71a070a1d

Author
Tim O'Brien <timo@t413.com>
Committer
Tim O'Brien <timo@t413.com>
Date

Message

Add bgnotify plugin, a cross-platform background notifier!

Diff

  1diff --git a/plugins/bgnotify/README.md b/plugins/bgnotify/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..bf4b1fd09d7c7d268b35aeac1856d8ed5edac880
  4--- /dev/null
  5+++ b/plugins/bgnotify/README.md
  6@@ -0,0 +1,39 @@
  7+# bgnotify zsh plugin
  8+
  9+cross-platform background notifications for long running commands! Supports OSX and Ubuntu linux.
 10+
 11+Standalone homepage: [t413/zsh-background-notify](https://github.com/t413/zsh-background-notify)
 12+
 13+----------------------------------
 14+
 15+## How to use!
 16+
 17+Just add bgnotify to your plugins list in your `.zshrc`
 18+
 19+- On OS X you'll need [terminal-notifer](https://github.com/alloy/terminal-notifier)
 20+  * `brew install terminal-notifier` (or `gem install terminal-notifier`)
 21+- On ubuntu you're already all set!
 22+
 23+
 24+## Configuration
 25+
 26+One can configure a few things:
 27+
 28+- `bgnotify_threshold` sets the notification threshold time (default 6 seconds)
 29+- `function bgnotify_formatted` lets you change the notification
 30+
 31+Use these by adding a function definition before the your call to source. Example:
 32+
 33+~~~ sh
 34+bgnotify_threshold=4  ## set your own notification threshold
 35+
 36+function bgnotify_formatted {
 37+  ## $1=exit_status, $2=command, $3=elapsed_time
 38+  [ $1 -eq 0 ] && title="Holy Smokes Batman!" || title="Holy Graf Zeppelin!"
 39+  bgnotify "$title -- after $3 s" "$2";
 40+}
 41+
 42+plugins=(git bgnotify)  ## add to plugins list
 43+source $ZSH/oh-my-zsh.sh  ## existing source call
 44+~~~
 45+
 46diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
 47new file mode 100755
 48index 0000000000000000000000000000000000000000..f204d9dc84c7cf13fc7dfd65bc98c6620a5ab1a1
 49--- /dev/null
 50+++ b/plugins/bgnotify/bgnotify.plugin.zsh
 51@@ -0,0 +1,65 @@
 52+#!/usr/bin/env zsh
 53+
 54+
 55+## setup ##
 56+
 57+[[ -o interactive ]] || return #interactive only!
 58+zmodload zsh/datetime || { print "can't load zsh/datetime"; return } # faster than date()
 59+autoload -Uz add-zsh-hook || { print "can't add zsh hook!"; return }
 60+
 61+(( ${+bgnotify_threshold} )) || bgnotify_threshold=5 #default 10 seconds
 62+
 63+
 64+## definitions ##
 65+
 66+if ! (type notify_formatted | grep -q 'function'); then
 67+  echo "using default notify_formatted"
 68+  function bgnotify_formatted {
 69+    ## exit_status, command, elapsed_time
 70+    [ $1 -eq 0 ] && title="#win (took $3 s)" || title="#fail (took $3 s)"
 71+    bgnotify "$title" "$2"
 72+  }
 73+fi
 74+
 75+currentWindowId () {
 76+  if hash notify-send 2>/dev/null; then #ubuntu!
 77+    xprop -root | awk '/NET_ACTIVE_WINDOW/ { print $5; exit }'
 78+  elif hash osascript 2>/dev/null; then #osx
 79+    osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null
 80+  fi
 81+}
 82+
 83+bgnotify () {
 84+  if hash notify-send 2>/dev/null; then #ubuntu!
 85+    notify-send $1 $2
 86+  elif hash terminal-notifier 2>/dev/null; then #osx
 87+    terminal-notifier -message $2 -title $1
 88+  elif hash growlnotify 2>/dev/null; then #osx growl
 89+    growlnotify -m $1 $2
 90+  fi
 91+}
 92+
 93+
 94+## Zsh hooks ##
 95+
 96+bgnotify_begin() {
 97+  bgnotify_timestamp=$EPOCHSECONDS
 98+  bgnotify_lastcmd=$1
 99+  bgnotify_windowid=$(currentWindowId)
100+}
101+
102+bgnotify_end() {
103+  didexit=$?
104+  elapsed=$(( $EPOCHSECONDS - $bgnotify_timestamp ))
105+  past_threshold=$(( $elapsed >= $bgnotify_threshold ))
106+  if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then
107+    if [ $(currentWindowId) != "$bgnotify_windowid" ]; then
108+      print -n "\a"
109+      bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"
110+    fi
111+  fi
112+  bgnotify_timestamp=0 #reset it to 0!
113+}
114+
115+add-zsh-hook preexec bgnotify_begin
116+add-zsh-hook precmd bgnotify_end