guard 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +310 -303
- data/LICENSE +19 -19
- data/README.md +434 -434
- data/bin/guard +6 -6
- data/lib/guard.rb +384 -384
- data/lib/guard/cli.rb +178 -179
- data/lib/guard/dsl.rb +370 -370
- data/lib/guard/dsl_describer.rb +60 -60
- data/lib/guard/group.rb +22 -22
- data/lib/guard/guard.rb +98 -98
- data/lib/guard/hook.rb +118 -118
- data/lib/guard/interactor.rb +78 -78
- data/lib/guard/listener.rb +346 -346
- data/lib/guard/listeners/darwin.rb +66 -66
- data/lib/guard/listeners/linux.rb +98 -98
- data/lib/guard/listeners/polling.rb +55 -55
- data/lib/guard/listeners/windows.rb +61 -61
- data/lib/guard/notifier.rb +211 -211
- data/lib/guard/templates/Guardfile +2 -2
- data/lib/guard/ui.rb +188 -188
- data/lib/guard/version.rb +6 -6
- data/lib/guard/watcher.rb +110 -110
- data/man/guard.1 +93 -93
- data/man/guard.1.html +176 -176
- metadata +15 -15
data/lib/guard/notifier.rb
CHANGED
@@ -1,211 +1,211 @@
|
|
1
|
-
require 'rbconfig'
|
2
|
-
require 'pathname'
|
3
|
-
require 'guard/ui'
|
4
|
-
|
5
|
-
module Guard
|
6
|
-
|
7
|
-
# The notifier class handles cross-platform system notifications that supports:
|
8
|
-
#
|
9
|
-
# - Growl on Mac OS X
|
10
|
-
# - Libnotify on Linux
|
11
|
-
# - Notifu on Windows
|
12
|
-
#
|
13
|
-
module Notifier
|
14
|
-
|
15
|
-
# Application name as shown in the specific notification settings
|
16
|
-
APPLICATION_NAME = "Guard"
|
17
|
-
|
18
|
-
# Turn notifications off.
|
19
|
-
#
|
20
|
-
def self.turn_off
|
21
|
-
ENV["GUARD_NOTIFY"] = 'false'
|
22
|
-
end
|
23
|
-
|
24
|
-
# Turn notifications on. This tries to load the platform
|
25
|
-
# specific notification library.
|
26
|
-
#
|
27
|
-
# @return [Boolean] whether the notification could be enabled.
|
28
|
-
#
|
29
|
-
def self.turn_on
|
30
|
-
ENV["GUARD_NOTIFY"] = 'true'
|
31
|
-
case RbConfig::CONFIG['target_os']
|
32
|
-
when /darwin/i
|
33
|
-
require_growl
|
34
|
-
when /linux/i
|
35
|
-
require_libnotify
|
36
|
-
when /mswin|mingw/i
|
37
|
-
require_rbnotifu
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Show a message with the system notification.
|
42
|
-
#
|
43
|
-
# @see .image_path
|
44
|
-
#
|
45
|
-
# @param [String] the message to show
|
46
|
-
# @option options [Symbol, String] image the image symbol or path to an image
|
47
|
-
# @option options [String] title the notification title
|
48
|
-
#
|
49
|
-
def self.notify(message, options = {})
|
50
|
-
if enabled?
|
51
|
-
image = options.delete(:image) || :success
|
52
|
-
title = options.delete(:title) || "Guard"
|
53
|
-
|
54
|
-
case RbConfig::CONFIG['target_os']
|
55
|
-
when /darwin/i
|
56
|
-
notify_mac(title, message, image, options)
|
57
|
-
when /linux/i
|
58
|
-
notify_linux(title, message, image, options)
|
59
|
-
when /mswin|mingw/i
|
60
|
-
notify_windows(title, message, image, options)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# Test if the notifications are enabled and available.
|
66
|
-
#
|
67
|
-
# @return [Boolean] whether the notifications are available
|
68
|
-
#
|
69
|
-
def self.enabled?
|
70
|
-
ENV["GUARD_NOTIFY"] == 'true'
|
71
|
-
end
|
72
|
-
|
73
|
-
private
|
74
|
-
|
75
|
-
# Send a message to Growl either with the `growl` gem or the `growl_notify` gem.
|
76
|
-
#
|
77
|
-
# @param [String] title the notification title
|
78
|
-
# @param [String] message the message to show
|
79
|
-
# @param [Symbol, String] the image to user
|
80
|
-
# @param [Hash] options the growl options
|
81
|
-
#
|
82
|
-
def self.notify_mac(title, message, image, options = {})
|
83
|
-
require_growl # need for guard-rspec formatter that is called out of guard scope
|
84
|
-
|
85
|
-
default_options = { :title => title, :icon => image_path(image), :name => APPLICATION_NAME }
|
86
|
-
default_options.merge!(options)
|
87
|
-
|
88
|
-
if defined?(GrowlNotify)
|
89
|
-
default_options[:description] = message
|
90
|
-
default_options[:application_name] = APPLICATION_NAME
|
91
|
-
default_options.delete(:name)
|
92
|
-
|
93
|
-
GrowlNotify.send_notification(default_options) if enabled?
|
94
|
-
else
|
95
|
-
Growl.notify message, default_options.merge(options) if enabled?
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# Send a message to libnotify.
|
100
|
-
#
|
101
|
-
# @param [String] title the notification title
|
102
|
-
# @param [String] message the message to show
|
103
|
-
# @param [Symbol, String] the image to user
|
104
|
-
# @param [Hash] options the libnotify options
|
105
|
-
#
|
106
|
-
def self.notify_linux(title, message, image, options = {})
|
107
|
-
require_libnotify # need for guard-rspec formatter that is called out of guard scope
|
108
|
-
default_options = { :body => message, :summary => title, :icon_path => image_path(image), :transient => true }
|
109
|
-
Libnotify.show default_options.merge(options) if enabled?
|
110
|
-
end
|
111
|
-
|
112
|
-
# Send a message to notifu.
|
113
|
-
#
|
114
|
-
# @param [String] title the notification title
|
115
|
-
# @param [String] message the message to show
|
116
|
-
# @param [Symbol, String] the image to user
|
117
|
-
# @param [Hash] options the notifu options
|
118
|
-
#
|
119
|
-
def self.notify_windows(title, message, image, options = {})
|
120
|
-
require_rbnotifu # need for guard-rspec formatter that is called out of guard scope
|
121
|
-
default_options = { :message => message, :title => title, :type => image_level(image), :time => 3 }
|
122
|
-
Notifu.show default_options.merge(options) if enabled?
|
123
|
-
end
|
124
|
-
|
125
|
-
# Get the image path for an image symbol.
|
126
|
-
#
|
127
|
-
# Known symbols are:
|
128
|
-
#
|
129
|
-
# - failed
|
130
|
-
# - pending
|
131
|
-
# - success
|
132
|
-
#
|
133
|
-
# @param [Symbol] image the image name
|
134
|
-
# @return [String] the image path
|
135
|
-
#
|
136
|
-
def self.image_path(image)
|
137
|
-
images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
|
138
|
-
case image
|
139
|
-
when :failed
|
140
|
-
images_path.join("failed.png").to_s
|
141
|
-
when :pending
|
142
|
-
images_path.join("pending.png").to_s
|
143
|
-
when :success
|
144
|
-
images_path.join("success.png").to_s
|
145
|
-
else
|
146
|
-
# path given
|
147
|
-
image
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
# The notification level type for the given image.
|
152
|
-
#
|
153
|
-
# @param [Symbol] image the image
|
154
|
-
# @return [Symbol] the level
|
155
|
-
#
|
156
|
-
def self.image_level(image)
|
157
|
-
case image
|
158
|
-
when :failed
|
159
|
-
:error
|
160
|
-
when :pending
|
161
|
-
:warn
|
162
|
-
when :success
|
163
|
-
:info
|
164
|
-
else
|
165
|
-
:info
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
# Try to safely load growl and turns notifications
|
170
|
-
# off on load failure.
|
171
|
-
#
|
172
|
-
def self.require_growl
|
173
|
-
begin
|
174
|
-
require 'growl_notify'
|
175
|
-
|
176
|
-
if GrowlNotify.application_name != APPLICATION_NAME
|
177
|
-
GrowlNotify.config do |c|
|
178
|
-
c.notifications = c.default_notifications = [ APPLICATION_NAME ]
|
179
|
-
c.application_name = c.notifications.first
|
180
|
-
end
|
181
|
-
end
|
182
|
-
rescue LoadError
|
183
|
-
require 'growl'
|
184
|
-
end
|
185
|
-
rescue LoadError
|
186
|
-
turn_off
|
187
|
-
UI.info "Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile"
|
188
|
-
end
|
189
|
-
|
190
|
-
# Try to safely load libnotify and turns notifications
|
191
|
-
# off on load failure.
|
192
|
-
#
|
193
|
-
def self.require_libnotify
|
194
|
-
require 'libnotify'
|
195
|
-
rescue LoadError
|
196
|
-
turn_off
|
197
|
-
UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
|
198
|
-
end
|
199
|
-
|
200
|
-
# Try to safely load rb-notifu and turns notifications
|
201
|
-
# off on load failure.
|
202
|
-
#
|
203
|
-
def self.require_rbnotifu
|
204
|
-
require 'rb-notifu'
|
205
|
-
rescue LoadError
|
206
|
-
turn_off
|
207
|
-
UI.info "Please install rb-notifu gem for Windows notification support and add it to your Gemfile"
|
208
|
-
end
|
209
|
-
|
210
|
-
end
|
211
|
-
end
|
1
|
+
require 'rbconfig'
|
2
|
+
require 'pathname'
|
3
|
+
require 'guard/ui'
|
4
|
+
|
5
|
+
module Guard
|
6
|
+
|
7
|
+
# The notifier class handles cross-platform system notifications that supports:
|
8
|
+
#
|
9
|
+
# - Growl on Mac OS X
|
10
|
+
# - Libnotify on Linux
|
11
|
+
# - Notifu on Windows
|
12
|
+
#
|
13
|
+
module Notifier
|
14
|
+
|
15
|
+
# Application name as shown in the specific notification settings
|
16
|
+
APPLICATION_NAME = "Guard"
|
17
|
+
|
18
|
+
# Turn notifications off.
|
19
|
+
#
|
20
|
+
def self.turn_off
|
21
|
+
ENV["GUARD_NOTIFY"] = 'false'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Turn notifications on. This tries to load the platform
|
25
|
+
# specific notification library.
|
26
|
+
#
|
27
|
+
# @return [Boolean] whether the notification could be enabled.
|
28
|
+
#
|
29
|
+
def self.turn_on
|
30
|
+
ENV["GUARD_NOTIFY"] = 'true'
|
31
|
+
case RbConfig::CONFIG['target_os']
|
32
|
+
when /darwin/i
|
33
|
+
require_growl
|
34
|
+
when /linux/i
|
35
|
+
require_libnotify
|
36
|
+
when /mswin|mingw/i
|
37
|
+
require_rbnotifu
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Show a message with the system notification.
|
42
|
+
#
|
43
|
+
# @see .image_path
|
44
|
+
#
|
45
|
+
# @param [String] the message to show
|
46
|
+
# @option options [Symbol, String] image the image symbol or path to an image
|
47
|
+
# @option options [String] title the notification title
|
48
|
+
#
|
49
|
+
def self.notify(message, options = {})
|
50
|
+
if enabled?
|
51
|
+
image = options.delete(:image) || :success
|
52
|
+
title = options.delete(:title) || "Guard"
|
53
|
+
|
54
|
+
case RbConfig::CONFIG['target_os']
|
55
|
+
when /darwin/i
|
56
|
+
notify_mac(title, message, image, options)
|
57
|
+
when /linux/i
|
58
|
+
notify_linux(title, message, image, options)
|
59
|
+
when /mswin|mingw/i
|
60
|
+
notify_windows(title, message, image, options)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Test if the notifications are enabled and available.
|
66
|
+
#
|
67
|
+
# @return [Boolean] whether the notifications are available
|
68
|
+
#
|
69
|
+
def self.enabled?
|
70
|
+
ENV["GUARD_NOTIFY"] == 'true'
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# Send a message to Growl either with the `growl` gem or the `growl_notify` gem.
|
76
|
+
#
|
77
|
+
# @param [String] title the notification title
|
78
|
+
# @param [String] message the message to show
|
79
|
+
# @param [Symbol, String] the image to user
|
80
|
+
# @param [Hash] options the growl options
|
81
|
+
#
|
82
|
+
def self.notify_mac(title, message, image, options = {})
|
83
|
+
require_growl # need for guard-rspec formatter that is called out of guard scope
|
84
|
+
|
85
|
+
default_options = { :title => title, :icon => image_path(image), :name => APPLICATION_NAME }
|
86
|
+
default_options.merge!(options)
|
87
|
+
|
88
|
+
if defined?(GrowlNotify)
|
89
|
+
default_options[:description] = message
|
90
|
+
default_options[:application_name] = APPLICATION_NAME
|
91
|
+
default_options.delete(:name)
|
92
|
+
|
93
|
+
GrowlNotify.send_notification(default_options) if enabled?
|
94
|
+
else
|
95
|
+
Growl.notify message, default_options.merge(options) if enabled?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Send a message to libnotify.
|
100
|
+
#
|
101
|
+
# @param [String] title the notification title
|
102
|
+
# @param [String] message the message to show
|
103
|
+
# @param [Symbol, String] the image to user
|
104
|
+
# @param [Hash] options the libnotify options
|
105
|
+
#
|
106
|
+
def self.notify_linux(title, message, image, options = {})
|
107
|
+
require_libnotify # need for guard-rspec formatter that is called out of guard scope
|
108
|
+
default_options = { :body => message, :summary => title, :icon_path => image_path(image), :transient => true }
|
109
|
+
Libnotify.show default_options.merge(options) if enabled?
|
110
|
+
end
|
111
|
+
|
112
|
+
# Send a message to notifu.
|
113
|
+
#
|
114
|
+
# @param [String] title the notification title
|
115
|
+
# @param [String] message the message to show
|
116
|
+
# @param [Symbol, String] the image to user
|
117
|
+
# @param [Hash] options the notifu options
|
118
|
+
#
|
119
|
+
def self.notify_windows(title, message, image, options = {})
|
120
|
+
require_rbnotifu # need for guard-rspec formatter that is called out of guard scope
|
121
|
+
default_options = { :message => message, :title => title, :type => image_level(image), :time => 3 }
|
122
|
+
Notifu.show default_options.merge(options) if enabled?
|
123
|
+
end
|
124
|
+
|
125
|
+
# Get the image path for an image symbol.
|
126
|
+
#
|
127
|
+
# Known symbols are:
|
128
|
+
#
|
129
|
+
# - failed
|
130
|
+
# - pending
|
131
|
+
# - success
|
132
|
+
#
|
133
|
+
# @param [Symbol] image the image name
|
134
|
+
# @return [String] the image path
|
135
|
+
#
|
136
|
+
def self.image_path(image)
|
137
|
+
images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
|
138
|
+
case image
|
139
|
+
when :failed
|
140
|
+
images_path.join("failed.png").to_s
|
141
|
+
when :pending
|
142
|
+
images_path.join("pending.png").to_s
|
143
|
+
when :success
|
144
|
+
images_path.join("success.png").to_s
|
145
|
+
else
|
146
|
+
# path given
|
147
|
+
image
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# The notification level type for the given image.
|
152
|
+
#
|
153
|
+
# @param [Symbol] image the image
|
154
|
+
# @return [Symbol] the level
|
155
|
+
#
|
156
|
+
def self.image_level(image)
|
157
|
+
case image
|
158
|
+
when :failed
|
159
|
+
:error
|
160
|
+
when :pending
|
161
|
+
:warn
|
162
|
+
when :success
|
163
|
+
:info
|
164
|
+
else
|
165
|
+
:info
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Try to safely load growl and turns notifications
|
170
|
+
# off on load failure.
|
171
|
+
#
|
172
|
+
def self.require_growl
|
173
|
+
begin
|
174
|
+
require 'growl_notify'
|
175
|
+
|
176
|
+
if GrowlNotify.application_name != APPLICATION_NAME
|
177
|
+
GrowlNotify.config do |c|
|
178
|
+
c.notifications = c.default_notifications = [ APPLICATION_NAME ]
|
179
|
+
c.application_name = c.notifications.first
|
180
|
+
end
|
181
|
+
end
|
182
|
+
rescue LoadError
|
183
|
+
require 'growl'
|
184
|
+
end
|
185
|
+
rescue LoadError
|
186
|
+
turn_off
|
187
|
+
UI.info "Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile"
|
188
|
+
end
|
189
|
+
|
190
|
+
# Try to safely load libnotify and turns notifications
|
191
|
+
# off on load failure.
|
192
|
+
#
|
193
|
+
def self.require_libnotify
|
194
|
+
require 'libnotify'
|
195
|
+
rescue LoadError
|
196
|
+
turn_off
|
197
|
+
UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
|
198
|
+
end
|
199
|
+
|
200
|
+
# Try to safely load rb-notifu and turns notifications
|
201
|
+
# off on load failure.
|
202
|
+
#
|
203
|
+
def self.require_rbnotifu
|
204
|
+
require 'rb-notifu'
|
205
|
+
rescue LoadError
|
206
|
+
turn_off
|
207
|
+
UI.info "Please install rb-notifu gem for Windows notification support and add it to your Gemfile"
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
# A sample Guardfile
|
2
|
-
# More info at https://github.com/guard/guard#readme
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
data/lib/guard/ui.rb
CHANGED
@@ -1,188 +1,188 @@
|
|
1
|
-
module Guard
|
2
|
-
|
3
|
-
# The UI class helps to format messages for the user.
|
4
|
-
#
|
5
|
-
module UI
|
6
|
-
class << self
|
7
|
-
|
8
|
-
color_enabled = nil
|
9
|
-
|
10
|
-
# Show an info message.
|
11
|
-
#
|
12
|
-
# @param [String] message the message to show
|
13
|
-
# @option options [Boolean] reset whether to clean the output before
|
14
|
-
#
|
15
|
-
def info(message, options = { })
|
16
|
-
unless ENV['GUARD_ENV'] == 'test'
|
17
|
-
reset_line if options[:reset]
|
18
|
-
puts color(message) if message != ''
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Show a red error message that is prefixed with ERROR.
|
23
|
-
#
|
24
|
-
# @param [String] message the message to show
|
25
|
-
# @option options [Boolean] reset whether to clean the output before
|
26
|
-
#
|
27
|
-
def error(message, options = { })
|
28
|
-
unless ENV['GUARD_ENV'] == 'test'
|
29
|
-
reset_line if options[:reset]
|
30
|
-
puts color('ERROR: ', :red) + message
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# Show a red deprecation message that is prefixed with DEPRECATION.
|
35
|
-
#
|
36
|
-
# @param [String] message the message to show
|
37
|
-
# @option options [Boolean] reset whether to clean the output before
|
38
|
-
#
|
39
|
-
def deprecation(message, options = { })
|
40
|
-
unless ENV['GUARD_ENV'] == 'test'
|
41
|
-
reset_line if options[:reset]
|
42
|
-
puts color('DEPRECATION: ', :red) + message
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Show a debug message that is prefixed with DEBUG and a timestamp.
|
47
|
-
#
|
48
|
-
# @param [String] message the message to show
|
49
|
-
# @option options [Boolean] reset whether to clean the output before
|
50
|
-
#
|
51
|
-
def debug(message, options = { })
|
52
|
-
unless ENV['GUARD_ENV'] == 'test'
|
53
|
-
reset_line if options[:reset]
|
54
|
-
puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# Reset a line.
|
59
|
-
#
|
60
|
-
def reset_line
|
61
|
-
print(color_enabled? ? "\r\e[0m" : "\r\n")
|
62
|
-
end
|
63
|
-
|
64
|
-
# Clear the output.
|
65
|
-
#
|
66
|
-
def clear
|
67
|
-
system('clear;')
|
68
|
-
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
# Reset a color sequence.
|
73
|
-
#
|
74
|
-
# @deprecated
|
75
|
-
# @param [String] text the text
|
76
|
-
#
|
77
|
-
def reset_color(text)
|
78
|
-
deprecation('UI.reset_color(text) is deprecated, please use color(text, ' ') instead.')
|
79
|
-
color(text, '')
|
80
|
-
end
|
81
|
-
|
82
|
-
# Checks if color output can be enabled.
|
83
|
-
#
|
84
|
-
# @return [Boolean] whether color is enabled or not
|
85
|
-
#
|
86
|
-
def color_enabled?
|
87
|
-
if @color_enabled.nil?
|
88
|
-
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
|
89
|
-
if ENV['ANSICON']
|
90
|
-
@color_enabled = true
|
91
|
-
else
|
92
|
-
begin
|
93
|
-
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
94
|
-
require 'Win32/Console/ANSI'
|
95
|
-
@color_enabled = true
|
96
|
-
rescue LoadError
|
97
|
-
@color_enabled = false
|
98
|
-
info "You must 'gem install win32console' to use color on Windows"
|
99
|
-
end
|
100
|
-
end
|
101
|
-
else
|
102
|
-
@color_enabled = true
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
@color_enabled
|
107
|
-
end
|
108
|
-
|
109
|
-
# Colorizes a text message. See the constant in the UI class for possible
|
110
|
-
# color_options parameters. You can pass optionally :bright, a foreground
|
111
|
-
# color and a background color.
|
112
|
-
#
|
113
|
-
# @example
|
114
|
-
#
|
115
|
-
# color('Hello World', :red, :bright)
|
116
|
-
#
|
117
|
-
# @param [String] the text to colorize
|
118
|
-
# @param [Array] color_options the color options
|
119
|
-
#
|
120
|
-
def color(text, *color_options)
|
121
|
-
color_code = ''
|
122
|
-
color_options.each do |color_option|
|
123
|
-
color_option = color_option.to_s
|
124
|
-
if color_option != ''
|
125
|
-
if !(color_option =~ /\d+/)
|
126
|
-
color_option = const_get("ANSI_ESCAPE_#{ color_option.upcase }")
|
127
|
-
end
|
128
|
-
color_code += ';' + color_option
|
129
|
-
end
|
130
|
-
end
|
131
|
-
color_enabled? ? "\e[0#{ color_code }m#{ text }\e[0m" : text
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
# Brighten the color
|
137
|
-
ANSI_ESCAPE_BRIGHT = '1'
|
138
|
-
|
139
|
-
# Black foreground color
|
140
|
-
ANSI_ESCAPE_BLACK = '30'
|
141
|
-
|
142
|
-
# Red foreground color
|
143
|
-
ANSI_ESCAPE_RED = '31'
|
144
|
-
|
145
|
-
# Green foreground color
|
146
|
-
ANSI_ESCAPE_GREEN = '32'
|
147
|
-
|
148
|
-
# Yellow foreground color
|
149
|
-
ANSI_ESCAPE_YELLOW = '33'
|
150
|
-
|
151
|
-
# Blue foreground color
|
152
|
-
ANSI_ESCAPE_BLUE = '34'
|
153
|
-
|
154
|
-
# Magenta foreground color
|
155
|
-
ANSI_ESCAPE_MAGENTA = '35'
|
156
|
-
|
157
|
-
# Cyan foreground color
|
158
|
-
ANSI_ESCAPE_CYAN = '36'
|
159
|
-
|
160
|
-
# White foreground color
|
161
|
-
ANSI_ESCAPE_WHITE = '37'
|
162
|
-
|
163
|
-
# Black background color
|
164
|
-
ANSI_ESCAPE_BGBLACK = '40'
|
165
|
-
|
166
|
-
# Red background color
|
167
|
-
ANSI_ESCAPE_BGRED = '41'
|
168
|
-
|
169
|
-
# Green background color
|
170
|
-
ANSI_ESCAPE_BGGREEN = '42'
|
171
|
-
|
172
|
-
# Yellow background color
|
173
|
-
ANSI_ESCAPE_BGYELLOW = '43'
|
174
|
-
|
175
|
-
# Blue background color
|
176
|
-
ANSI_ESCAPE_BGBLUE = '44'
|
177
|
-
|
178
|
-
# Magenta background color
|
179
|
-
ANSI_ESCAPE_BGMAGENTA = '45'
|
180
|
-
|
181
|
-
# Cyan background color
|
182
|
-
ANSI_ESCAPE_BGCYAN = '46'
|
183
|
-
|
184
|
-
# White background color
|
185
|
-
ANSI_ESCAPE_BGWHITE = '47'
|
186
|
-
|
187
|
-
end
|
188
|
-
end
|
1
|
+
module Guard
|
2
|
+
|
3
|
+
# The UI class helps to format messages for the user.
|
4
|
+
#
|
5
|
+
module UI
|
6
|
+
class << self
|
7
|
+
|
8
|
+
color_enabled = nil
|
9
|
+
|
10
|
+
# Show an info message.
|
11
|
+
#
|
12
|
+
# @param [String] message the message to show
|
13
|
+
# @option options [Boolean] reset whether to clean the output before
|
14
|
+
#
|
15
|
+
def info(message, options = { })
|
16
|
+
unless ENV['GUARD_ENV'] == 'test'
|
17
|
+
reset_line if options[:reset]
|
18
|
+
puts color(message) if message != ''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Show a red error message that is prefixed with ERROR.
|
23
|
+
#
|
24
|
+
# @param [String] message the message to show
|
25
|
+
# @option options [Boolean] reset whether to clean the output before
|
26
|
+
#
|
27
|
+
def error(message, options = { })
|
28
|
+
unless ENV['GUARD_ENV'] == 'test'
|
29
|
+
reset_line if options[:reset]
|
30
|
+
puts color('ERROR: ', :red) + message
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Show a red deprecation message that is prefixed with DEPRECATION.
|
35
|
+
#
|
36
|
+
# @param [String] message the message to show
|
37
|
+
# @option options [Boolean] reset whether to clean the output before
|
38
|
+
#
|
39
|
+
def deprecation(message, options = { })
|
40
|
+
unless ENV['GUARD_ENV'] == 'test'
|
41
|
+
reset_line if options[:reset]
|
42
|
+
puts color('DEPRECATION: ', :red) + message
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Show a debug message that is prefixed with DEBUG and a timestamp.
|
47
|
+
#
|
48
|
+
# @param [String] message the message to show
|
49
|
+
# @option options [Boolean] reset whether to clean the output before
|
50
|
+
#
|
51
|
+
def debug(message, options = { })
|
52
|
+
unless ENV['GUARD_ENV'] == 'test'
|
53
|
+
reset_line if options[:reset]
|
54
|
+
puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Reset a line.
|
59
|
+
#
|
60
|
+
def reset_line
|
61
|
+
print(color_enabled? ? "\r\e[0m" : "\r\n")
|
62
|
+
end
|
63
|
+
|
64
|
+
# Clear the output.
|
65
|
+
#
|
66
|
+
def clear
|
67
|
+
system('clear;')
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
# Reset a color sequence.
|
73
|
+
#
|
74
|
+
# @deprecated
|
75
|
+
# @param [String] text the text
|
76
|
+
#
|
77
|
+
def reset_color(text)
|
78
|
+
deprecation('UI.reset_color(text) is deprecated, please use color(text, ' ') instead.')
|
79
|
+
color(text, '')
|
80
|
+
end
|
81
|
+
|
82
|
+
# Checks if color output can be enabled.
|
83
|
+
#
|
84
|
+
# @return [Boolean] whether color is enabled or not
|
85
|
+
#
|
86
|
+
def color_enabled?
|
87
|
+
if @color_enabled.nil?
|
88
|
+
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
|
89
|
+
if ENV['ANSICON']
|
90
|
+
@color_enabled = true
|
91
|
+
else
|
92
|
+
begin
|
93
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
94
|
+
require 'Win32/Console/ANSI'
|
95
|
+
@color_enabled = true
|
96
|
+
rescue LoadError
|
97
|
+
@color_enabled = false
|
98
|
+
info "You must 'gem install win32console' to use color on Windows"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
else
|
102
|
+
@color_enabled = true
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
@color_enabled
|
107
|
+
end
|
108
|
+
|
109
|
+
# Colorizes a text message. See the constant in the UI class for possible
|
110
|
+
# color_options parameters. You can pass optionally :bright, a foreground
|
111
|
+
# color and a background color.
|
112
|
+
#
|
113
|
+
# @example
|
114
|
+
#
|
115
|
+
# color('Hello World', :red, :bright)
|
116
|
+
#
|
117
|
+
# @param [String] the text to colorize
|
118
|
+
# @param [Array] color_options the color options
|
119
|
+
#
|
120
|
+
def color(text, *color_options)
|
121
|
+
color_code = ''
|
122
|
+
color_options.each do |color_option|
|
123
|
+
color_option = color_option.to_s
|
124
|
+
if color_option != ''
|
125
|
+
if !(color_option =~ /\d+/)
|
126
|
+
color_option = const_get("ANSI_ESCAPE_#{ color_option.upcase }")
|
127
|
+
end
|
128
|
+
color_code += ';' + color_option
|
129
|
+
end
|
130
|
+
end
|
131
|
+
color_enabled? ? "\e[0#{ color_code }m#{ text }\e[0m" : text
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
# Brighten the color
|
137
|
+
ANSI_ESCAPE_BRIGHT = '1'
|
138
|
+
|
139
|
+
# Black foreground color
|
140
|
+
ANSI_ESCAPE_BLACK = '30'
|
141
|
+
|
142
|
+
# Red foreground color
|
143
|
+
ANSI_ESCAPE_RED = '31'
|
144
|
+
|
145
|
+
# Green foreground color
|
146
|
+
ANSI_ESCAPE_GREEN = '32'
|
147
|
+
|
148
|
+
# Yellow foreground color
|
149
|
+
ANSI_ESCAPE_YELLOW = '33'
|
150
|
+
|
151
|
+
# Blue foreground color
|
152
|
+
ANSI_ESCAPE_BLUE = '34'
|
153
|
+
|
154
|
+
# Magenta foreground color
|
155
|
+
ANSI_ESCAPE_MAGENTA = '35'
|
156
|
+
|
157
|
+
# Cyan foreground color
|
158
|
+
ANSI_ESCAPE_CYAN = '36'
|
159
|
+
|
160
|
+
# White foreground color
|
161
|
+
ANSI_ESCAPE_WHITE = '37'
|
162
|
+
|
163
|
+
# Black background color
|
164
|
+
ANSI_ESCAPE_BGBLACK = '40'
|
165
|
+
|
166
|
+
# Red background color
|
167
|
+
ANSI_ESCAPE_BGRED = '41'
|
168
|
+
|
169
|
+
# Green background color
|
170
|
+
ANSI_ESCAPE_BGGREEN = '42'
|
171
|
+
|
172
|
+
# Yellow background color
|
173
|
+
ANSI_ESCAPE_BGYELLOW = '43'
|
174
|
+
|
175
|
+
# Blue background color
|
176
|
+
ANSI_ESCAPE_BGBLUE = '44'
|
177
|
+
|
178
|
+
# Magenta background color
|
179
|
+
ANSI_ESCAPE_BGMAGENTA = '45'
|
180
|
+
|
181
|
+
# Cyan background color
|
182
|
+
ANSI_ESCAPE_BGCYAN = '46'
|
183
|
+
|
184
|
+
# White background color
|
185
|
+
ANSI_ESCAPE_BGWHITE = '47'
|
186
|
+
|
187
|
+
end
|
188
|
+
end
|