Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow overriding Emacs notifier options via Guardfile #337

Merged
merged 2 commits into from

4 participants

@d1
d1 commented

The first patch allows general overriding of the DEFAULTS options in the emacs notifier.

The second patch adds the ability to override options that don't exist in DEFAULTS. For example, a custom color can be set for 'pending' notifications (instead of just falling back to DEFAULTS[:default]) by providing a :pending option to the notification method.

This allows the Guardfile to look something like:

emacs_colors = {
  :success => 'Green',
  :pending => 'Yellow',
  :failed  => 'Red',
  :default => 'White',
}
notification :emacs, emacs_colors

Please let me know if if there is anything that you'd like to be done differently.

@netzpirat netzpirat merged commit 3de3c84 into guard:master

1 check passed

Details default The Travis build passed
@netzpirat
Owner

Everything is great! Thanks a lot.

@thibaudgg
Owner

:+1: Thanks!

@coveralls

Coverage Status

Coverage decreased (-31.89%) when pulling 3de3c84 on d1:emacs-notifier-options into d011713 on guard:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 56 additions and 24 deletions.
  1. +23 −19 lib/guard/notifiers/emacs.rb
  2. +33 −5 spec/guard/notifiers/emacs_spec.rb
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
Something went wrong with that request. Please try again.