Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Extend `:tmux` notifier with use of `tmux display-message` and options to configure them #334

Merged
merged 5 commits into from

3 participants

@matthijsgroen

The current :tmux notifier implementation was too limited that it only colored the left status bar. I want to see a message, so updated the notifier to also use display-message when configured. I has extended configurable options, so 'powerline' users can pimp there messages :-)

@thibaudgg
Owner

Ok for you @netzpirat? (I don't know/use :tmux)

@netzpirat
Owner

I know but currently don't use tmux, so we have to rely on the experience of @matthijsgroen.

@matthijsgroen

What it basically does (easy to test) when in tmux

tmux display-message 'hello world'

This will show for a short period of time (configurable by tmux set display-time) an message instead of the statusbar. after the period the statusbar returns. this is all standard tmux behaviour, and ideal for notifications such as those of Guard. The user can get the message history by using Ctrl+b ~ (where Ctrl+b is your key to activate tmux).

I think a quick scan of the code will give a general idea what it does.

@netzpirat netzpirat merged commit 00826c4 into from
@netzpirat
Owner

Thanks a lot for your detailed information. I've added it straight to the README.

@matthijsgroen

Ah I'm afraid that is not fully correct, since my code will set that display-time for the user, it overwrites custom settings. I will refork and update the readme in a pull request ok? (sorry for the late response, I was away for the weekend)

@thibaudgg
Owner

Ok, for the new pull request. Thanks!

@matthijsgroen

When will the rubygem be published on rubygems.org?

@netzpirat
Owner

This has been already published with 1.4.1

@matthijsgroen

hmm weird, rubygems is still listing 1.4.0 as the latest version http://rubygems.org/gems/guard/versions

@netzpirat
Owner

You're right, somehow the "release" wasn't released at all. I reverted the 1.4.1 release commit and will release a new version today or tomorrow.

@docwhat docwhat referenced this pull request from a commit in docwhat/guard
@netzpirat netzpirat Add some Tmux info to the README.
Thanks  @matthijsgroen for explaining it in detail:
guard#334
9c08c5b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 90 additions and 4 deletions.
  1. +36 −2 lib/guard/notifiers/tmux.rb
  2. +54 −2 spec/guard/notifiers/tmux_spec.rb
View
38 lib/guard/notifiers/tmux.rb
@@ -3,7 +3,8 @@ module Notifier
# Default options for Tmux
- # Changes the color of the Tmux status bar
+ # Changes the color of the Tmux status bar, and optionally
+ # shows messages in the status bar. (see display_message)
#
# @example Add the `:tmux` notifier to your `Guardfile`
# notification :tmux
@@ -16,7 +17,12 @@ module Tmux
:tmux_environment => 'TMUX',
:success => 'green',
:failed => 'red',
- :default => 'green'
+ :pending => 'yellow',
+ :default => 'green',
+ :timeout => 5,
+ :display_message => false,
+ :default_message_format => '%s - %s',
+ :line_separator => ' - '
}
# Test if currently running in a Tmux session
@@ -46,6 +52,32 @@ def available?(silent = false)
def notify(type, title, message, image, options = { })
color = tmux_color type, options
system("#{ DEFAULTS[:client] } set -g status-left-bg #{ color }")
+
+ show_message = options[:display_message] || DEFAULTS[:display_message]
+ display_message(type, title, message, options) if show_message
+ end
+
+ # Display a message in the statusbar of tmux.
+ #
+ # @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 [Hash] options additional notification library options
+ # @option options [Integer] timeout the amount of seconds to show the message in the statusbar (default is 5)
+ # @option options [String] default_message_format a string to use as formatter. (default is '%s - %s')
+ # @option options [String] line_separator a string to use instead of a line-break. (default is ' - ')
+ def display_message(type, title, message, options = { })
+ message_format = options["#{type}_message_format".to_sym] || options[:default_message_format] || DEFAULTS[:default_message_format]
+ display_time = options[:timeout] || DEFAULTS[:timeout]
+ separator = options[:line_separator] || DEFAULTS[:line_separator]
+
+ color = tmux_color type, options
+ formatted_message = message.split("\n").join(separator)
+ display_message = message_format % [title, formatted_message]
+
+ system("#{ DEFAULTS[:client] } set display-time #{ display_time * 1000 }")
+ system("#{ DEFAULTS[:client] } set message-bg #{ color }")
+ system("#{ DEFAULTS[:client] } display-message '#{ display_message }'")
end
# Get the Tmux color for the notification type.
@@ -60,6 +92,8 @@ def tmux_color(type, options = { })
options[:success] || DEFAULTS[:success]
when 'failed'
options[:failed] || DEFAULTS[:failed]
+ when 'pending'
+ options[:pending] || DEFAULTS[:pending]
else
options[:default] || DEFAULTS[:default]
end
View
56 spec/guard/notifiers/tmux_spec.rb
@@ -61,8 +61,8 @@ def self.show(options) end
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 -g status-left-bg green"
+ it 'should set the tmux status bar color to yellow on pending' do
+ subject.should_receive(:system).with "tmux set -g status-left-bg yellow"
subject.notify('pending', 'any title', 'any message', 'any image', { })
end
@@ -72,5 +72,57 @@ def self.show(options) end
subject.notify('notify', 'any title', 'any message', 'any image', { })
end
+
+ it 'calls display_message if the display_message flag is set' do
+ subject.stub :system => true
+ subject.should_receive(:display_message).with('notify', 'any title', 'any message', { :display_message => true })
+
+ subject.notify('notify', 'any title', 'any message', 'any image', { :display_message => true })
+ end
+
+ it 'does not call display message if the display_message flag is not set' do
+ subject.stub :system => true
+ subject.should_receive(:display_message).never
+
+ subject.notify('notify', 'any title', 'any message', 'any image', { })
+ end
+ end
+
+ describe '.display_message' do
+ before do
+ subject.stub :system => true
+ end
+
+ it 'sets the display-time' do
+ subject.should_receive(:system).with('tmux set display-time 3000').once
+ subject.display_message 'success', 'any title', 'any message', :timeout => 3
+ end
+
+ it 'sets the background color' do
+ subject.stub :tmux_color => 'blue'
+ subject.should_receive(:system).with('tmux set message-bg blue').once
+ subject.display_message 'success', 'any title', 'any message'
+ end
+
+ it 'displays the message' do
+ subject.should_receive(:system).with('tmux display-message \'any title - any message\'').once
+ subject.display_message 'success', 'any title', 'any message'
+ end
+
+ it 'formats the message' do
+ subject.should_receive(:system).with('tmux display-message \'(any title) -> any message - line two\'').once
+ subject.display_message 'success', 'any title', "any message\nline two", :default_message_format => '(%s) -> %s'
+ end
+
+ it 'formats the message based on type' do
+ subject.should_receive(:system).with('tmux display-message \'[any title] => any message - line two\'').once
+ subject.display_message 'success', 'any title', "any message\nline two", :success_message_format => '[%s] => %s', :default_message_format => '(%s) -> %s'
+ end
+
+ it 'handles line-breaks' do
+ subject.should_receive(:system).with('tmux display-message \'any title - any message xx line two\'').once
+ subject.display_message 'success', 'any title', "any message\nline two", :line_separator => ' xx '
+ end
+
end
end
Something went wrong with that request. Please try again.