Allow overriding Emacs notifier options via Guardfile #337

Merged
merged 2 commits into from Oct 10, 2012
View
42 lib/guard/notifiers/emacs.rb
@@ -3,14 +3,6 @@
module Guard
module Notifier
- # Default options for EmacsClient
- DEFAULTS = {
- :client => 'emacsclient',
- :success => 'ForestGreen',
- :failed => 'Firebrick',
- :default => 'Black',
- }
-
# Send a notification to Emacs with emacsclient (http://www.emacswiki.org/emacs/EmacsClient).
#
# @example Add the `:emacs` notifier to your `Guardfile`
@@ -19,6 +11,13 @@ module Notifier
module Emacs
extend self
+ DEFAULTS = {
+ :client => 'emacsclient',
+ :success => 'ForestGreen',
+ :failed => 'Firebrick',
+ :default => 'Black',
+ }
+
# Test if Emacs with running server is available.
#
# @param [Boolean] silent true if no error messages should be shown
@@ -41,28 +40,33 @@ def available?(silent = false)
# @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 [Boolean] sticky make the notification sticky
+ # @option options [String] success the color to use for success notifications (default is 'ForestGreen')
+ # @option options [String] failed the color to use for failure notifications (default is 'Firebrick')
+ # @option options [String] pending the color to use for pending notifications
+ # @option options [String] default the default color to use (default is 'Black')
+ # @option options [String] client the client to use for notification (default is 'emacsclient')
# @option options [String, Integer] priority specify an int or named key (default is 0)
#
def notify(type, title, message, image, options = { })
- system(%(#{ DEFAULTS[:client] } --eval "(set-face-background 'modeline \\"#{ emacs_color(type) }\\")"))
+ options = DEFAULTS.merge options
+ color = emacs_color type, options
+ system(%(#{ options[:client] } --eval "(set-face-background 'modeline \\"#{ color }\\")"))
end
# Get the Emacs color for the notification type.
# You can configure your own color by overwrite the defaults.
#
# @param [String] type the notification type
+ # @param [Hash] options aditional notification options
+ # @option options [String] success the color to use for success notifications (default is 'ForestGreen')
+ # @option options [String] failed the color to use for failure notifications (default is 'Firebrick')
+ # @option options [String] pending the color to use for pending notifications
+ # @option options [String] default the default color to use (default is 'Black')
# @return [String] the name of the emacs color
#
- def emacs_color(type)
- case type
- when 'success'
- DEFAULTS[:success]
- when 'failed'
- DEFAULTS[:failed]
- else
- DEFAULTS[:default]
- end
+ def emacs_color(type, options = {})
+ default = options[:default] || DEFAULTS[:default]
+ options.fetch(type.to_sym, default)
end
end
end
View
38 spec/guard/notifiers/emacs_spec.rb
@@ -12,13 +12,41 @@ def self.show(options) end
after { Object.send(:remove_const, :Emacs) if defined?(::Emacs) }
describe '.notify' do
- it 'should set correct modeline color using emacsclient' do
- subject.should_receive(:system).with do |command|
- command.should include("emacsclient")
- command.should include("(set-face-background 'modeline \\\"ForestGreen\\\")")
+ context 'when no color options are specified' do
+ it 'should set modeline color to the default color using emacsclient' do
+ subject.should_receive(:system).with do |command|
+ command.should include("emacsclient")
+ command.should include("(set-face-background 'modeline \\\"ForestGreen\\\")")
+ end
+
+ subject.notify('success', 'any title', 'any message', 'any image', { })
+ end
+ end
+
+ context 'when a color option is specified for "success" notifications' do
+ let(:options) { {:success => 'Orange'} }
+
+ it 'should set modeline color to the specified color using emacsclient' do
+ subject.should_receive(:system).with do |command|
+ command.should include("emacsclient")
+ command.should include("(set-face-background 'modeline \\\"Orange\\\")")
+ end
+
+ subject.notify('success', 'any title', 'any message', 'any image', options)
end
+ end
+
+ context 'when a color option is specified for "pending" notifications' do
+ let(:options) { {:pending => 'Yellow'} }
- subject.notify('success', 'any title', 'any message', 'any image', { })
+ it 'should set modeline color to the specified color using emacsclient' do
+ subject.should_receive(:system).with do |command|
+ command.should include("emacsclient")
+ command.should include("(set-face-background 'modeline \\\"Yellow\\\")")
+ end
+
+ subject.notify('pending', 'any title', 'any message', 'any image', options)
+ end
end
end
end