yummi 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,6 +16,6 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  *~
19
- *.iml
20
- *.ipr
21
19
  *.iws
20
+ *.sublime-project
21
+ *.sublime-workspace
data/bin/colorize CHANGED
@@ -25,10 +25,15 @@
25
25
  require_relative '../lib/yummi'
26
26
  require 'optparse'
27
27
 
28
+ @params = []
29
+
28
30
  opt = OptionParser::new
29
31
  opt.on '-c COLOR', '--color=COLOR', 'Colorize using the given color' do |color|
30
32
  @color = color
31
33
  end
34
+ opt.on '-p PATTERN', '--pattern=PATTERN', 'Sets a pattern to colorize each line' do |pattern|
35
+ @colorizer = Yummi::Colorizers.line pattern
36
+ end
32
37
  opt.on '-t TEXT', '--text=TEXT', 'Colorize the given text' do |text|
33
38
  @text = text
34
39
  end
@@ -38,14 +43,26 @@ opt.on '-h', '--help', 'Display this help message' do
38
43
  end
39
44
  opt.parse! ARGV
40
45
 
41
- if @color
46
+ def print_out text
47
+ if @color
48
+ puts Yummi::colorize text, @color
49
+ elsif @colorizer
50
+ puts @colorizer.colorize text
51
+ end
52
+ end
53
+
54
+ if @color or @colorizer
42
55
  if @text
43
- puts Yummi::colorize @text, @color
56
+ print_out @text
44
57
  else
45
- ARGF.each_line do |line|
46
- puts Yummi::colorize line.chomp, @color
58
+ begin
59
+ ARGF.each_line do |line|
60
+ print_out line.chomp
61
+ end
62
+ rescue Interrupt
63
+ #Do nothing
47
64
  end
48
65
  end
49
66
  else
50
- abort 'Please give a color.'
67
+ abort 'Please give a color or choose a pattern.'
51
68
  end
@@ -57,7 +57,7 @@ def full_colors
57
57
  # colorize the values based on comparison
58
58
  red_to_negative = lambda { |value| :red if value < 0 }
59
59
  green_to_positive = lambda { |value| :green if value > 0 }
60
- brown_to_zero = lambda { |value| :brown if value == 0 }
60
+ brown_to_zero = lambda { |value| :yellow if value == 0 }
61
61
  @table.colorize [:value, :total], :using => Yummi::Colorizers.join(
62
62
  red_to_negative, green_to_positive, brown_to_zero
63
63
  )
@@ -69,7 +69,7 @@ def full_colors
69
69
  end
70
70
 
71
71
  def zebra_colors
72
- @table.row_colorizer Yummi::Colorizers.stripe :brown, :purple
72
+ @table.row_colorizer Yummi::Colorizers.stripe :yellow, :purple
73
73
  end
74
74
 
75
75
  def no_colors
@@ -44,7 +44,7 @@ end
44
44
 
45
45
  opt.parse! ARGV
46
46
 
47
- @box.add 'The MIT License', :color => :yellow, :align => :center
47
+ @box.add 'The MIT License', :color => :intense_yellow, :align => :center
48
48
  @box.line_break
49
49
  @box.add 'Copyright (c) 2012 Marcelo Guimaraes <ataxexe@gmail.com>', :color => :green, :align => :center
50
50
  @box.separator
@@ -0,0 +1,55 @@
1
+ # The MIT License
2
+ #
3
+ # Copyright (c) 2012 Marcelo Guimarães <ataxexe@gmail.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require_relative '../lib/yummi'
24
+
25
+ params = {
26
+ :prefix => /\[/,
27
+ :suffix => /\]/,
28
+ :patterns => {
29
+ 'ERROR' => :red,
30
+ 'FATAL' => :intense_red,
31
+ 'WARN' => :yellow,
32
+ 'INFO' => :green,
33
+ 'DEBUG' => :gray
34
+ }
35
+ }
36
+
37
+ log = <<LOG
38
+ [INFO] Starting
39
+ [WARN] Example output
40
+ [DEGUB] Connecting to server
41
+ [ERROR] Error while connecting
42
+ caused by: ConnectionException
43
+ at Connection.connect
44
+ at Startup
45
+ [INFO] Could not connect to server
46
+ [FATAL] A fatal error
47
+ [WARN] Application shutdown
48
+ [DEBUG] Shutdown command executed
49
+ LOG
50
+
51
+ colorizer = Yummi::Colorizers.line(params)
52
+
53
+ log.each_line do |line|
54
+ puts colorizer.colorize(line.chomp)
55
+ end
@@ -0,0 +1,260 @@
1
+ # The MIT License
2
+ #
3
+ # Copyright (c) 2012 Marcelo Guimarães <ataxexe@gmail.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'yaml'
24
+
25
+ module Yummi
26
+ #
27
+ # A module that defines a colorizer capable component.
28
+ #
29
+ # Include this module in any component that returns a color in response for :call:.
30
+ #
31
+ module Colorizer
32
+
33
+ #
34
+ # Colorizes a string by passing the arguments to the :call: method to get the proper
35
+ # color.
36
+ #
37
+ # === Args
38
+ #
39
+ # An array of arguments that will be passed to :call: method to get the color. By
40
+ # convention, the first argument must be the object to colorize (to_s is called on it
41
+ # for getting the text to colorize).
42
+ #
43
+ def colorize (*args)
44
+ Yummi.colorize args.first.to_s, color_for(args)
45
+ end
46
+
47
+ #
48
+ # Returns the color for the given value
49
+ #
50
+ # === Args
51
+ #
52
+ # An array of arguments that will be passed to :call: method to get the color. By
53
+ # convention, the first argument must be the object to colorize (to_s is called on it
54
+ # for getting the text to colorize).#
55
+ def color_for (*args)
56
+ call *args
57
+ end
58
+
59
+ end
60
+
61
+ #
62
+ # Adds the #Colorizer module to the given block, so you can use it to colorize texts.
63
+ #
64
+ # === Example
65
+ #
66
+ # colorizer = Yummi.to_colorize { |value| value % 2 == 0 ? :green : :blue }
67
+ # 10.times { |n| puts colorizer.colorize n }
68
+ #
69
+ def self.to_colorize &block
70
+ block.extend Colorizer
71
+ end
72
+
73
+ # A module with useful colorizers
74
+ module Colorizers
75
+
76
+ # Joins the given colorizers to work as one
77
+ def self.join *colorizers
78
+ join = Yummi::GroupedComponent::new
79
+ colorizers.each { |c| join << c }
80
+ join.extend Colorizer
81
+ end
82
+
83
+ # Returns a new instance of #DataEvalColorizer
84
+ def self.by_data_eval &block
85
+ DataEvalColorizer::new(&block)
86
+ end
87
+
88
+ # Returns a new instance of #EvalColorizer
89
+ def self.by_eval &block
90
+ EvalColorizer::new(&block)
91
+ end
92
+
93
+ # Returns a new instance of #StripeColorizer
94
+ def self.stripe *colors
95
+ StripeColorizer::new(*colors)
96
+ end
97
+
98
+ # Returns a new instance of #LineColorizer
99
+ def self.line mappings
100
+ LineColorizer::new mappings
101
+ end
102
+
103
+ #
104
+ # A colorizer for lines that follows a pattern. This colorizer is usefull
105
+ # for log files.
106
+ #
107
+ class LineColorizer
108
+ include Yummi::Colorizer
109
+
110
+ #
111
+ # Creates a new instance using the parameters.
112
+ #
113
+ # === Args
114
+ #
115
+ # A hash containing the following keys:
116
+ #
117
+ # * prefix: a pattern prefix
118
+ # * suffix: a pattern suffix
119
+ # * patterns: a pattern => color hash
120
+ #
121
+ # If a string is passed, a file containing a YAML configuration
122
+ # will be loaded. If the string doesn't represent a file, the
123
+ # following patterns will be used to find it:
124
+ #
125
+ # * $HOME/.yummi/PATTERN.yaml
126
+ # * $YUMMI_GEM/yummy/mappings/PATTERN.yaml
127
+ #
128
+ def initialize params
129
+ unless params.is_a? Hash
130
+ file = File.expand_path params.to_s
131
+ if File.exist? file
132
+ params = YAML::load_file file
133
+ else
134
+ ['~/.yummi', File.join(File.dirname(__FILE__), 'mappings')].each do |path|
135
+ file = File.join(path, "#{params}.yaml")
136
+ if File.exist? file
137
+ params = YAML::load_file file
138
+ break
139
+ end
140
+ end
141
+ end
142
+ end
143
+ patterns = (params[:patterns] or params['patterns'])
144
+ prefix = (params[:prefix] or params['prefix'])
145
+ suffix = (params[:suffix] or params['suffix'])
146
+ @patterns = Hash[*(patterns.collect do |pattern, color|
147
+ [/#{prefix}#{pattern.to_s}#{suffix}/, color]
148
+ end).flatten]
149
+
150
+ @last_color = nil
151
+ end
152
+
153
+ def call *args
154
+ line = args.first.to_s
155
+ @patterns.each do |regex, color|
156
+ if regex.match(line)
157
+ @last_color = color
158
+ return color
159
+ end
160
+ end
161
+ @last_color
162
+ end
163
+
164
+ end
165
+
166
+ # A colorizer that cycles through colors to create a striped effect
167
+ class StripeColorizer
168
+ include Yummi::Colorizer
169
+
170
+ # Creates a new colorizer using the given colors
171
+ def initialize (*colors)
172
+ @colors = colors
173
+ @count = -1
174
+ end
175
+
176
+ def call *args
177
+ @count += 1
178
+ @colors[@count % @colors.size]
179
+ end
180
+
181
+ end
182
+
183
+ #
184
+ # A colorizer that evaluates a main block and returns a color based on other blocks.
185
+ #
186
+ # The main block must be compatible with the colorizing type (receiving a column
187
+ # value in case of a table column colorizer or the row index and row value in case
188
+ # of a table row colorizer).
189
+ #
190
+ # === Example
191
+ #
192
+ # # assuming that the table has :max and :current aliases
193
+ # colorizer = DataEvalColorizer::new { |index, data| data[:current] / data[:max] }
194
+ # # the result of the expression above will be passed to this block
195
+ # colorizer.use(:red) { |value| value >= 0.9 }
196
+ #
197
+ # table.using_row.colorize :current, :using => colorizer
198
+ #
199
+ class EvalColorizer
200
+ include Yummi::Colorizer
201
+
202
+ def initialize (&block)
203
+ @block = block
204
+ @colors = []
205
+ @eval_blocks = []
206
+ end
207
+
208
+ #
209
+ # Uses the given color if the given block returns something when evaluated with the
210
+ # result of main block.
211
+ #
212
+ # An objtect that responds to :call may also be used.
213
+ #
214
+ def use (color, component = nil, &eval_block)
215
+ @colors << color
216
+ @eval_blocks << (component or eval_block)
217
+ end
218
+
219
+ # Resolves the value using the main block and given arguments
220
+ def resolve_value (*args)
221
+ @block.call *args
222
+ end
223
+
224
+ def call (*args)
225
+ value = resolve_value *args
226
+ @eval_blocks.each_index do |i|
227
+ return @colors[i] if @eval_blocks[i].call(value)
228
+ end
229
+ nil
230
+ end
231
+
232
+ end
233
+
234
+ #
235
+ # A colorizer that evaluates a main block and returns a color based on other blocks.
236
+ #
237
+ # The main block can receive any parameters and the names must be aliases the current
238
+ # evaluated data.
239
+ #
240
+ # === Example
241
+ #
242
+ # # assuming that the table has :max and :current aliases
243
+ # colorizer = DataEvalColorizer::new { |max, current| current / max }
244
+ # # the result of the expression above will be passed to this block
245
+ # colorizer.use(:red) { |value| value >= 0.9 }
246
+ #
247
+ # table.using_row.colorize :current, :using => colorizer
248
+ #
249
+ class DataEvalColorizer < EvalColorizer
250
+ include Yummi::BlockHandler, Yummi::Colorizer
251
+
252
+ def resolve_value (*args)
253
+ block_call args.first, &@block # by convention, the first arg is data
254
+ end
255
+
256
+ end
257
+
258
+ end
259
+
260
+ end
@@ -0,0 +1,8 @@
1
+ prefix: '\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3}\s'
2
+ patterns:
3
+ TRACE : cyan
4
+ DEBUG : blue
5
+ INFO : gray
6
+ WARN : yellow
7
+ ERROR : red
8
+ FATAL : intense_red
@@ -0,0 +1,8 @@
1
+ prefix: '\d{2}:\d{2}:\d{2},\d{3}\s'
2
+ patterns:
3
+ TRACE : cyan
4
+ DEBUG : blue
5
+ INFO : gray
6
+ WARN : yellow
7
+ ERROR : red
8
+ FATAL : intense_red
@@ -0,0 +1,12 @@
1
+ prefix: '####<[^>]+>\s<'
2
+ suffix: '>'
3
+ patterns:
4
+ Trace : purple
5
+ Debug : blue
6
+ Info : gray
7
+ Notice : cyan
8
+ Warning : yellow
9
+ Error : red
10
+ Critical : intense_red
11
+ Alert : highlight_red
12
+ Emergency : blink_red
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.4.2"
24
+ VERSION = "0.4.3"
25
25
  end
data/lib/yummi.rb CHANGED
@@ -31,7 +31,7 @@ module Yummi
31
31
  # Normal Linux Terminal Colors, used by default in normal, underscore, blink and
32
32
  # highlight color types
33
33
  NORMAL_COLORS = {
34
- :colors => [:black, :red, :green, :brown, :blue, :purple, :cyan, :gray],
34
+ :colors => [:black, :red, :green, :yellow, :blue, :purple, :cyan, :gray],
35
35
  :default => :gray
36
36
  }
37
37
  # Intense Linux Terminal Colors, used by default in intense and strong color types
@@ -137,7 +137,7 @@ module Yummi
137
137
  block.parameters.each do |parameter|
138
138
  args << context[parameter[1]]
139
139
  end
140
- block.call *args
140
+ block.call(*args)
141
141
  end
142
142
 
143
143
  module_function :block_call
@@ -202,172 +202,6 @@ module Yummi
202
202
 
203
203
  end
204
204
 
205
- #
206
- # A module that defines a colorizer capable component.
207
- #
208
- # Include this module in any component that returns a color in response for :call:.
209
- #
210
- module Colorizer
211
-
212
- #
213
- # Colorizes a string by passing the arguments to the :call: method to get the proper
214
- # color.
215
- #
216
- # === Args
217
- #
218
- # An array of arguments that will be passed to :call: method to get the color. By
219
- # convention, the first argument must be the object to colorize (to_s is called on it
220
- # for getting the text to colorize).
221
- #
222
- def colorize (*args)
223
- Yummi.colorize args.first.to_s, color_for(args)
224
- end
225
-
226
- #
227
- # Returns the color for the given value
228
- #
229
- # === Args
230
- #
231
- # An array of arguments that will be passed to :call: method to get the color. By
232
- # convention, the first argument must be the object to colorize (to_s is called on it
233
- # for getting the text to colorize).#
234
- def color_for (*args)
235
- call *args
236
- end
237
-
238
- end
239
-
240
- #
241
- # Adds the #Colorizer module to the given block, so you can use it to colorize texts.
242
- #
243
- # === Example
244
- #
245
- # colorizer = Yummi.to_colorize { |value| value % 2 == 0 ? :green : :blue }
246
- # 10.times { |n| puts colorizer.colorize n }
247
- #
248
- def self.to_colorize &block
249
- block.extend Colorizer
250
- end
251
-
252
- # A module with useful colorizers
253
- module Colorizers
254
-
255
- # Joins the given colorizers to work as one
256
- def self.join *colorizers
257
- join = Yummi::GroupedComponent::new
258
- colorizers.each { |c| join << c }
259
- join.extend Colorizer
260
- end
261
-
262
- # Returns a new instance of #DataEvalColorizer
263
- def self.by_data_eval &block
264
- DataEvalColorizer::new &block
265
- end
266
-
267
- # Returns a new instance of #EvalColorizer
268
- def self.by_eval &block
269
- EvalColorizer::new &block
270
- end
271
-
272
- # Returns a new instance of #StripeColorizer
273
- def self.stripe *colors
274
- StripeColorizer::new *colors
275
- end
276
-
277
- # A colorizer that cycles through colors to create a striped effect
278
- class StripeColorizer
279
- include Yummi::Colorizer
280
-
281
- # Creates a new colorizer using the given colors
282
- def initialize (*colors)
283
- @colors = colors
284
- @count = -1
285
- end
286
-
287
- def call *args
288
- @count += 1
289
- @colors[@count % @colors.size]
290
- end
291
-
292
- end
293
-
294
- #
295
- # A colorizer that evaluates a main block and returns a color based on other blocks.
296
- #
297
- # The main block must be compatible with the colorizing type (receiving a column
298
- # value in case of a table column colorizer or the row index and row value in case
299
- # of a table row colorizer).
300
- #
301
- # === Example
302
- #
303
- # # assuming that the table has :max and :current aliases
304
- # colorizer = DataEvalColorizer::new { |index, data| data[:current] / data[:max] }
305
- # # the result of the expression above will be passed to this block
306
- # colorizer.use(:red) { |value| value >= 0.9 }
307
- #
308
- # table.using_row.colorize :current, :using => colorizer
309
- #
310
- class EvalColorizer
311
- include Yummi::Colorizer
312
-
313
- def initialize (&block)
314
- @block = block
315
- @colors = []
316
- @eval_blocks = []
317
- end
318
-
319
- #
320
- # Uses the given color if the given block returns something when evaluated with the
321
- # result of main block.
322
- #
323
- # An objtect that responds to :call may also be used.
324
- #
325
- def use (color, component = nil, &eval_block)
326
- @colors << color
327
- @eval_blocks << (component or eval_block)
328
- end
329
-
330
- # Resolves the value using the main block and given arguments
331
- def resolve_value (*args)
332
- @block.call *args
333
- end
334
-
335
- def call (*args)
336
- value = resolve_value *args
337
- @eval_blocks.each_index do |i|
338
- return @colors[i] if @eval_blocks[i].call(value)
339
- end
340
- nil
341
- end
342
-
343
- end
344
-
345
- #
346
- # A colorizer that evaluates a main block and returns a color based on other blocks.
347
- #
348
- # The main block can receive any parameters and the names must be aliases the current
349
- # evaluated data.
350
- #
351
- # === Example
352
- #
353
- # # assuming that the table has :max and :current aliases
354
- # colorizer = DataEvalColorizer::new { |max, current| current / max }
355
- # # the result of the expression above will be passed to this block
356
- # colorizer.use(:red) { |value| value >= 0.9 }
357
- #
358
- # table.using_row.colorize :current, :using => colorizer
359
- #
360
- class DataEvalColorizer < EvalColorizer
361
- include Yummi::BlockHandler, Yummi::Colorizer
362
-
363
- def resolve_value (*args)
364
- block_call args.first, &@block # by convention, the first arg is data
365
- end
366
-
367
- end
368
-
369
- end
370
-
371
205
  # A module used to create an alias method in a formatter block
372
206
  module FormatterBlock
373
207
 
@@ -503,6 +337,7 @@ end
503
337
 
504
338
  require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
505
339
 
340
+ require_relative "yummi/colorizers"
506
341
  require_relative 'yummi/color_mapping'
507
342
  require_relative 'yummi/table'
508
343
  require_relative 'yummi/text_box'