/
tmux.rb
69 lines (63 loc) · 2.18 KB
/
tmux.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 = { })
color = tmux_color type, options
system("#{ DEFAULTS[:client] } set -g status-left-bg #{ color }")
end
# Get the Tmux color for the notification type.
# You can configure your own color by overwriting the defaults.
#
# @param [String] type the notification type
# @return [String] the name of the emacs color
#
def tmux_color(type, options = { })
case type
when 'success'
options[:success] || DEFAULTS[:success]
when 'failed'
options[:failed] || DEFAULTS[:failed]
else
options[:default] || DEFAULTS[:default]
end
end
end
end
end