Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor Tmux options store/restore.
  • Loading branch information
netzpirat committed Dec 21, 2012
1 parent 8900a42 commit 9f0b57c
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 82 deletions.
2 changes: 1 addition & 1 deletion lib/guard.rb
Expand Up @@ -196,9 +196,9 @@ def stop
within_preserved_state(false) do
::Guard::UI.debug 'Guard stops all plugins'
runner.run(:stop)
::Guard::Notifier.turn_off
::Guard::UI.info 'Bye bye...', :reset => true
listener.stop
::Guard::Notifier.turn_off
::Guard.running = false
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/guard/notifier.rb
Expand Up @@ -118,6 +118,7 @@ def turn_off
notifier = get_notifier_module(notification[:name])
notifier.turn_off(notification[:options]) if notifier.respond_to?(:turn_off)
end

ENV['GUARD_NOTIFY'] = 'false'
end

Expand Down
86 changes: 41 additions & 45 deletions lib/guard/notifiers/tmux.rb
@@ -1,8 +1,6 @@
module Guard
module Notifier

# Default options for Tmux

# Changes the color of the Tmux status bar and optionally
# shows messages in the status bar.
#
Expand All @@ -18,6 +16,7 @@ module Notifier
module Tmux
extend self

# Default options for Tmux
DEFAULTS = {
:client => 'tmux',
:tmux_environment => 'TMUX',
Expand Down Expand Up @@ -62,7 +61,7 @@ def available?(silent = false)
# @option options [Boolean] display_message whether to display a message or not
#
def notify(type, title, message, image, options = { })
color = tmux_color type, options
color = tmux_color(type, options)
color_location = options[:color_location] || DEFAULTS[:color_location]

system("#{ DEFAULTS[:client] } set #{ color_location } #{ color }")
Expand Down Expand Up @@ -123,64 +122,61 @@ def tmux_color(type, options = { })
end
end

# Notfications starting,
# quiet tmux output
# Notification starting, save the current Tmux settings
# and quiet the Tmux output.
#
def turn_on(options = { })
save_tmux_state
system(" #{ DEFAULTS[:client] } set quiet on")
unless @options_stored
reset_options_store

`#{ DEFAULTS[:client] } show`.each_line do |line|
option, _, setting = line.chomp.partition(' ')
@options_store[option] = setting
end

@options_stored = true
end

system("#{ DEFAULTS[:client] } set quiet on")
end

# Notification stopping. Restore the previous Tmux state
# if available (existing options are restored, new options
# are unset) and unquite the Tmux output.
#
def turn_off(options = { })
restore_tmux_state
if @options_stored
@options_store.each do |key, value|
if value
system("#{ DEFAULTS[:client] } set #{ key } #{ value }")
else
system("#{ DEFAULTS[:client] } set -u #{ key }")
end
end

reset_options_store
end

system("#{ DEFAULTS[:client] } set quiet off")
end

# Saves current tmux options that will be restored when notifications are turned off.
# @tmux_state { }, nil marks options to be 'unset'
def save_tmux_state
@tmux_state = {
private

# Reset the internal Tmux options store defaults.
#
def reset_options_store
@options_stored = false
@options_store = {
'status-left-bg' => nil,
'status-right-bg' => nil,
'status-left-fg' => nil,
'status-right-fg' => nil,
'message-bg' => nil,
'message-fg' => nil,
'display-time' => nil,
'display-time' => nil
}
tmux_state = `#{ DEFAULTS[:client] } show`

tmux_state.each_line do |line|
option, space, setting = line.chomp.partition(' ')
@tmux_state[option] = setting
end
@ready_to_restore = true
end

# Restore tmux settings, if previously saved.
# If an option was saved it restores the value
# If an option did not exist it attempts to un-set it
# tmux output, back on
#
def restore_tmux_state
return unless ready_to_restore
@ready_to_restore = false

@tmux_state.each do |key, value|
if value
system("#{ DEFAULTS[:client] } set #{ key } #{ value }")
else
system("#{ DEFAULTS[:client] } set -u #{ key }")
end
end
system("#{ DEFAULTS[:client] } set quiet off")
end

def get_tmux_option option
@tmux_state[option]
end

attr_accessor :ready_to_restore

end
end
end
28 changes: 26 additions & 2 deletions spec/guard/notifier_spec.rb
Expand Up @@ -17,6 +17,11 @@
Guard::Notifier.turn_on
Guard::Notifier.should be_enabled
end

it 'turns on the defined notification module' do
::Guard::Notifier::GNTP.should_receive(:turn_on)
Guard::Notifier.turn_on
end
end

context 'without configured notifications' do
Expand Down Expand Up @@ -58,7 +63,6 @@
Guard::Notifier.turn_on
end


it 'does enable the notifications when a library is available' do
Guard::Notifier.should_receive(:add_notification) do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
Expand All @@ -68,6 +72,15 @@
Guard::Notifier.should be_enabled
end

it 'does turn on the notification module for libraries that are available' do
::Guard::Notifier::GNTP.should_receive(:turn_on)
Guard::Notifier.should_receive(:add_notification) do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
true
end.any_number_of_times
Guard::Notifier.turn_on
end

it 'does not enable the notifications when no library is available' do
Guard::Notifier.should_receive(:add_notification).any_number_of_times.and_return false
Guard::Notifier.turn_on
Expand All @@ -94,9 +107,20 @@
before { ENV['GUARD_NOTIFY'] = 'true' }

it 'disables the notifications' do
subject.turn_off
Guard::Notifier.turn_off
ENV['GUARD_NOTIFY'].should == 'false'
end

context 'when turned on with available notifications' do
before do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
end

it 'turns off each notification' do
::Guard::Notifier::GNTP.should_receive(:turn_off)
Guard::Notifier.turn_off
end
end
end

describe 'toggle_notification' do
Expand Down

0 comments on commit 9f0b57c

Please sign in to comment.