Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roy van de Water
authored and
Integrum User
committed
Sep 25, 2012
1 parent
5ebc94b
commit 9bd90ea
Showing
4 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |