yummi 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,8 +35,9 @@ Examples:
35
35
 
36
36
  Line patterns are configured with an yaml file containing:
37
37
 
38
- * prefix (optional): prefix for pattern
39
- * suffix (optional): suffix for pattern
38
+ * prefix (optional): prefix for pattern
39
+ * suffix (optional): suffix for pattern
40
+ * options (optional): option flags to use
40
41
  * patterns: a pattern => color map
41
42
 
42
43
  Example:
@@ -50,13 +51,13 @@ Example:
50
51
  ERROR : red
51
52
  FATAL : intense_red
52
53
 
53
- Yummi provides a set of mappings that you, check yummi/mappings dir.
54
+ Yummi provides a set of patterns, check yummi/patterns dir.
54
55
 
55
- Mappings provided by yummi can be passed with the file name, example:
56
56
 
57
- tail -f $JBOSS_HOME/standalone/log/server.log | yummi -p jboss7
57
+ tail -f $JBOSS_HOME/standalone/log/server.log | yummi -p jboss
58
58
 
59
- Mappings in ~/.yummi dir may also used only with the file name.
59
+ Patterns in ~/.yummi/patterns and provided by yummi may also be used by passing
60
+ only the file name without extension
60
61
 
61
62
  ## Contributing
62
63
 
data/bin/yummi CHANGED
@@ -31,8 +31,11 @@ opt = OptionParser::new
31
31
  opt.on '-c COLOR', '--color=COLOR', 'Colorize using the given color' do |color|
32
32
  @color = color
33
33
  end
34
- opt.on '-p PATTERN', '--pattern=PATTERN', 'Sets a pattern to colorize each line' do |pattern|
35
- @colorizer = Yummi::Colorizers.line pattern
34
+ opt.on '-p PATTERNS', '--pattern=PATTERNS', Array, 'Sets a pattern to colorize each line' do |patterns|
35
+ @colorizer = Yummi::Colorizers.pattern(patterns)
36
+ end
37
+ opt.on '--log', 'Uses the default log pattern to colorize each line' do
38
+ @colorizer = Yummi::Colorizers.pattern :log
36
39
  end
37
40
  opt.on '-m message', '--message=MESSAGE', 'Colorize the given message' do |message|
38
41
  @message = message
@@ -46,7 +49,7 @@ end
46
49
  opt.on '--data-type', 'Defines the data type to parse the values' do |data_type|
47
50
  @data_type = data_type
48
51
  end
49
- opt.on '-e FILES', '--eval=FILES', Array, 'Include the files for extending components' do |files|
52
+ opt.on '-l FILES', '--load=FILES', Array, 'Include the files for extending components' do |files|
50
53
  files.each do |file|
51
54
  load File.expand_path(file)
52
55
  end
@@ -77,12 +80,9 @@ elsif @table_builder
77
80
  abort "Please give the data to print" unless @data
78
81
  table = @table_builder.build_table
79
82
  extension = File::extname(@data)[1..-1]
80
- table.data = case (@data_type or extension)
81
- when "yaml"
82
- YAML::load_file(@data)
83
- when "csv"
84
- require 'csv'
85
- CSV::parse(File.read(@data), :converters => :all)
83
+ type = (@data_type or extension).to_sym
84
+ if Yummi::DataParser.respond_to? type
85
+ table.data = Yummi::DataParser.send(type, File.read(@data))
86
86
  else
87
87
  abort "Unsupported extension #{extension}"
88
88
  end
@@ -93,6 +93,6 @@ else
93
93
  print_out line.chomp
94
94
  end
95
95
  rescue Interrupt
96
- #Do nothing
96
+ puts Yummi::colorize("Aborted!", :red)
97
97
  end
98
98
  end
@@ -22,22 +22,10 @@
22
22
 
23
23
  require_relative '../lib/yummi'
24
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
25
  log = <<LOG
38
26
  [INFO] Starting
39
27
  [WARN] Example output
40
- [DEGUB] Connecting to server
28
+ [DEBUG] Connecting to server
41
29
  [ERROR] Error while connecting
42
30
  caused by: ConnectionException
43
31
  at Connection.connect
@@ -48,8 +36,16 @@ caused by: ConnectionException
48
36
  [DEBUG] Shutdown command executed
49
37
  LOG
50
38
 
51
- colorizer = Yummi::Colorizers.line(params)
39
+ colorizer = Yummi::Colorizers.pattern :prefix => /\[/,
40
+ :suffix => /\]/,
41
+ :patterns => {
42
+ 'ERROR' => :red,
43
+ 'FATAL' => :intense_red,
44
+ 'WARN' => :yellow,
45
+ 'INFO' => :green,
46
+ 'DEBUG' => :gray
47
+ }
52
48
 
53
49
  log.each_line do |line|
54
50
  puts colorizer.colorize(line.chomp)
55
- end
51
+ end
@@ -84,9 +84,9 @@ module Yummi
84
84
  StripeColorizer::new(*colors)
85
85
  end
86
86
 
87
- # Returns a new instance of #LineColorizer
88
- def self.line mappings
89
- LineColorizer::new mappings
87
+ # Returns a new instance of #PatternColorizer
88
+ def self.pattern mappings
89
+ PatternColorizer::new mappings
90
90
  end
91
91
 
92
92
  #
@@ -213,14 +213,19 @@ module Yummi
213
213
  end
214
214
 
215
215
  #
216
- # A colorizer for lines that follows a pattern. This colorizer is usefull
216
+ # A colorizer for strings that follows a pattern. This colorizer is usefull
217
217
  # for log files.
218
218
  #
219
- class LineColorizer
219
+ class PatternColorizer
220
220
  include Yummi::Colorizer
221
221
 
222
+ def initialize mappings = nil
223
+ @patterns = []
224
+ map mappings if mappings
225
+ end
226
+
222
227
  #
223
- # Creates a new instance using the parameters.
228
+ # Maps a set of patterns to colors.
224
229
  #
225
230
  # === Args
226
231
  #
@@ -228,49 +233,88 @@ module Yummi
228
233
  #
229
234
  # * prefix: a pattern prefix
230
235
  # * suffix: a pattern suffix
236
+ # * options: option flags to use
237
+ # * mode: the mode to use:
238
+ # - all : colorize the entire text
239
+ # - grep : colorize only the matched text
231
240
  # * patterns: a pattern => color hash
232
241
  #
233
- # If a string is passed, a file containing a YAML configuration
234
- # will be loaded. If the string doesn't represent a file, the
235
- # following patterns will be used to find it:
242
+ # If a string is passed instead of a hash, a file containing a
243
+ # YAML configuration will be loaded. If the string doesn't represent
244
+ # a file, the following patterns will be used to find it:
236
245
  #
237
- # * $HOME/.yummi/PATTERN.yaml
238
- # * $YUMMI_GEM/yummy/mappings/PATTERN.yaml
246
+ # * $HOME/.yummi/patterns/PATTERN.yaml
247
+ # * $YUMMI_GEM/yummy/patterns/PATTERN.yaml
239
248
  #
240
- def initialize params
241
- unless params.is_a? Hash
242
- file = File.expand_path params.to_s
243
- if File.exist? file
244
- params = YAML::load_file file
245
- else
246
- ['~/.yummi', File.join(File.dirname(__FILE__), 'mappings')].each do |path|
247
- file = File.join(path, "#{params}.yaml")
248
- if File.exist? file
249
- params = YAML::load_file file
250
- break
251
- end
249
+ def map params
250
+ if params.is_a? Array
251
+ params.each { |p| map p }
252
+ elsif params.is_a? String or params.is_a? Symbol
253
+ map Yummi::Helpers::load_resource params, :from => :patterns
254
+ else
255
+ config params
256
+ end
257
+ end
258
+
259
+ def call ctx
260
+ ctx = Yummi::Context::new(ctx) unless ctx.is_a? Context
261
+ text = ctx.value.to_s
262
+ @patterns.each do |config|
263
+ config[:patterns].each do |regex, color|
264
+ if regex.match(text)
265
+ return match(text, config)
252
266
  end
253
267
  end
254
268
  end
255
- patterns = (params[:patterns] or params['patterns'])
256
- prefix = (params[:prefix] or params['prefix'])
257
- suffix = (params[:suffix] or params['suffix'])
258
- @patterns = Hash[*(patterns.collect do |pattern, color|
259
- [/#{prefix}#{pattern.to_s}#{suffix}/, color]
269
+ return text.colorize(@last_color) if @last_color
270
+ text
271
+ end
272
+
273
+ alias :colorize :call
274
+
275
+ private
276
+
277
+ def config params
278
+ Yummi::Helpers.symbolize_keys(params)
279
+ prefix = params[:prefix]
280
+ suffix = params[:suffix]
281
+ options = params[:options]
282
+ mode = (params[:mode] or :all)
283
+
284
+ patterns = Hash[*(params[:patterns].collect do |pattern, color|
285
+ [
286
+ Regexp::new("#{prefix}#{pattern.to_s}#{suffix}",options),
287
+ color
288
+ ]
260
289
  end).flatten]
261
290
 
262
- @last_color = nil
291
+ @patterns << {
292
+ :mode => mode,
293
+ :patterns => patterns
294
+ }
263
295
  end
264
296
 
265
- def call ctx
266
- line = ctx.value.to_s
267
- @patterns.each do |regex, color|
268
- if regex.match(line)
297
+ def match(text, config)
298
+ send "colorize_#{config[:mode]}", text, config
299
+ end
300
+
301
+ def colorize_all(text, match)
302
+ match[:patterns].each do |regex, color|
303
+ if regex.match(text)
269
304
  @last_color = color
270
- return color
305
+ return text.colorize(color)
271
306
  end
272
307
  end
273
- @last_color
308
+ text
309
+ end
310
+
311
+ def colorize_grep(text, match)
312
+ return text unless match
313
+ @last_color = nil
314
+ match[:patterns].each do |regex, color|
315
+ text = text.colorize regex => color
316
+ end
317
+ text
274
318
  end
275
319
 
276
320
  end
@@ -0,0 +1,37 @@
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
+ module Yummi
24
+
25
+ module DataParser
26
+
27
+ def self.yaml(input)
28
+ YAML::load(input)
29
+ end
30
+
31
+ def self.csv(input)
32
+ require 'csv' #autoload will be dead, using require instead
33
+ CSV::parse(input, :converters => :all)
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,40 @@
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
+ class String
24
+
25
+ def uncolorize
26
+ Yummi::uncolorize self
27
+ end
28
+
29
+ def colorize params
30
+ if params.is_a? Hash
31
+ text = self
32
+ params.each do |regexp, color|
33
+ text = text.gsub(regexp, "\\0".colorize(color))
34
+ end
35
+ return text
36
+ end
37
+ Yummi::colorize self, params
38
+ end
39
+
40
+ end
data/lib/yummi/logger.rb CHANGED
@@ -46,9 +46,9 @@ module Yummi
46
46
  #
47
47
  # Creates a new formatter with the colors (assuming Linux default terminal colors):
48
48
  #
49
- # * +DEBUG+: no color
50
- # * +INFO+: green
51
- # * +WARN+: brown
49
+ # * +DEBUG+: blue
50
+ # * +INFO+: white
51
+ # * +WARN+: yellow
52
52
  # * +ERROR+: red
53
53
  # * +FATAL+: red (intense)
54
54
  # * +ANY+: gray (intense)
@@ -58,8 +58,8 @@ module Yummi
58
58
  #
59
59
  def initialize &block
60
60
  @colors = {
61
- :debug => nil,
62
- :info => :green,
61
+ :debug => :blue,
62
+ :info => :white,
63
63
  :warn => :yellow,
64
64
  :error => :red,
65
65
  :fatal => :intense_red,
@@ -1,8 +1,8 @@
1
- prefix: '\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3}\s'
1
+ prefix: '(\d{4}-\d{2}-\d{2}\s)?\d{2}:\d{2}:\d{2},\d{3}\s'
2
2
  patterns:
3
3
  TRACE : cyan
4
4
  DEBUG : blue
5
- INFO : gray
5
+ INFO : white
6
6
  WARN : yellow
7
7
  ERROR : red
8
8
  FATAL : intense_red
@@ -0,0 +1,9 @@
1
+ prefix: '.*'
2
+ options: i
3
+ patterns:
4
+ TRACE : cyan
5
+ DEBUG : blue
6
+ INFO : white
7
+ WARN : yellow
8
+ ERROR : red
9
+ FATAL : intense_red
@@ -3,10 +3,10 @@ suffix: '>'
3
3
  patterns:
4
4
  Trace : purple
5
5
  Debug : blue
6
- Info : gray
6
+ Info : white
7
7
  Notice : cyan
8
8
  Warning : yellow
9
9
  Error : red
10
10
  Critical : intense_red
11
- Alert : intense_underline_red
12
- Emergency : highlight_red
11
+ Alert : intense_red
12
+ Emergency : intense_red
data/lib/yummi/table.rb CHANGED
@@ -41,7 +41,7 @@ module Yummi
41
41
  # * Values: using :value key
42
42
  #
43
43
  # The colors must be supported by #Yummi#Color#parse or defined in #Yummi#Color#COLORS
44
- attr_accessor :colors
44
+ attr_accessor :style
45
45
  # The table layout (horizontal or vertical)
46
46
  attr_reader :layout
47
47
  # The table header
@@ -59,7 +59,7 @@ module Yummi
59
59
  @header = []
60
60
  @title = nil
61
61
  @description = nil
62
- @colors = {
62
+ @style = {
63
63
  :title => :intense_yellow,
64
64
  :description => :intense_gray,
65
65
  :header => :intense_blue,
@@ -80,7 +80,7 @@ module Yummi
80
80
 
81
81
  # Indicates that the table should not use colors.
82
82
  def no_colors
83
- @colors = {
83
+ @style = {
84
84
  :title => nil,
85
85
  :header => nil,
86
86
  :value => nil
@@ -355,8 +355,8 @@ module Yummi
355
355
  data_output = build_data_output
356
356
 
357
357
  string = ""
358
- string << Color.colorize(@title, @colors[:title]) << $/ if @title
359
- string << Color.colorize(@description, @colors[:description]) << $/ if @description
358
+ string << Color.colorize(@title, @style[:title]) << $/ if @title
359
+ string << Color.colorize(@description, @style[:description]) << $/ if @description
360
360
  table_data = header_output + data_output
361
361
  if @layout == :vertical
362
362
  # don't use array transpose because the data may differ in each line size
@@ -430,7 +430,7 @@ module Yummi
430
430
  @header.each do |line|
431
431
  _data = []
432
432
  line.each do |h|
433
- _data << {:value => h, :color => @colors[:header]}
433
+ _data << {:value => h, :color => @style[:header]}
434
434
  end
435
435
  output << _data
436
436
  end
@@ -485,7 +485,7 @@ module Yummi
485
485
  elsif colorizer
486
486
  color = colorizer.call(column)
487
487
  else
488
- color = @colors[:value]
488
+ color = @style[:value]
489
489
  end
490
490
  formatter = component[:formatters][col_index]
491
491
  formatter = component[:null_formatter] if column.nil? and @null_formatter
@@ -151,7 +151,7 @@ module Yummi
151
151
  width = 0
152
152
  sizes = [] # the real size of each line
153
153
  content.each do |line|
154
- size = (Yummi::Color::raw line.chomp).size
154
+ size = line.chomp.uncolorize.size
155
155
  sizes << size
156
156
  width = [width, size].max
157
157
  end
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.6.1"
24
+ VERSION = "0.7.0"
25
25
  end
data/lib/yummi.rb CHANGED
@@ -30,8 +30,8 @@ module Yummi
30
30
  module Schema
31
31
  # Normal Linux Terminal Colors, used by default in normal color types
32
32
  NORMAL_COLORS = {
33
- :colors => [:black, :red, :green, :yellow, :blue, :purple, :cyan, [:gray, :white]],
34
- :default => :gray
33
+ :colors => [:black, :red, :green, [:yellow, :orange], :blue, :purple, :cyan, [:gray, :white]],
34
+ :default => :white
35
35
  }
36
36
  # Intense Linux Terminal Colors, used by default in bold color types
37
37
  ALTERNATE_COLORS = {
@@ -97,21 +97,17 @@ module Yummi
97
97
 
98
98
  # Escape the given text with the given color code
99
99
  def self.escape key
100
- return key unless key and COLORS[key.to_s.to_sym]
101
- "\e[#{COLORS[key.to_s.to_sym]}m"
100
+ return key unless key and COLORS[key.to_sym]
101
+ "\e[#{COLORS[key.to_sym]}m"
102
102
  end
103
103
 
104
104
  # Colorize the given text with the given color
105
105
  def self.colorize string, color
106
+ return string if color.to_s == 'none'
106
107
  color, end_color = [color, "\e[0;0m"].map { |key| Color.escape(key) }
107
108
  color ? "#{color}#{string}#{end_color}" : string
108
109
  end
109
110
 
110
- # Extracts the text from a colorized string
111
- def self.raw string
112
- string.gsub(/\e\[\d;\d{2}m/, '').gsub(/\e\[0;0m/, '')
113
- end
114
-
115
111
  end
116
112
 
117
113
  #
@@ -136,6 +132,13 @@ module Yummi
136
132
  end
137
133
  end
138
134
 
135
+ #
136
+ # Extracts the text from a colorized string
137
+ #
138
+ def self.uncolorize string
139
+ string.gsub(/\e\[\d;\d{2}m/, '').gsub(/\e\[0;0m/, '')
140
+ end
141
+
139
142
  # A module to align texts based on a reference width
140
143
  module Aligner
141
144
 
@@ -301,6 +304,23 @@ module Yummi
301
304
 
302
305
  module Helpers
303
306
 
307
+ def self.load_resource name, params = {:from => ''}
308
+ file = File.expand_path name.to_s
309
+ if File.exist? file
310
+ return YAML::load_file(file)
311
+ else
312
+ from = params[:from].to_s
313
+ [
314
+ File.join(File.expand_path('~/.yummi'), from),
315
+ File.join(File.dirname(__FILE__), 'yummi', from)
316
+ ].each do |path|
317
+ file = File.join(path, "#{name}.yaml")
318
+ return YAML::load_file(file) if File.exist?(file)
319
+ end
320
+ end
321
+ raise Exception::new("Unable to load #{name}")
322
+ end
323
+
304
324
  def self.symbolize_keys hash
305
325
  hash.replace(hash.inject({}) do |h, (k, v)|
306
326
  v = symbolize_keys(v) if v.is_a? Hash
@@ -322,6 +342,8 @@ end
322
342
 
323
343
  require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
324
344
 
345
+ require_relative 'yummi/extensions'
346
+ require_relative 'yummi/data_parser'
325
347
  require_relative "yummi/colorizers"
326
348
  require_relative "yummi/formatters"
327
349
  require_relative 'yummi/color_mapping'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yummi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email:
@@ -39,12 +39,14 @@ files:
39
39
  - lib/yummi.rb
40
40
  - lib/yummi/color_mapping.rb
41
41
  - lib/yummi/colorizers.rb
42
+ - lib/yummi/data_parser.rb
43
+ - lib/yummi/extensions.rb
42
44
  - lib/yummi/formatters.rb
43
45
  - lib/yummi/logger.rb
44
- - lib/yummi/mappings/jboss5.yaml
45
- - lib/yummi/mappings/jboss7.yaml
46
- - lib/yummi/mappings/weblogic11g.yaml
47
46
  - lib/yummi/no_colors.rb
47
+ - lib/yummi/patterns/jboss.yaml
48
+ - lib/yummi/patterns/log.yaml
49
+ - lib/yummi/patterns/weblogic.yaml
48
50
  - lib/yummi/table.rb
49
51
  - lib/yummi/table_builder.rb
50
52
  - lib/yummi/text_box.rb
@@ -1,8 +0,0 @@
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