guard 2.10.2 → 2.10.3

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