Skip to content

Commit

Permalink
Add tmux notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy van de Water authored and Integrum User committed Sep 25, 2012
1 parent 5ebc94b commit 9bd90ea
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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
-----------------

Expand Down
9 changes: 6 additions & 3 deletions lib/guard/notifier.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions 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
70 changes: 70 additions & 0 deletions 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.