guard 2.7.2 → 2.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,278 @@
1
+ require "lumberjack"
2
+
3
+ require "guard/options"
4
+ require "guard/ui/colors"
5
+
6
+ require "guard/terminal"
7
+
8
+ module Guard
9
+ # The UI class helps to format messages for the user. Everything that is
10
+ # logged through this class is considered either as an error message or a
11
+ # diagnostic message and is written to standard error ($stderr).
12
+ #
13
+ # If your Guard plugin does some output that is piped into another process
14
+ # for further processing, please just write it to STDOUT with `puts`.
15
+ #
16
+ module UI
17
+ include Colors
18
+
19
+ class << self
20
+ # Get the Guard::UI logger instance
21
+ #
22
+ def logger
23
+ @logger ||= begin
24
+ Lumberjack::Logger.new(
25
+ options.fetch(:device) { $stderr },
26
+ options)
27
+ end
28
+ end
29
+
30
+ # Since logger is global, for Aruba in-process to properly
31
+ # separate output between calls, we need to reset
32
+ #
33
+ # We don't use logger=() since it's expected to be a Lumberjack instance
34
+ def reset_logger
35
+ @logger = nil
36
+ end
37
+
38
+ # Get the logger options
39
+ #
40
+ # @return [Hash] the logger options
41
+ #
42
+ def options
43
+ @options ||= ::Guard::Options.new(
44
+ level: :info,
45
+ template: ":time - :severity - :message",
46
+ time_format: "%H:%M:%S")
47
+ end
48
+
49
+ # Set the logger options
50
+ #
51
+ # @param [Hash] options the logger options
52
+ # @option options [Symbol] level the log level
53
+ # @option options [String] template the logger template
54
+ # @option options [String] time_format the time format
55
+ #
56
+ def options=(options)
57
+ @options = ::Guard::Options.new(options)
58
+ end
59
+
60
+ # Show an info message.
61
+ #
62
+ # @param [String] message the message to show
63
+ # @option options [Boolean] reset whether to clean the output before
64
+ # @option options [String] plugin manually define the calling plugin
65
+ #
66
+ def info(message, options = {})
67
+ _filtered_logger_message(message, :info, nil, options)
68
+ end
69
+
70
+ # Show a yellow warning message that is prefixed with WARNING.
71
+ #
72
+ # @param [String] message the message to show
73
+ # @option options [Boolean] reset whether to clean the output before
74
+ # @option options [String] plugin manually define the calling plugin
75
+ #
76
+ def warning(message, options = {})
77
+ _filtered_logger_message(message, :warn, :yellow, options)
78
+ end
79
+
80
+ # Show a red error message that is prefixed with ERROR.
81
+ #
82
+ # @param [String] message the message to show
83
+ # @option options [Boolean] reset whether to clean the output before
84
+ # @option options [String] plugin manually define the calling plugin
85
+ #
86
+ def error(message, options = {})
87
+ _filtered_logger_message(message, :error, :red, options)
88
+ end
89
+
90
+ # Show a red deprecation message that is prefixed with DEPRECATION.
91
+ # It has a log level of `warn`.
92
+ #
93
+ # @param [String] message the message to show
94
+ # @option options [Boolean] reset whether to clean the output before
95
+ # @option options [String] plugin manually define the calling plugin
96
+ #
97
+ def deprecation(message, options = {})
98
+ msg = "neither ::Guard.setup nor ::Guard.reset_options was called"
99
+ fail msg if ::Guard.options.nil?
100
+ warning(message, options) if ::Guard.options[:show_deprecations]
101
+ end
102
+
103
+ # Show a debug message that is prefixed with DEBUG and a timestamp.
104
+ #
105
+ # @param [String] message the message to show
106
+ # @option options [Boolean] reset whether to clean the output before
107
+ # @option options [String] plugin manually define the calling plugin
108
+ #
109
+ def debug(message, options = {})
110
+ _filtered_logger_message(message, :debug, :yellow, options)
111
+ end
112
+
113
+ # Reset a line.
114
+ #
115
+ def reset_line
116
+ $stderr.print(color_enabled? ? "\r\e[0m" : "\r\n")
117
+ end
118
+
119
+ # Clear the output if clearable.
120
+ #
121
+ def clear(opts = {})
122
+ return unless ::Guard.options[:clear]
123
+
124
+ fail "UI not set up!" if @clearable.nil?
125
+ return unless @clearable || opts[:force]
126
+
127
+ @clearable = false
128
+ ::Guard::Terminal.clear
129
+ rescue Errno::ENOENT => e
130
+ warning("Failed to clear the screen: #{e.inspect}")
131
+ end
132
+
133
+ # TODO: arguments: UI uses Guard::options anyway
134
+ def setup(_options)
135
+ @clearable = false
136
+ clear(force: true)
137
+ end
138
+
139
+ # Allow the screen to be cleared again.
140
+ #
141
+ def clearable
142
+ @clearable = true
143
+ end
144
+
145
+ # Show a scoped action message.
146
+ #
147
+ # @param [String] action the action to show
148
+ # @param [Hash] scope hash with a guard or a group scope
149
+ #
150
+ def action_with_scopes(action, scope)
151
+ first_non_blank_scope = _first_non_blank_scope(scope)
152
+ unless first_non_blank_scope.nil?
153
+ scope_message = first_non_blank_scope.map(&:title).join(", ")
154
+ end
155
+
156
+ info "#{ action } #{ scope_message || "all" }"
157
+ end
158
+
159
+ private
160
+
161
+ # Returns the first non-blank scope by searching in the given `scope`
162
+ # hash and in Guard.scope. Returns nil if no non-blank scope is found.
163
+ #
164
+ def _first_non_blank_scope(scope)
165
+ [:plugins, :groups].each do |scope_name|
166
+ s = scope[scope_name] || ::Guard.scope[scope_name]
167
+ return s if !s.nil? && !s.empty?
168
+ end
169
+
170
+ nil
171
+ end
172
+
173
+ # Filters log messages depending on either the
174
+ # `:only`` or `:except` option.
175
+ #
176
+ # @param [String] plugin the calling plugin name
177
+ # @yield When the message should be logged
178
+ # @yieldparam [String] param the calling plugin name
179
+ #
180
+ def _filter(plugin)
181
+ only = options[:only]
182
+ except = options[:except]
183
+ plugin ||= calling_plugin_name
184
+
185
+ match = !(only || except)
186
+ match ||= (only && only.match(plugin))
187
+ match ||= (except && !except.match(plugin))
188
+ return unless match
189
+ yield plugin
190
+ end
191
+
192
+ # Display a message of the type `method` and with the color `color_name`
193
+ # (no color by default) conditionnaly given a `plugin_name`.
194
+ #
195
+ # @param [String] plugin_name the calling plugin name
196
+ # @option options [Boolean] reset whether to clean the output before
197
+ # @option options [String] plugin manually define the calling plugin
198
+ #
199
+ def _filtered_logger_message(message, method, color_name, options = {})
200
+ message = color(message, color_name) if color_name
201
+
202
+ _filter(options[:plugin]) do |plugin|
203
+ reset_line if options[:reset]
204
+ logger.send(method, message, plugin)
205
+ end
206
+ end
207
+
208
+ # Tries to extract the calling Guard plugin name
209
+ # from the call stack.
210
+ #
211
+ # @param [Integer] depth the stack depth
212
+ # @return [String] the Guard plugin name
213
+ #
214
+ def calling_plugin_name(depth = 2)
215
+ name = /(guard\/[a-z_]*)(\/[a-z_]*)?.rb:/i.match(caller[depth])
216
+ return "Guard" unless name
217
+ name[1].split("/").map do |part|
218
+ part.split(/[^a-z0-9]/i).map(&:capitalize).join
219
+ end.join("::")
220
+ end
221
+
222
+ # Checks if color output can be enabled.
223
+ #
224
+ # @return [Boolean] whether color is enabled or not
225
+ #
226
+ def color_enabled?
227
+ @color_enabled_initialized ||= false
228
+ @color_enabled = nil unless @color_enabled_initialized
229
+ @color_enabled_initialized = true
230
+ if @color_enabled.nil?
231
+ if Gem.win_platform?
232
+ if ENV["ANSICON"]
233
+ @color_enabled = true
234
+ else
235
+ begin
236
+ require "rubygems" unless ENV["NO_RUBYGEMS"]
237
+ require "Win32/Console/ANSI"
238
+ @color_enabled = true
239
+ rescue LoadError
240
+ @color_enabled = false
241
+ info "Run 'gem install win32console' to use color on Windows"
242
+ end
243
+ end
244
+ else
245
+ @color_enabled = true
246
+ end
247
+ end
248
+
249
+ @color_enabled
250
+ end
251
+
252
+ # Colorizes a text message. See the constant in the UI class for possible
253
+ # color_options parameters. You can pass optionally :bright, a foreground
254
+ # color and a background color.
255
+ #
256
+ # @example
257
+ #
258
+ # color('Hello World', :red, :bright)
259
+ #
260
+ # @param [String] text the text to colorize
261
+ # @param [Array] color_options the color options
262
+ #
263
+ def color(text, *color_options)
264
+ color_code = ""
265
+ color_options.each do |color_option|
266
+ color_option = color_option.to_s
267
+ next if color_option == ""
268
+
269
+ unless color_option =~ /\d+/
270
+ color_option = const_get("ANSI_ESCAPE_#{ color_option.upcase }")
271
+ end
272
+ color_code += ";" + color_option
273
+ end
274
+ color_enabled? ? "\e[0#{ color_code }m#{ text }\e[0m" : text
275
+ end
276
+ end
277
+ end
278
+ end
data/lib/guard/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Guard
2
- VERSION = "2.7.2"
2
+ VERSION = "2.7.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.2
4
+ version: 2.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thibaud Guillaume-Gentil
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-03 00:00:00.000000000 Z
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -97,25 +97,36 @@ files:
97
97
  - images/pending.png
98
98
  - images/success.png
99
99
  - lib/guard.rb
100
+ - lib/guard.rb.orig
100
101
  - lib/guard/aruba_adapter.rb
101
102
  - lib/guard/cli.rb
103
+ - lib/guard/cli.rb.orig
102
104
  - lib/guard/commander.rb
105
+ - lib/guard/commander.rb.orig
103
106
  - lib/guard/commands/all.rb
107
+ - lib/guard/commands/all.rb.orig
104
108
  - lib/guard/commands/change.rb
105
109
  - lib/guard/commands/notification.rb
106
110
  - lib/guard/commands/pause.rb
107
111
  - lib/guard/commands/reload.rb
112
+ - lib/guard/commands/reload.rb.orig
108
113
  - lib/guard/commands/scope.rb
114
+ - lib/guard/commands/scope.rb.orig
109
115
  - lib/guard/commands/show.rb
110
116
  - lib/guard/deprecated_methods.rb
117
+ - lib/guard/deprecated_methods.rb.orig
111
118
  - lib/guard/deprecator.rb
119
+ - lib/guard/deprecator.rb.orig
112
120
  - lib/guard/dsl.rb
113
121
  - lib/guard/dsl_describer.rb
114
122
  - lib/guard/dsl_describer.rb.orig
115
123
  - lib/guard/group.rb
116
124
  - lib/guard/guard.rb
125
+ - lib/guard/guard.rb.orig
117
126
  - lib/guard/guardfile.rb
127
+ - lib/guard/guardfile.rb.orig
118
128
  - lib/guard/guardfile/evaluator.rb
129
+ - lib/guard/guardfile/evaluator.rb.orig
119
130
  - lib/guard/guardfile/generator.rb
120
131
  - lib/guard/interactor.rb
121
132
  - lib/guard/jobs/base.rb
@@ -123,6 +134,7 @@ files:
123
134
  - lib/guard/jobs/sleep.rb
124
135
  - lib/guard/notifier.rb
125
136
  - lib/guard/notifiers/base.rb
137
+ - lib/guard/notifiers/base.rb.orig
126
138
  - lib/guard/notifiers/emacs.rb
127
139
  - lib/guard/notifiers/file_notifier.rb
128
140
  - lib/guard/notifiers/gntp.rb
@@ -138,14 +150,18 @@ files:
138
150
  - lib/guard/plugin/base.rb
139
151
  - lib/guard/plugin/hooker.rb
140
152
  - lib/guard/plugin_util.rb
153
+ - lib/guard/plugin_util.rb.orig
141
154
  - lib/guard/rake_task.rb
142
155
  - lib/guard/reevaluator.rb
143
156
  - lib/guard/runner.rb
157
+ - lib/guard/runner.rb.orig
144
158
  - lib/guard/setuper.rb
159
+ - lib/guard/setuper.rb.orig
145
160
  - lib/guard/sheller.rb
146
161
  - lib/guard/templates/Guardfile
147
162
  - lib/guard/terminal.rb
148
163
  - lib/guard/ui.rb
164
+ - lib/guard/ui.rb.orig
149
165
  - lib/guard/ui/colors.rb
150
166
  - lib/guard/version.rb
151
167
  - lib/guard/version.rb.orig