Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Notifier Tmux: Saves and Restores tmux state on guard start, pause an…

…d exit
  • Loading branch information...
commit d0451826d8ec6cfbf2adbab213ef43a73b3bf22c 1 parent aaf779d
@rudicode rudicode authored
View
6 lib/guard/notifier.rb
@@ -103,6 +103,8 @@ def turn_on
else
notifications.each do |notification|
::Guard::UI.info "Guard uses #{ get_notifier_module(notification[:name]).to_s.split('::').last } to send notifications."
+ notifier = get_notifier_module(notification[:name])
+ notifier.turn_on(notification[:options]) if notifier.respond_to?(:turn_on)
end
ENV['GUARD_NOTIFY'] = 'true'
@@ -112,6 +114,10 @@ def turn_on
# Turn notifications off.
#
def turn_off
+ notifications.each do |notification|
+ notifier = get_notifier_module(notification[:name])
+ notifier.turn_off(notification[:options]) if notifier.respond_to?(:turn_off)
+ end
ENV['GUARD_NOTIFY'] = 'false'
end
View
60 lib/guard/notifiers/tmux.rb
@@ -64,6 +64,7 @@ def available?(silent = false)
def notify(type, title, message, image, options = { })
color = tmux_color type, options
color_location = options[:color_location] || DEFAULTS[:color_location]
+
system("#{ DEFAULTS[:client] } set #{ color_location } #{ color }")
show_message = options[:display_message] || DEFAULTS[:display_message]
@@ -121,6 +122,65 @@ def tmux_color(type, options = { })
options[:default] || DEFAULTS[:default]
end
end
+
+ # Notfications starting,
+ # quiet tmux output
+ #
+ def turn_on(options = { })
+ save_tmux_state
+ system(" #{ DEFAULTS[:client] } set quiet on")
+ end
+
+ def turn_off(options = { })
+ restore_tmux_state
+ 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 = {
+ '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,
+ }
+ 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
View
43 spec/guard/notifiers/tmux_spec.rb
@@ -157,4 +157,47 @@
end
end
+
+ describe '.save_tmux_state' do
+ before do
+ subject.stub(:`).and_return("option1 setting1\noption2 setting2\n")
+ end
+
+ it 'saves the current tmux options' do
+ subject.should_receive(:`).with('tmux show')
+ subject.save_tmux_state
+ subject.get_tmux_option("option1").should eq "setting1"
+ subject.get_tmux_option("option2").should eq "setting2"
+ end
+
+ it 'sets the ready_to_restore flag to true after state is saved' do
+ subject.should_receive(:`).with('tmux show')
+ subject.save_tmux_state
+ subject.ready_to_restore.should be_true
+ end
+
+ end
+
+ describe '.restore_tmux_state' do
+ before do
+ subject.stub(:`).and_return("option1 setting1\noption2 setting2\n")
+ subject.stub :system => true
+ end
+
+ it 'restores the tmux options' do
+ subject.should_receive(:`).with('tmux show')
+ subject.save_tmux_state
+ subject.should_receive(:system).with('tmux set quiet off')
+ subject.should_receive(:system).with('tmux set option2 setting2')
+ subject.should_receive(:system).with('tmux set -u status-left-bg')
+ subject.should_receive(:system).with('tmux set option1 setting1')
+ subject.should_receive(:system).with('tmux set -u status-right-bg')
+ subject.should_receive(:system).with('tmux set -u status-right-fg')
+ subject.should_receive(:system).with('tmux set -u status-left-fg')
+ subject.should_receive(:system).with('tmux set -u message-fg')
+ subject.should_receive(:system).with('tmux set -u message-bg')
+ subject.restore_tmux_state
+ end
+
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.