Permalink
Browse files

Add tmux notifier

  • Loading branch information...
1 parent 5ebc94b commit 9bd90eaaf5e52a8878478a9a4ec5344370cd7977 Roy van de Water committed with Integrum User Sep 25, 2012
Showing with 148 additions and 3 deletions.
  1. +4 −0 README.md
  2. +6 −3 lib/guard/notifier.rb
  3. +68 −0 lib/guard/notifiers/tmux.rb
  4. +70 −0 spec/guard/notifiers/tmux_spec.rb
View
4 README.md
@@ -188,6 +188,10 @@ group :development do
end
```
+### Tmux
+
+* To use Tmux notifications, you have to start guard within a [tmux](http://tmux.sourceforge.net/) session
+
Add Guard plugins
-----------------
View
9 lib/guard/notifier.rb
@@ -14,6 +14,7 @@ module Guard
# * rb-notifu
# * emacs
# * Terminal Notifier
+ # * Tmux
#
# Please see the documentation of each notifier for more information about the requirements
# and configuration possibilities.
@@ -45,6 +46,7 @@ module Notifier
require 'guard/notifiers/rb_notifu'
require 'guard/notifiers/emacs'
require 'guard/notifiers/terminal_notifier'
+ require 'guard/notifiers/tmux'
extend self
@@ -58,7 +60,8 @@ module Notifier
[:notifysend, ::Guard::Notifier::NotifySend],
[:notifu, ::Guard::Notifier::Notifu],
[:emacs, ::Guard::Notifier::Emacs],
- [:terminal_notifier, ::Guard::Notifier::TerminalNotifier]
+ [:terminal_notifier, ::Guard::Notifier::TerminalNotifier],
+ [:tmux, ::Guard::Notifier::Tmux]
]
# Get the available notifications.
@@ -126,7 +129,7 @@ def add_notification(name, options = { }, silent = false)
return turn_off if name == :off
notifier = get_notifier_module(name)
-
+
if notifier && notifier.available?(silent)
self.notifications = notifications << { :name => name, :options => options }
true
@@ -168,7 +171,7 @@ def get_notifier_module(name)
notifier = NOTIFIERS.detect { |n| n.first == name }
notifier ? notifier.last : notifier
end
-
+
# Auto detect the available notification library. This goes through
# the list of supported notification gems and picks the first that
# is available.
View
68 lib/guard/notifiers/tmux.rb
@@ -0,0 +1,68 @@
+module Guard
+ module Notifier
+
+ # Default options for Tmux
+
+ # Changes the color of the Tmux status bar
+ #
+ # @example Add the `:tmux` notifier to your `Guardfile`
+ # notification :tmux
+ #
+ module Tmux
+ extend self
+
+ DEFAULTS = {
+ :client => 'tmux',
+ :tmux_environment => 'TMUX',
+ :success => 'green',
+ :failed => 'red',
+ :default => 'green',
+ }
+
+ # Test if currently running in a Tmux session
+ #
+ # @param [Boolean] silent true if no error messages should be shown
+ # @return [Boolean] the availability status
+ #
+ def available?(silent = false)
+ if ENV[DEFAULTS[:tmux_environment]].nil?
+ ::Guard::UI.error 'The :tmux notifier runs only on when guard is executed inside of a tmux session.' unless silent
+ false
+ else
+ true
+ 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 [Boolean] sticky make the notification sticky
+ # @option options [String, Integer] priority specify an int or named key (default is 0)
+ #
+ def notify(type, title, message, image, options = { })
+ system("#{DEFAULTS[:client]} set-window-option -g window-status-current-bg #{tmux_color type}")
+ end
+
+ # Get the Tmux color for the notification type.
+ # You can configure your own color by overwrite the defaults.
+ #
+ # @param [String] type the notification type
+ # @return [String] the name of the emacs color
+ #
+ def tmux_color(type)
+ case type
+ when 'success'
+ DEFAULTS[:success]
+ when 'failed'
+ DEFAULTS[:failed]
+ else
+ DEFAULTS[:default]
+ end
+ end
+ end
+ end
+end
View
70 spec/guard/notifiers/tmux_spec.rb
@@ -0,0 +1,70 @@
+require 'spec_helper'
+
+describe Guard::Notifier::Tmux do
+ before(:all) { Object.send(:remove_const, :Tmux) if defined?(::Tmux) }
+
+ before do
+ class ::Tmux
+ def self.show(options) end
+ end
+ end
+
+ after { Object.send(:remove_const, :Tmux) if defined?(::Tmux) }
+
+ describe '.available?' do
+ context "when the TMUX environment variable is set" do
+ before :each do
+ ENV['TMUX'] = 'something'
+ end
+
+ it "should return true" do
+ subject.available?.should be_true
+ end
+ end
+
+ context "when the TMUX environment variable is not set" do
+ before :each do
+ ENV['TMUX'] = nil
+ end
+
+ context 'without the silent option' do
+ it 'shows an error message when the TMUX environment variable is not set' do
+ ::Guard::UI.should_receive(:error).with "The :tmux notifier runs only on when guard is executed inside of a tmux session."
+ subject.available?
+ end
+ end
+
+ context 'with the silent option' do
+ it 'should return false' do
+ subject.available?(true).should be_false
+ end
+ end
+ end
+ end
+
+ describe '.notify' do
+ it 'should set the tmux status bar color to green on success' do
+ subject.should_receive(:system).with "tmux set-window-option -g window-status-current-bg green"
+
+ subject.notify('success', 'any title', 'any message', 'any image', { })
+ end
+
+ it 'should set the tmux status bar color to red on failure' do
+ subject.should_receive(:system).with "tmux set-window-option -g window-status-current-bg red"
+
+ subject.notify('failed', 'any title', 'any message', 'any image', { })
+ end
+
+ it 'should set the tmux status bar color to green on pending' do
+ subject.should_receive(:system).with "tmux set-window-option -g window-status-current-bg green"
+
+ subject.notify('pending', 'any title', 'any message', 'any image', { })
+ end
+
+ it 'should set the tmux status bar color to green on notify' do
+ subject.should_receive(:system).with "tmux set-window-option -g window-status-current-bg green"
+
+ subject.notify('notify', 'any title', 'any message', 'any image', { })
+ end
+ end
+end

0 comments on commit 9bd90ea

Please sign in to comment.