Permalink
Browse files

Merge remote-tracking branch 'alandipert/master'

  • Loading branch information...
2 parents 9ea3b7c + b95e96e commit cd60ed19911d8f262ba5c1a968957e4e150d5063 @netzpirat netzpirat committed Feb 10, 2012
Showing with 120 additions and 1 deletion.
  1. +4 −0 README.md
  2. +3 −1 lib/guard/notifier.rb
  3. +81 −0 lib/guard/notifiers/notifysend.rb
  4. +1 −0 spec/guard/notifier_spec.rb
  5. +31 −0 spec/guard/notifiers/notifysend_spec.rb
View
@@ -105,6 +105,10 @@ group :development do
end
```
+If you are unable to build the `libnotify` gem on your system, Guard
+also has a built in notifier - `notifysend` - that shells out to the
+`notify-send` utility that comes with `libnotify-bin`.
+
#### Notifu
* Runs on Windows
@@ -7,6 +7,7 @@
require 'guard/notifiers/growl'
require 'guard/notifiers/growl_notify'
require 'guard/notifiers/libnotify'
+require 'guard/notifiers/notifysend'
require 'guard/notifiers/rb_notifu'
module Guard
@@ -47,6 +48,7 @@ module Notifier
:growl => ::Guard::Notifier::Growl,
:growl_notify => ::Guard::Notifier::GrowlNotify,
:libnotify => ::Guard::Notifier::Libnotify,
+ :notifysend => ::Guard::Notifier::NotifySend,
:notifu => ::Guard::Notifier::Notifu
}
@@ -151,7 +153,7 @@ def notify(message, options = { })
# is available.
#
def auto_detect_notification
- available = [:growl_notify, :gntp, :growl, :libnotify, :notifu].any? { |notifier| add_notification(notifier, { }, true) }
+ available = [:growl_notify, :gntp, :growl, :libnotify, :notifysend, :notifu].any? { |notifier| add_notification(notifier, { }, true) }
::Guard::UI.info('Guard could not detect any of the supported notification libraries.') unless available
end
@@ -0,0 +1,81 @@
+require 'rbconfig'
+
+module Guard
+ module Notifier
+
+ # System notifications using notify-send, a binary that ships with
+ # the libnotify-bin package on many Debian-based distributions.
+ #
+ # @example Add the `:notifysend` notifier to your `Guardfile`
+ # notification :notifysend
+ #
+ module NotifySend
+ extend self
+
+ # Default options for the notify-send program
+ DEFAULTS = {
+ :t => 3000 # Default timeout is 3000ms
+ }
+
+ # Full list of options supported by notify-send
+ SUPPORTED = [:u, :t, :i, :c, :h]
+
+ # Test if the notification program is available.
+ #
+ # @param [Boolean] silent true if no error messages should be shown
+ # @return [Boolean] the availability status
+ #
+ def available?(silent = false)
+ if (RbConfig::CONFIG['host_os'] =~ /linux|freebsd|openbsd|sunos|solaris/) and (not `which notify-send`.empty?)
+ true
+ else
+ ::Guard::UI.error 'The :notifysend notifier runs only on Linux, FreeBSD, OpenBSD and Solaris with the libnotify-bin package installed.' unless silent
+ false
+ end
+ end
+
+ # Show a system notification.
+ #
+ # @param [String] type the notification type. Either 'success', 'pending', 'failed' or 'notify'
+ # @param [String] title the notification title
+ # @param [String] message the notification message body
+ # @param [String] image the path to the notification image
+ # @param [Hash] options additional notification library options
+ # @option options [String] c the notification category
+ # @option options [Number] t the number of milliseconds to display (1000, 3000)
+ #
+ def notify(type, title, message, image, options = { })
+ command = "notify-send '#{title}' '#{message}'"
+ system(to_command_string(command, SUPPORTED, DEFAULTS.merge(options).merge({
+ :u => notifysend_urgency(type),
+ :i => image
+ })))
+ end
+
+ private
+
+ # Convert Guards notification type to the best matching
+ # notify-send urgency.
+ #
+ # @param [String] type the Guard notification type
+ # @return [String] the notify-send urgency
+ #
+ def notifysend_urgency(type)
+ {'failed' => 'critical', 'pending' => 'normal'}.fetch(type, 'low')
+ end
+
+ # Build a shell command out of a command string and option hash.
+ #
+ # @param [String] command the command execute
+ # @param [Array] supported list of supported option flags
+ # @param [Hash] options additional command options
+ # @return [String] the command and its options converted to a shell command.
+ #
+ def to_command_string(command, supported, options = {})
+ options.reduce(command) do |cmd, (flag, value)|
+ supported.include?(flag) ? cmd + " -#{flag} '#{value}'" : cmd
+ end
+ end
+ end
+ end
+end
@@ -35,6 +35,7 @@
Guard::Notifier.should_receive(:add_notification).with(:gntp, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:growl, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:libnotify, { }, true).and_return false
+ Guard::Notifier.should_receive(:add_notification).with(:notifysend, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:notifu, { }, true).and_return false
Guard::Notifier.turn_on
end
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Guard::Notifier::NotifySend do
+ before(:all) { Object.send(:remove_const, :NotifySend) if defined?(::NotifySend) }
+
+ before do
+ class ::NotifySend
+ def self.show(options) end
+ end
+ end
+
+ after { Object.send(:remove_const, :NotifySend) if defined?(::NotifySend) }
+
+ describe '.available?' do
+ context 'without the silent option' do
+ it 'shows an error message when not available on the host OS' do
+ ::Guard::UI.should_receive(:error).with 'The :notifysend notifier runs only on Linux, FreeBSD, OpenBSD and Solaris with the libnotify-bin package installed.'
+ RbConfig::CONFIG.should_receive(:[]).with('host_os').and_return 'darwin'
+ subject.available?
+ end
+ end
+
+ context 'with the silent option' do
+ it 'does not show an error message when not available on the host OS' do
+ ::Guard::UI.should_not_receive(:error).with 'The :notifysend notifier runs only on Linux, FreeBSD, OpenBSD and Solaris with the libnotify-bin package installed.'
+ RbConfig::CONFIG.should_receive(:[]).with('host_os').and_return 'darwin'
+ subject.available?(true)
+ end
+ end
+ end
+end

0 comments on commit cd60ed1

Please sign in to comment.