yummi 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -16,9 +16,47 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install yummi
18
18
 
19
- ## Usage
19
+ ## Ruby Tools
20
20
 
21
- Comming soon...
21
+ Yummi module provides a set of tools to colorize inputs. Check docs for more information
22
+ about how to build tables, text boxes, colored log formatters and more. You can also check
23
+ the examples dir to see how to use Yummi features.
24
+
25
+ ## Command Line Tools
26
+
27
+ Yummi exposes a 'colorize' program that you can use to colorize texts and apply
28
+ patterns to colorize lines (usefull to tail logs).
29
+
30
+ Examples:
31
+
32
+ colorize -c intense_red -m "some text"
33
+ echo "some text" | colorize -c intense_red
34
+ tail -f $JBOSS_HOME/standalone/log/server.log | colorize -p path-to-your-jboss7-mapping.yaml
35
+
36
+ Line patterns are configured with an yaml file containing:
37
+
38
+ * prefix (optional): prefix for pattern
39
+ * suffix (optional): suffix for pattern
40
+ * patterns: a pattern => color map
41
+
42
+ Example:
43
+
44
+ prefix: '\d{2}:\d{2}:\d{2},\d{3}\s'
45
+ patterns:
46
+ TRACE : cyan
47
+ DEBUG : blue
48
+ INFO : gray
49
+ WARN : yellow
50
+ ERROR : red
51
+ FATAL : intense_red
52
+
53
+ Yummi provides a set of mappings that you, check yummi/mappings dir.
54
+
55
+ Mappings provided by yummi can be passed with the file name, example:
56
+
57
+ tail -f $JBOSS_HOME/standalone/log/server.log | colorize -p jboss7
58
+
59
+ Mappings in ~/.yummi dir may also used only with the file name.
22
60
 
23
61
  ## Contributing
24
62
 
data/bin/colorize CHANGED
@@ -34,35 +34,65 @@ end
34
34
  opt.on '-p PATTERN', '--pattern=PATTERN', 'Sets a pattern to colorize each line' do |pattern|
35
35
  @colorizer = Yummi::Colorizers.line pattern
36
36
  end
37
- opt.on '-t TEXT', '--text=TEXT', 'Colorize the given text' do |text|
38
- @text = text
37
+ opt.on '-m message', '--message=MESSAGE', 'Colorize the given message' do |message|
38
+ @message = message
39
+ end
40
+ opt.on '-t file', '--table=FILE', 'Defines the file mapping the table to print' do |file|
41
+ @table_builder = Yummi::TableBuilder::new(file).defaults
42
+ end
43
+ opt.on '-d data', '--data=FILE', 'Defines the file containing the data to print' do |data|
44
+ @data = File.expand_path(data)
45
+ end
46
+ opt.on '--data-type', 'Defines the data type to parse the values' do |data_type|
47
+ @data_type = data_type
48
+ end
49
+ opt.on '-e FILES', '--eval=FILES', Array, 'Sets a file to eval for extending components' do |files|
50
+ files.each do |file|
51
+ load File.expand_path(file)
52
+ end
39
53
  end
40
54
  opt.on '-h', '--help', 'Display this help message' do
41
55
  puts opt
42
56
  exit 0
43
57
  end
58
+
59
+ if ARGV.empty?
60
+ puts opt
61
+ exit 0
62
+ end
63
+
44
64
  opt.parse! ARGV
45
65
 
46
- def print_out text
66
+ def print_out message
47
67
  if @color
48
- puts Yummi::colorize text, @color
68
+ puts Yummi::colorize message, @color
49
69
  elsif @colorizer
50
- puts @colorizer.colorize text
70
+ puts @colorizer.colorize message
51
71
  end
52
72
  end
53
73
 
54
- if @color or @colorizer
55
- if @text
56
- print_out @text
74
+ if @message
75
+ print_out @message
76
+ elsif @table_builder
77
+ abort "Please give the data to print" unless @data
78
+ table = @table_builder.build_table
79
+ 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)
57
86
  else
58
- begin
59
- ARGF.each_line do |line|
60
- print_out line.chomp
61
- end
62
- rescue Interrupt
63
- #Do nothing
64
- end
87
+ abort "Unsupported extension #{extension}"
65
88
  end
89
+ puts table
66
90
  else
67
- abort 'Please give a color or choose a pattern.'
91
+ begin
92
+ ARGF.each_line do |line|
93
+ print_out line.chomp
94
+ end
95
+ rescue Interrupt
96
+ #Do nothing
97
+ end
68
98
  end
@@ -22,23 +22,14 @@
22
22
 
23
23
  require 'optparse'
24
24
  require_relative '../lib/yummi'
25
+ require_relative 'cash_flow_table_extensions.rb'
25
26
 
26
27
  opt = OptionParser::new
27
28
 
28
- @table = Yummi::Table::new
29
- # setting the header sets the aliases automatically
30
- @table.header = ['Description', 'Value', 'Total', 'Eletronic', "Authentication\nCode"]
31
- # sets the title
32
- @table.title = 'Cash Flow'
33
- @table.description = 'How you spend your money'
34
- # formats booleans using Yes or No
35
- @table.format :eletronic, :using => Yummi::Formatters.yes_or_no
36
- # shows values without minus signal and rounded
37
- @table.format :value, :using => lambda { |value| "%.2f" % value.abs }
38
- # shows totals rounded
39
- @table.format :total, :using => Yummi::Formatters.round(2)
40
- @table.format_null :with => 'none'
41
- # table data
29
+ tablebuilder = Yummi::TableBuilder::new('cash_flow_table.yaml')
30
+
31
+ @table = tablebuilder.defaults.build_table
32
+
42
33
  @table.data = [['Initial', 0, 0, false, nil],
43
34
  ['Deposit', 100.58, 100.58, true, "QAWSEDRFTGH535"],
44
35
  ['Withdraw', -50.23, 50.35, true, "34ERDTF6GYU"],
@@ -46,50 +37,6 @@ opt = OptionParser::new
46
37
  ['Deposit', 50, 0.35, false, nil],
47
38
  ['Deposit', 600, 600.35, false, nil]]
48
39
 
49
- def full_colors
50
- @table.colorize :description, :with => :purple
51
- # Authentication Code will be highlighted
52
- @table.colorize :authentication_code, :with => :highlight_gray
53
- # colorize booleans based on their values
54
- @table.colorize :eletronic do |b|
55
- b ? :blue : :cyan
56
- end
57
- # colorize the values based on comparison
58
- red_to_negative = lambda { |value| :red if value < 0 }
59
- green_to_positive = lambda { |value| :green if value > 0 }
60
- brown_to_zero = lambda { |value| :yellow if value == 0 }
61
- @table.colorize [:value, :total], :using => Yummi::Colorizers.join(
62
- red_to_negative, green_to_positive, brown_to_zero
63
- )
64
- # colorize rows that Value is greater than Total
65
- @table.row_colorizer do |data| # or |data, index| if you need the index
66
- :white if data[:value] > data[:total]
67
- end
68
- @table.colorize_null :with => :red
69
- end
70
-
71
- def zebra_colors
72
- @table.row_colorizer Yummi::Colorizers.stripe :yellow, :purple
73
- end
74
-
75
- def no_colors
76
- @table.no_colors
77
- end
78
-
79
- opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
80
- case type
81
- when 'zebra'
82
- zebra_colors
83
- when 'full'
84
- # colorize all values from the Description column to purple
85
- full_colors
86
- when 'none'
87
- no_colors
88
- else
89
- end
90
- @colors = type
91
- end
92
-
93
40
  opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
94
41
  @table.layout = layout.to_sym
95
42
  end
@@ -104,8 +51,6 @@ end
104
51
 
105
52
  opt.parse ARGV
106
53
 
107
- full_colors unless @colors
108
-
109
54
  if @box
110
55
  @box << @table
111
56
  @box.print
@@ -0,0 +1,36 @@
1
+ title: Cash Flow
2
+ description: How you spend your money
3
+ layout: horizontal
4
+ header:
5
+ - Description
6
+ - Value
7
+ - Total
8
+ - Eletronic
9
+ - "Authentication\nCode"
10
+ format:
11
+ eletronic: boolean
12
+ value,total:
13
+ numeric:
14
+ positive: "%.2f"
15
+ zero: "%.2f"
16
+ negative: "%.2f"
17
+ undefined:
18
+ with: none
19
+ color:
20
+ description:
21
+ with: purple
22
+ authentication_code:
23
+ with: highlight_gray
24
+ eletronic:
25
+ boolean:
26
+ if_true: blue
27
+ if_false: cyan
28
+ value,total:
29
+ numeric:
30
+ positive: green
31
+ zero: gray
32
+ negative: red
33
+ undefined:
34
+ with: red
35
+ row_color:
36
+ blessed_income: white
@@ -0,0 +1,6 @@
1
+ Initial, 0, 0, false
2
+ Deposit, 100.58, 100.58, true, QAWSEDRFTGH535
3
+ Withdraw, -50.23, 50.35, true, 34ERDTF6GYU
4
+ Withdraw, -100, -49.65, true, 2344EDRFT5
5
+ Deposit, 50, 0.35, false
6
+ Deposit, 600, 600.35, false
@@ -0,0 +1,30 @@
1
+ - - Initial
2
+ - 0
3
+ - 0
4
+ - false
5
+ -
6
+ - - Deposit
7
+ - 100.58
8
+ - 100.58
9
+ - true
10
+ - QAWSEDRFTGH535
11
+ - - Withdraw
12
+ - -50.23
13
+ - 50.35
14
+ - true
15
+ - 34ERDTF6GYU
16
+ - - Withdraw
17
+ - -100
18
+ - -49.65
19
+ - true
20
+ - 2344EDRFT5
21
+ - - Deposit
22
+ - 50
23
+ - 0.35
24
+ - false
25
+ -
26
+ - - Deposit
27
+ - 600
28
+ - 600.35
29
+ - false
30
+ -
@@ -0,0 +1,9 @@
1
+ module Yummi
2
+ module Colorizers
3
+ def self.blessed_income color
4
+ Yummi::to_colorize do |data| # or |data, index| if you need the index
5
+ color if data[:value] > data[:total]
6
+ end
7
+ end
8
+ end
9
+ end
@@ -27,7 +27,7 @@ require_relative '../lib/yummi'
27
27
 
28
28
  @box.width = 70
29
29
  @box.default_align = :justify
30
- @box.default_separator[:color] = :brown
30
+ @box.default_separator[:color] = :yellow
31
31
 
32
32
  opt = OptionParser::new
33
33
 
@@ -35,26 +35,20 @@ opt = OptionParser::new
35
35
  @table.format :free_memory, :using => Yummi::Formatters.byte
36
36
 
37
37
  # colorizer for memory
38
- memory_colorizer = Yummi::Colorizers.by_data_eval do |free_memory, max_memory|
39
- free_memory.to_f / max_memory
40
- end
41
- memory_colorizer.use(:red) { |value| value > 0.1 and value < 0.3 }
42
- memory_colorizer.use(:intense_red) { |value| value <= 0.1 }
38
+ memory_colorizer = Yummi::Colorizers.percentage :max => :max_memory, :free => :free_memory
43
39
 
44
40
  # colorizer for threads
45
- thread_colorizer = Yummi::Colorizers.by_data_eval do |max_threads, in_use_threads|
46
- in_use_threads.to_f / max_threads
47
- end
48
- thread_colorizer.use(:brown) { |value| value > 0.7 and value < 0.9 }
49
- thread_colorizer.use(:intense_cyan) { |value| value >= 0.9 }
41
+ thread_colorizer = Yummi::Colorizers.percentage :max => :max_threads,
42
+ :using => :in_use_threads,
43
+ :threshold => {
44
+ :warn => 0.9,
45
+ :bad => 0.7
46
+ }
50
47
 
51
- opt.on '--color TYPE', 'Specify the color type (zebra,row,cell,none)' do |type|
48
+ opt.on '--color TYPE', 'Specify the color type (zebra,cell,none)' do |type|
52
49
  case type
53
50
  when 'zebra'
54
- @table.row_colorizer Yummi::Colorizers.stripe :brown, :purple
55
- when 'row'
56
- @table.row_colorizer memory_colorizer
57
- @table.row_colorizer thread_colorizer
51
+ @table.row_colorizer Yummi::Colorizers.stripe :yellow, :purple
58
52
  when 'cell'
59
53
  @table.using_row do
60
54
  @table.colorize :free_memory, :using => memory_colorizer
data/lib/yummi.rb CHANGED
@@ -202,85 +202,11 @@ module Yummi
202
202
 
203
203
  end
204
204
 
205
- # A module used to create an alias method in a formatter block
206
- module FormatterBlock
207
-
208
- # Calls the :call: method
209
- def format (value)
210
- call value
211
- end
212
-
213
- end
214
-
215
- # Extends the given block with #FormatterBlock
216
- def self.to_format &block
217
- block.extend FormatterBlock
218
- end
219
-
220
- # A module with useful formatters
221
- module Formatters
222
-
223
- # A formatter for boolean values that uses 'Yes' or 'No'
224
- def self.yes_or_no
225
- Yummi::to_format do |value|
226
- value ? "Yes" : "No"
227
- end
228
- end
229
-
230
- # A formatter to float values that uses the precision to round the value
231
- def self.round precision
232
- Yummi::to_format do |value|
233
- "%.#{precision}f" % value
234
- end
235
- end
236
-
237
- # Defines the modes to format a byte value
238
- BYTE_MODES = {
239
- :iec => {
240
- :range => %w{B KiB MiB GiB TiB PiB EiB ZiB YiB},
241
- :step => 1024
242
- },
243
- :si => {
244
- :range => %w{B KB MB GB TB PB EB ZB YB},
245
- :step => 1000
246
- }
247
- }
248
-
249
- #
250
- # Formats a byte value to ensure easily reading
251
- #
252
- # === Hash Args
253
- #
254
- # +precision+::
255
- # How many decimal digits should be displayed. (Defaults to 1)
256
- # +mode+::
257
- # Which mode should be used to display unit symbols. (Defaults to :iec)
258
- #
259
- # See #BYTE_MODES
260
- #
261
- def self.byte params = {}
262
- Yummi::to_format do |value|
263
- value = value.to_i if value.is_a? String
264
- mode = (params[:mode] or :iec)
265
- range = BYTE_MODES[mode][:range]
266
- step = BYTE_MODES[mode][:step]
267
- params[:precision] ||= 1
268
- result = value
269
- range.each_index do |i|
270
- minimun = (step ** i)
271
- result = "%.#{params[:precision]}f #{range[i]}" % (value.to_f / minimun) if value >= minimun
272
- end
273
- result
274
- end
275
- end
276
-
277
- end
278
-
279
205
  # A class to expose indexed data by numeric indexes and aliases.
280
206
  class IndexedData
281
207
 
282
208
  def initialize (aliases, data)
283
- @aliases = aliases
209
+ @aliases = aliases.collect {|a| a.to_s}
284
210
  @data = data
285
211
  end
286
212
 
@@ -288,7 +214,9 @@ module Yummi
288
214
  if value.is_a? Fixnum
289
215
  @data[value]
290
216
  else
291
- @data[@aliases.index(value)]
217
+ index = @aliases.index(value.to_s)
218
+ raise Exception::new("Unknow alias #{value}: \nAliases: #{@aliases}") unless index
219
+ @data[index]
292
220
  end
293
221
  end
294
222
 
@@ -333,13 +261,34 @@ module Yummi
333
261
 
334
262
  end
335
263
 
264
+ module Helpers
265
+
266
+ def self.symbolize_keys hash
267
+ hash.replace(hash.inject({}) do |h, (k, v)|
268
+ v = symbolize_keys(v) if v.is_a? Hash
269
+ if v.is_a? Array
270
+ v.each do |e|
271
+ if e.is_a? Hash
272
+ symbolize_keys(e)
273
+ end
274
+ end
275
+ end
276
+ k = k.to_sym if k.respond_to? :to_sym
277
+ h[k] = v
278
+ h
279
+ end)
280
+ end
281
+ end
282
+
336
283
  end
337
284
 
338
285
  require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
339
286
 
340
287
  require_relative "yummi/colorizers"
288
+ require_relative "yummi/formatters"
341
289
  require_relative 'yummi/color_mapping'
342
290
  require_relative 'yummi/table'
291
+ require_relative 'yummi/table_builder'
343
292
  require_relative 'yummi/text_box'
344
293
  require_relative 'yummi/logger'
345
294
 
@@ -53,7 +53,7 @@ module Yummi
53
53
  # convention, the first argument must be the object to colorize (to_s is called on it
54
54
  # for getting the text to colorize).#
55
55
  def color_for (*args)
56
- call *args
56
+ call(*args)
57
57
  end
58
58
 
59
59
  end
@@ -100,6 +100,110 @@ module Yummi
100
100
  LineColorizer::new mappings
101
101
  end
102
102
 
103
+ #
104
+ # A colorizer that uses the given color.
105
+ #
106
+ def self.with color
107
+ Yummi::to_colorize do |value|
108
+ color
109
+ end
110
+ end
111
+
112
+ def self.when params
113
+ Yummi::to_colorize do |value|
114
+ color = params[value]
115
+ return color if color
116
+ if value.respond_to? :to_sym
117
+ color = params[value.to_sym]
118
+ end
119
+ unless color
120
+ params.each do |k, v|
121
+ return v if k.to_s == value.to_s
122
+ end
123
+ end
124
+ color
125
+ end
126
+ end
127
+
128
+ #
129
+ # A colorizer for boolean values.
130
+ #
131
+ # Parameters:
132
+ # - if_true: color used if the value is true (defaults to green)
133
+ # - if_false: color used if the value is false (defaults to yellow)
134
+ #
135
+ def self.boolean params = {}
136
+ Yummi::to_colorize do |value|
137
+ if value.to_s.downcase == "true"
138
+ (params[:if_true] or :green)
139
+ else
140
+ (params[:if_false] or :yellow)
141
+ end
142
+ end
143
+ end
144
+
145
+ #
146
+ # A colorizer that uses a set of minimun values to use a color.
147
+ #
148
+ # Parameters:
149
+ # - MINIMUN_VALUE: COLOR_TO_USE
150
+ #
151
+ def self.threshold params
152
+ colorizer = lambda do |value|
153
+ params.sort.reverse_each do |limit, color|
154
+ return color if value > limit
155
+ end
156
+ end
157
+ Yummi::to_colorize(&colorizer)
158
+ end
159
+
160
+ def self.percentage params
161
+ PercentageColorizer::new params
162
+ end
163
+
164
+ def self.numeric params
165
+ Yummi::to_format do |value|
166
+ if params[:negative] and value < 0
167
+ params[:negative]
168
+ elsif params[:positive] and value > 0
169
+ params[:positive]
170
+ elsif params[:zero] and value == 0
171
+ params[:zero]
172
+ end
173
+ end
174
+ end
175
+
176
+ class PercentageColorizer
177
+ include Yummi::Colorizer
178
+
179
+ def initialize(params)
180
+ @max = params[:max]
181
+ @free = params[:free]
182
+ @using = params[:using]
183
+ @color = params[:color] || {
184
+ :bad => :red,
185
+ :warn => :yellow,
186
+ :good => :green
187
+ }
188
+ @threshold = params[:threshold] || {
189
+ :warn => 0.30,
190
+ :bad => 0.15
191
+ }
192
+ end
193
+
194
+ def call(data)
195
+ max = data[@max.to_sym].to_f
196
+ free = @using ? max - data[@using.to_sym].to_f : data[@free.to_sym].to_f
197
+
198
+ percentage = free / max
199
+
200
+ return @color[:bad] if percentage <= @threshold[:bad]
201
+ return @color[:warn] if percentage <= @threshold[:warn]
202
+ @color[:good]
203
+ end
204
+
205
+ end
206
+
103
207
  #
104
208
  # A colorizer for lines that follows a pattern. This colorizer is usefull
105
209
  # for log files.
@@ -169,7 +273,7 @@ module Yummi
169
273
 
170
274
  # Creates a new colorizer using the given colors
171
275
  def initialize (*colors)
172
- @colors = colors
276
+ @colors = colors.flatten
173
277
  @count = -1
174
278
  end
175
279
 
@@ -218,11 +322,11 @@ module Yummi
218
322
 
219
323
  # Resolves the value using the main block and given arguments
220
324
  def resolve_value (*args)
221
- @block.call *args
325
+ @block.call(*args)
222
326
  end
223
327
 
224
328
  def call (*args)
225
- value = resolve_value *args
329
+ value = resolve_value(*args)
226
330
  @eval_blocks.each_index do |i|
227
331
  return @colors[i] if @eval_blocks[i].call(value)
228
332
  end
@@ -0,0 +1,149 @@
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
+ # A module used to create an alias method in a formatter block
26
+ module FormatterBlock
27
+
28
+ # Calls the :call: method
29
+ def format (value)
30
+ call value
31
+ end
32
+
33
+ end
34
+
35
+ # Extends the given block with #FormatterBlock
36
+ def self.to_format &block
37
+ block.extend FormatterBlock
38
+ end
39
+
40
+ # A module with useful formatters
41
+ module Formatters
42
+
43
+ #
44
+ # A formatter for boolean values that uses 'Yes' or 'No' by default
45
+ #
46
+ # === Hash Args
47
+ #
48
+ # :if_true => String to use when value is true
49
+ # :if_false => String to use when value is false
50
+ #
51
+ def self.boolean params = {}
52
+ Yummi::to_format do |value|
53
+ if value.to_s.downcase == "true"
54
+ (params[:if_true] or "Yes")
55
+ else
56
+ (params[:if_false] or "No")
57
+ end
58
+ end
59
+ end
60
+
61
+ # A formatter to round float values
62
+ def self.round precision
63
+ Yummi::to_format do |value|
64
+ "%.#{precision}f" % value
65
+ end
66
+ end
67
+
68
+ # A formatter that uses the given format
69
+ def self.with format
70
+ Yummi::to_format do |value|
71
+ format % value
72
+ end
73
+ end
74
+
75
+ #
76
+ # A formatter for numeric values
77
+ #
78
+ # === Hash Args
79
+ #
80
+ # :negative => format to use when value is negative
81
+ # :zero => format to use when value is zero
82
+ # :positive => format to use when value is positive
83
+ #
84
+ def self.numeric params
85
+ Yummi::to_format do |value|
86
+ if params[:negative] and value < 0
87
+ params[:negative] % value.abs
88
+ elsif params[:positive] and value > 0
89
+ params[:positive] % value
90
+ elsif params[:zero] and value == 0
91
+ params[:zero] % value
92
+ end
93
+ end
94
+ end
95
+
96
+ #
97
+ # A formatter for percentual values.
98
+ #
99
+ # Paramters:
100
+ # The precision to use (defaults to 3)
101
+ #
102
+ def self.percentage precision = 3
103
+ Yummi::to_format do |value|
104
+ "%.#{precision}f%%" % (value * 100)
105
+ end
106
+ end
107
+
108
+ # Defines the modes to format a byte value
109
+ BYTE_MODES = {
110
+ :iec => {
111
+ :range => %w{B KiB MiB GiB TiB PiB EiB ZiB YiB},
112
+ :step => 1024
113
+ },
114
+ :si => {
115
+ :range => %w{B KB MB GB TB PB EB ZB YB},
116
+ :step => 1000
117
+ }
118
+ }
119
+
120
+ #
121
+ # Formats a byte value to ensure easily reading
122
+ #
123
+ # === Hash Args
124
+ #
125
+ # +precision+::
126
+ # How many decimal digits should be displayed. (Defaults to 1)
127
+ # +mode+::
128
+ # Which mode should be used to display unit symbols. (Defaults to :iec)
129
+ #
130
+ # See #BYTE_MODES
131
+ #
132
+ def self.byte params = {}
133
+ Yummi::to_format do |value|
134
+ value = value.to_i if value.is_a? String
135
+ mode = (params[:mode] or :iec)
136
+ range = BYTE_MODES[mode][:range]
137
+ step = BYTE_MODES[mode][:step]
138
+ params[:precision] ||= 1
139
+ result = value
140
+ range.each_index do |i|
141
+ minimun = (step ** i)
142
+ result = "%.#{params[:precision]}f #{range[i]}" % (value.to_f / minimun) if value >= minimun
143
+ end
144
+ result
145
+ end
146
+ end
147
+
148
+ end
149
+ end
data/lib/yummi/table.rb CHANGED
@@ -46,6 +46,8 @@ module Yummi
46
46
  attr_accessor :colors
47
47
  # The table layout (horizontal or vertical)
48
48
  attr_reader :layout
49
+ # The table header
50
+ attr_reader :header
49
51
 
50
52
  # Creates a new table with the default attributes:
51
53
  #
@@ -144,6 +146,7 @@ module Yummi
144
146
  def align (indexes, type)
145
147
  [*indexes].each do |index|
146
148
  index = parse_index(index)
149
+ raise Exception::new "Undefined column #{index}" unless index
147
150
  @align[index] = type
148
151
  end
149
152
  end
@@ -153,15 +156,12 @@ module Yummi
153
156
  # The component must respond to +call+ with the index and the row as the arguments and
154
157
  # return a color or +nil+ if default color should be used. A block can also be used.
155
158
  #
156
- # You can add as much colorizers as you want. The first color returned will be used.
157
- #
158
159
  # === Example
159
160
  #
160
161
  # table.row_colorizer { |i, row| :red if row[:value] < 0 }
161
162
  #
162
163
  def row_colorizer (colorizer = nil, &block)
163
- @row_colorizer ||= Yummi::GroupedComponent::new
164
- @row_colorizer << (colorizer or block)
164
+ @row_colorizer = (colorizer or block)
165
165
  end
166
166
 
167
167
  #
@@ -205,7 +205,6 @@ module Yummi
205
205
  # #using_row) as the arguments and return a color or +nil+ if default color should be
206
206
  # used. A block can also be used.
207
207
  #
208
- # You can add as much colorizers as you want. The first color returned will be used.
209
208
  #
210
209
  # === Args
211
210
  #
@@ -224,9 +223,12 @@ module Yummi
224
223
  def colorize (indexes, params = {}, &block)
225
224
  [*indexes].each do |index|
226
225
  index = parse_index(index)
227
- @colorizers[index] ||= []
228
- obj = (params[:using] or block or (proc { |v| params[:with] }))
229
- @colorizers[index] << {:use_row => @using_row, :component => obj}
226
+ if index
227
+ obj = (params[:using] or block or (proc { |v| params[:with] }))
228
+ @colorizers[index] = {:use_row => @using_row, :component => obj}
229
+ else
230
+ colorize_null params, &block
231
+ end
230
232
  end
231
233
  end
232
234
 
@@ -271,9 +273,13 @@ module Yummi
271
273
  def format (indexes, params = {}, &block)
272
274
  [*indexes].each do |index|
273
275
  index = parse_index(index)
274
- @formatters[index] = (params[:using] or block)
275
- @formatters[index] ||= proc do |value|
276
- params[:with] % value
276
+ if index
277
+ @formatters[index] = (params[:using] or block)
278
+ @formatters[index] ||= proc do |value|
279
+ params[:with] % value
280
+ end
281
+ else
282
+ format_null params, &block
277
283
  end
278
284
  end
279
285
  end
@@ -377,18 +383,12 @@ module Yummi
377
383
  color = nil
378
384
  value = nil
379
385
  column = row[col_index]
380
- colorizers = @colorizers[col_index]
386
+ colorizer = @colorizers[col_index]
381
387
  if @null_colorizer and column.nil?
382
388
  color = @null_colorizer.call(column)
383
- elsif colorizers
384
- colorizers.each do |colorizer|
385
- arg = colorizer[:use_row] ? IndexedData::new(@aliases, row) : column
386
- c = colorizer[:component].call(arg)
387
- if c
388
- color = c
389
- break
390
- end
391
- end
389
+ elsif colorizer
390
+ arg = colorizer[:use_row] ? IndexedData::new(@aliases, row) : column
391
+ color = colorizer[:component].call(arg)
392
392
  else
393
393
  color = @colors[:value]
394
394
  end
@@ -410,8 +410,7 @@ module Yummi
410
410
  end,
411
411
  :new => proc do |value, data|
412
412
  {:value => value, :color => data[:color]}
413
- end
414
- )
413
+ end)
415
414
  _row_data.each do |_row|
416
415
  output << _row
417
416
  end
@@ -444,8 +443,8 @@ module Yummi
444
443
  end
445
444
 
446
445
  def parse_index(value)
447
- return @aliases.index(value) unless value.is_a? Fixnum
448
- value
446
+ return value if value.is_a? Fixnum
447
+ (@aliases.index(value) or @aliases.index(value.to_sym))
449
448
  end
450
449
 
451
450
  def max_width(data, column)
@@ -0,0 +1,150 @@
1
+ # The MIT License
2
+ #
3
+ # Copyright (c) 2011-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
+ class TableBuilder
25
+
26
+ attr_accessor :config, :repositories
27
+
28
+ def initialize config = {}
29
+ if config.is_a? String
30
+ config = Yummi::Helpers::symbolize_keys(YAML::load_file(config))
31
+ end
32
+ @config = config
33
+ @repositories = {}
34
+
35
+ @repositories[:formatters] = [Yummi::Formatters]
36
+ @repositories[:colorizers] = [Yummi::Colorizers]
37
+ @repositories[:row_based_colorizers] = [Yummi::Colorizers]
38
+ @repositories[:using_row_colorizers] = [Yummi::Colorizers]
39
+ end
40
+
41
+ def defaults
42
+ component [:color, :colorize], :repository => :colorizers,
43
+ :invoke => :colorize
44
+
45
+ component :format, :repository => :formatters,
46
+ :invoke => :format
47
+
48
+ component [:row_color, :colorize_row], :repository => :row_based_colorizers,
49
+ :invoke => :row_colorizer,
50
+ :row_based => true
51
+
52
+ component [:state, :health], :repository => :using_row_colorizers,
53
+ :invoke => :colorize,
54
+ :using_row => true
55
+ self
56
+ end
57
+
58
+ def components
59
+ @components ||= {}
60
+ @components
61
+ end
62
+
63
+ def component keys, params
64
+ [*keys].each do |key|
65
+ components[key] = params
66
+ end
67
+ end
68
+
69
+ def build_table
70
+ table = Yummi::Table::new
71
+ table.title = config[:title]
72
+ table.description= config[:description]
73
+ table.aliases = config[:aliases] if config[:aliases]
74
+ table.header = config[:header] if config[:header]
75
+ table.layout = config[:layout].to_sym if config[:layout]
76
+
77
+ components.each do |key, component_config|
78
+ block = lambda do |params|
79
+ if component_config[:using_row]
80
+ table.using_row do
81
+ table.send component_config[:invoke], *params
82
+ end
83
+ else
84
+ table.send component_config[:invoke], *params
85
+ end
86
+ end
87
+ if component_config[:row_based]
88
+ parse_row_based_component config[key], component_config, &block
89
+ else
90
+ parse_component config[key], component_config, &block
91
+ end
92
+ end
93
+
94
+ table
95
+ end
96
+
97
+ def parse_component(definitions, config)
98
+ if definitions
99
+ definitions.each do |column, component_config|
100
+ callback = []
101
+ column = column.to_s.split(/,/)
102
+ callback << column
103
+ component = create_component(component_config, config)
104
+ callback << {:using => component}
105
+ yield(callback)
106
+ end
107
+ end
108
+ end
109
+
110
+ def parse_row_based_component(definitions, config)
111
+ if definitions
112
+ if definitions.is_a? Hash
113
+ definitions.each do |component_name, params|
114
+ yield(create_component({component_name => params}, config))
115
+ end
116
+ else
117
+ puts definitions
118
+ component = create_component(definitions, config)
119
+ puts component
120
+ yield(component)
121
+ end
122
+ end
123
+ end
124
+
125
+ def create_component(component_config, config)
126
+ repositories = @repositories[config[:repository]].reverse
127
+ if component_config.is_a? Hash
128
+ component_config = Yummi::Helpers::symbolize_keys(component_config)
129
+ component = Yummi::GroupedComponent::new
130
+ component_config.each do |component_name, params|
131
+ repositories.each do |repository|
132
+ if repository.respond_to? component_name
133
+ component << repository.send(component_name, params)
134
+ break
135
+ end
136
+ end
137
+ end
138
+ return component
139
+ else
140
+ repositories.each do |repository|
141
+ if repository.respond_to? component_config
142
+ return repository.send(component_config)
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ end
149
+
150
+ end
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.4.3"
24
+ VERSION = "0.5.0"
25
25
  end
data/yummi.ipr CHANGED
@@ -21,6 +21,7 @@
21
21
  <entry name="?*.dtd" />
22
22
  <entry name="?*.tld" />
23
23
  <entry name="?*.ftl" />
24
+ <entry name="trace.info" />
24
25
  </wildcardResourcePatterns>
25
26
  <annotationProcessing enabled="false" useClasspath="true" />
26
27
  </component>
@@ -41,6 +42,9 @@
41
42
  <option name="SKIP_IMPORT_STATEMENTS" value="false" />
42
43
  </component>
43
44
  <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
45
+ <component name="GradleSettings">
46
+ <option name="gradleHome" value="$USER_HOME$/build/gradle" />
47
+ </component>
44
48
  <component name="GradleUISettings2">
45
49
  <setting name="root" />
46
50
  </component>
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.4.3
4
+ version: 0.5.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-09-29 00:00:00.000000000 Z
12
+ date: 2012-10-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email:
@@ -27,6 +27,10 @@ files:
27
27
  - Rakefile
28
28
  - bin/colorize
29
29
  - examples/cash_flow_table.rb
30
+ - examples/cash_flow_table.yaml
31
+ - examples/cash_flow_table_data.csv
32
+ - examples/cash_flow_table_data.yaml
33
+ - examples/cash_flow_table_extensions.rb
30
34
  - examples/license_box.rb
31
35
  - examples/list_files.rb
32
36
  - examples/logcat_colorizer.rb
@@ -35,12 +39,14 @@ files:
35
39
  - lib/yummi.rb
36
40
  - lib/yummi/color_mapping.rb
37
41
  - lib/yummi/colorizers.rb
42
+ - lib/yummi/formatters.rb
38
43
  - lib/yummi/logger.rb
39
44
  - lib/yummi/mappings/jboss5.yaml
40
45
  - lib/yummi/mappings/jboss7.yaml
41
46
  - lib/yummi/mappings/weblogic11g.yaml
42
47
  - lib/yummi/no_colors.rb
43
48
  - lib/yummi/table.rb
49
+ - lib/yummi/table_builder.rb
44
50
  - lib/yummi/text_box.rb
45
51
  - lib/yummi/version.rb
46
52
  - yummi.gemspec