yummi 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,8 @@ tablebuilder = Yummi::TableBuilder::new('cash_flow_table.yaml')
35
35
  ['Withdraw', -50.23, 50.35, true, "34ERDTF6GYU"],
36
36
  ['Withdraw', -100, -49.65, true, "2344EDRFT5"],
37
37
  ['Deposit', 50, 0.35, false, nil],
38
- ['Deposit', 600, 600.35, false, nil]]
38
+ ['Deposit', 600, 600.35, false, nil],
39
+ ['Total', nil, 600.35, nil, nil]]
39
40
 
40
41
  opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
41
42
  @table.layout = layout.to_sym
@@ -33,4 +33,14 @@ color:
33
33
  undefined:
34
34
  with: red
35
35
  row_color:
36
- blessed_income: white
36
+ blessed_income: yellow
37
+
38
+ contexts:
39
+ - format:
40
+ total:
41
+ numeric:
42
+ positive: "%.2f"
43
+ zero: "%.2f"
44
+ negative: "%.2f"
45
+ row_color:
46
+ with: white
@@ -3,4 +3,5 @@ Deposit, 100.58, 100.58, true, QAWSEDRFTGH535
3
3
  Withdraw, -50.23, 50.35, true, 34ERDTF6GYU
4
4
  Withdraw, -100, -49.65, true, 2344EDRFT5
5
5
  Deposit, 50, 0.35, false
6
- Deposit, 600, 600.35, false
6
+ Deposit, 600, 600.35, false
7
+ Total, , 600.35
@@ -28,3 +28,8 @@
28
28
  - 600.35
29
29
  - false
30
30
  -
31
+ - - Total
32
+ -
33
+ - 600.35
34
+ -
35
+ -
@@ -39,11 +39,19 @@ opt = OptionParser::new
39
39
  opt.on '--color TYPE', 'Specify the color type (zebra,file,none)' do |type|
40
40
  case type
41
41
  when 'zebra'
42
- @table.row_colorizer Yummi::Colorizers.stripe :intense_gray, :intense_white
42
+ @table.colorize_row :using => Yummi::Colorizers.stripe(:intense_gray, :intense_white)
43
+ @table.context do
44
+ @table.colorize_row :with => :intense_blue
45
+ @table.format :size, :using => Yummi::Formatters.byte
46
+ end
43
47
  when 'file'
44
- @table.row_colorizer do |data| # or |data, index| if you need the index
48
+ @table.colorize_row do |data| # or |data, index| if you need the index
45
49
  data[:directory] ? :intense_gray : :intense_white
46
50
  end
51
+ @table.context do
52
+ @table.colorize_row :with => :intense_blue
53
+ @table.format :size, :using => Yummi::Formatters.byte
54
+ end
47
55
  when 'none'
48
56
  @table.no_colors
49
57
  else
@@ -64,4 +72,6 @@ files.each do |f|
64
72
  data << [f, File.size(f), File.directory?(f)] # the last value will not be printed
65
73
  end
66
74
  @table.data = data
75
+ @table << ["Total", @table.column(:size).inject(:+)]
76
+
67
77
  @table.print
@@ -31,8 +31,12 @@ opt = OptionParser::new
31
31
  # sets the title
32
32
  @table.title = 'Server Runtime Info'
33
33
  # formats memory info for easily reading
34
- @table.format :max_memory, :using => Yummi::Formatters.byte
35
- @table.format :free_memory, :using => Yummi::Formatters.byte
34
+ @table.format [:max_memory, :free_memory], :using => Yummi::Formatters.byte
35
+
36
+ @table.context do
37
+ @table.format [:max_memory, :free_memory], :using => Yummi::Formatters.byte
38
+ @table.colorize_row :with => :white
39
+ end
36
40
 
37
41
  # colorizer for memory
38
42
  memory_colorizer = Yummi::Colorizers.percentage :max => :max_memory, :free => :free_memory
@@ -48,8 +52,9 @@ thread_colorizer = Yummi::Colorizers.percentage :max => :max_threads,
48
52
  opt.on '--color TYPE', 'Specify the color type (zebra,cell,none)' do |type|
49
53
  case type
50
54
  when 'zebra'
51
- @table.row_colorizer Yummi::Colorizers.stripe :yellow, :purple
55
+ @table.colorize_row :using => Yummi::Colorizers.stripe(:yellow, :purple)
52
56
  when 'cell'
57
+ @table.colorize :server_name, :with => :purple
53
58
  @table.using_row do
54
59
  @table.colorize :free_memory, :using => memory_colorizer
55
60
  @table.colorize :in_use_threads, :using => thread_colorizer
@@ -80,26 +85,32 @@ opt.parse ARGV
80
85
 
81
86
  # sets the table data
82
87
  @table.data = [
83
- ['Server 1', 1_000_000, 750_000, 200, 170],
84
- ['Server 2', 1_000_000, 700_000, 200, 180],
85
- ['Server 3', 1_000_000, 50_000, 200, 50],
86
- ['Server 4', 1_000_000, 200_000, 200, 50],
87
- ['Server 5', 1_000_000, 5_000, 200, 50],
88
- ['Server 6', 1_000_000, 750_000, 200, 50],
88
+ ['Server 1', 1_000_000_000, 750_000_000, 200, 170],
89
+ ['Server 2', 1_000_000_000, 700_000_000, 200, 180],
90
+ ['Server 3', 1_000_000_000, 50_000_000, 200, 50],
91
+ ['Server 4', 1_000_000_000, 200_000_000, 200, 50],
92
+ ['Server 5', 1_000_000_000, 5_000_000, 200, 50],
93
+ ['Server 6', 1_000_000_000, 750_000_000, 200, 50],
89
94
  ]
90
95
 
91
96
  @table.add :server_name => 'Server 7',
92
- :max_memory => 1_000_000,
93
- :free_memory => 200_000,
97
+ :max_memory => 1_000_000_000,
98
+ :free_memory => 200_000_000,
94
99
  :max_threads => 200,
95
100
  :in_use_threads => 170
96
101
 
97
102
  @table.add :server_name => 'Server 8',
98
- :max_memory => 1_000_000,
99
- :free_memory => 5_000,
103
+ :max_memory => 1_000_000_000,
104
+ :free_memory => 5_000_000,
100
105
  :max_threads => 200,
101
106
  :in_use_threads => 180
102
107
 
108
+ @table.add :server_name => 'Total',
109
+ :max_memory => @table.column(:max_memory).inject(:+),
110
+ :free_memory => @table.column(:free_memory).inject(:+),
111
+ :max_threads => @table.column(:max_threads).inject(:+),
112
+ :in_use_threads => @table.column(:in_use_threads).inject(:+)
113
+
103
114
  if @box
104
115
  @box << @table
105
116
  @box.print
data/lib/yummi.rb CHANGED
@@ -165,16 +165,19 @@ module Yummi
165
165
 
166
166
  # Aligns the text to the right
167
167
  def self.right text, width
168
+ text = text.to_s unless text.is_a? String
168
169
  text.rjust(width)
169
170
  end
170
171
 
171
172
  # Aligns the text to the left
172
173
  def self.left text, width
174
+ text = text.to_s unless text.is_a? String
173
175
  text.ljust(width)
174
176
  end
175
177
 
176
178
  # Aligns the text to the center
177
179
  def self.center text, width
180
+ text = text.to_s unless text.is_a? String
178
181
  return text if text.size >= width
179
182
  size = width - text.size
180
183
  left_size = size / 2
@@ -184,6 +187,7 @@ module Yummi
184
187
 
185
188
  # Aligns the text to both sides
186
189
  def self.justify text, width
190
+ text = text.to_s unless text.is_a? String
187
191
  extra_spaces = width - text.size
188
192
  return text unless extra_spaces > 0
189
193
  words = text.split ' '
data/lib/yummi/table.rb CHANGED
@@ -48,7 +48,7 @@ module Yummi
48
48
  attr_reader :layout
49
49
  # The table header
50
50
  attr_reader :header
51
-
51
+
52
52
  # Creates a new table with the default attributes:
53
53
  #
54
54
  # * Title color: intense_yellow
@@ -74,12 +74,11 @@ module Yummi
74
74
  @aliases = []
75
75
 
76
76
  @align = [:left]
77
- @formatters = []
78
- @colorizers = []
79
- @row_colorizer = nil
80
-
81
- @predicated_formatters = nil
82
- @predicated_colorizers = nil
77
+ @components = {}
78
+ #@contexts = []
79
+ @context_rows = []
80
+ _define_ :default
81
+ @current_context = :default
83
82
  end
84
83
 
85
84
  # Indicates that the table should not use colors.
@@ -92,6 +91,20 @@ module Yummi
92
91
  @no_colors = true
93
92
  end
94
93
 
94
+ def context params = {}
95
+ params ||= {}
96
+ index = @context_rows.size #@contexts.size
97
+ _define_ index
98
+ #@contexts.insert(index, context)
99
+ @context_rows.insert(index, (params[:rows] or 1))
100
+
101
+ @current_context = index
102
+ yield if block_given?
103
+ @current_context = :default
104
+ end
105
+
106
+
107
+ # Sets the table print layout.
95
108
  def layout=(layout)
96
109
  @layout = layout
97
110
  case layout
@@ -104,6 +117,21 @@ module Yummi
104
117
  end
105
118
  end
106
119
 
120
+ # Retrieves the row at the given index
121
+ def row(index)
122
+ @data[index]
123
+ end
124
+
125
+ # Retrieves the column at the given index. Aliases can be used
126
+ def column(index)
127
+ index = parse_index(index)
128
+ columns = []
129
+ @data.each do |row|
130
+ columns << row[index]
131
+ end
132
+ columns
133
+ end
134
+
107
135
  #
108
136
  # Sets the table header. If no aliases are defined, they will be defined as the texts
109
137
  # in lowercase with line breaks and spaces replaced by underscores.
@@ -158,10 +186,11 @@ module Yummi
158
186
  #
159
187
  # === Example
160
188
  #
161
- # table.row_colorizer { |i, row| :red if row[:value] < 0 }
189
+ # table.colorize_row { |i, row| :red if row[:value] < 0 }
162
190
  #
163
- def row_colorizer (colorizer = nil, &block)
164
- @row_colorizer = (colorizer or block)
191
+ def colorize_row (params = nil, &block)
192
+ obj = extract_component(params, &block)
193
+ component[:row_colorizer] = obj
165
194
  end
166
195
 
167
196
  #
@@ -177,7 +206,7 @@ module Yummi
177
206
  #
178
207
  def using_row
179
208
  @using_row = true
180
- yield
209
+ yield if block_given?
181
210
  @using_row = false
182
211
  end
183
212
 
@@ -224,8 +253,8 @@ module Yummi
224
253
  [*indexes].each do |index|
225
254
  index = parse_index(index)
226
255
  if index
227
- obj = (params[:using] or block or (proc { |v| params[:with] }))
228
- @colorizers[index] = {:use_row => @using_row, :component => obj}
256
+ obj = extract_component(params, &block)
257
+ component[:colorizers][index] = {:use_row => @using_row, :component => obj}
229
258
  else
230
259
  colorize_null params, &block
231
260
  end
@@ -243,8 +272,8 @@ module Yummi
243
272
  # - :with defines the format to use
244
273
  #
245
274
  def colorize_null (params = {}, &block)
246
- @null_colorizer = (params[:using] or block)
247
- @null_colorizer ||= proc do |value|
275
+ component[:null_colorizer] = (params[:using] or block)
276
+ component[:null_colorizer] ||= proc do |value|
248
277
  params[:with]
249
278
  end
250
279
  end
@@ -274,8 +303,8 @@ module Yummi
274
303
  [*indexes].each do |index|
275
304
  index = parse_index(index)
276
305
  if index
277
- @formatters[index] = (params[:using] or block)
278
- @formatters[index] ||= proc do |value|
306
+ component[:formatters][index] = (params[:using] or block)
307
+ component[:formatters][index] ||= proc do |value|
279
308
  params[:with] % value
280
309
  end
281
310
  else
@@ -295,8 +324,8 @@ module Yummi
295
324
  # - :with defines the format to use
296
325
  #
297
326
  def format_null (params = {}, &block)
298
- @null_formatter = (params[:using] or block)
299
- @null_formatter ||= proc do |value|
327
+ component[:null_formatter] = (params[:using] or block)
328
+ component[:null_formatter] ||= proc do |value|
300
329
  params[:with] % value
301
330
  end
302
331
  end
@@ -326,6 +355,26 @@ module Yummi
326
355
  string << content(table_data)
327
356
  end
328
357
 
358
+ private
359
+
360
+ def extract_component params, &block
361
+ if params[:using]
362
+ params[:using]
363
+ elsif params[:with]
364
+ proc { |v| params[:with] }
365
+ else
366
+ block
367
+ end
368
+ end
369
+
370
+ def _define_ context
371
+ @components[context] = {
372
+ :formatters => [],
373
+ :colorizers => [],
374
+ :row_colorizer => nil,
375
+ }
376
+ end
377
+
329
378
  #
330
379
  # Gets the content string for the given color map and content
331
380
  #
@@ -373,8 +422,18 @@ module Yummi
373
422
  #
374
423
  def build_data_output
375
424
  output = []
376
-
425
+ rows = @data.size
426
+ # maps the context for each row
427
+ row_contexts = [:default] * rows
428
+ i = 1
429
+ @context_rows.reverse_each do |size|
430
+ row_contexts[(rows - size)...rows] = [@context_rows.size - i] * size
431
+ rows -= size
432
+ i += 1
433
+ end
377
434
  @data.each_index do |row_index|
435
+ # sets the current context
436
+ @current_context = row_contexts[row_index]
378
437
  row = @data[row_index]
379
438
  _row_data = []
380
439
  row = row.to_a if row.is_a? Range
@@ -383,34 +442,37 @@ module Yummi
383
442
  color = nil
384
443
  value = nil
385
444
  column = row[col_index]
386
- colorizer = @colorizers[col_index]
387
- if @null_colorizer and column.nil?
388
- color = @null_colorizer.call(column)
445
+ colorizer = component[:colorizers][col_index]
446
+ if component[:null_colorizer] and column.nil?
447
+ color = component[:null_colorizer].call(column)
389
448
  elsif colorizer
390
449
  arg = colorizer[:use_row] ? IndexedData::new(@aliases, row) : column
391
450
  color = colorizer[:component].call(arg)
392
451
  else
393
452
  color = @colors[:value]
394
453
  end
395
- formatter = @formatters[col_index]
396
- formatter = @null_formatter if column.nil? and @null_formatter
454
+ formatter = component[:formatters][col_index]
455
+ formatter = component[:null_formatter] if column.nil? and @null_formatter
397
456
  value = (formatter ? formatter.call(column) : column)
398
457
 
399
458
  _row_data << {:value => value, :color => color}
400
459
  end
401
- if @row_colorizer
460
+ row_colorizer = component[:row_colorizer]
461
+ if row_colorizer
402
462
  row_data = IndexedData::new @aliases, row
403
- row_color = @row_colorizer.call row_data, row_index
463
+ row_color = row_colorizer.call row_data, row_index
404
464
  _row_data.collect! { |data| data[:color] = row_color; data } if row_color
405
465
  end
406
466
 
407
- _row_data = normalize(_row_data,
408
- :extract => proc do |data|
409
- data[:value].to_s
410
- end,
411
- :new => proc do |value, data|
412
- {:value => value, :color => data[:color]}
413
- end)
467
+ _row_data = normalize(
468
+ _row_data,
469
+ :extract => proc do |data|
470
+ data[:value].to_s
471
+ end,
472
+ :new => proc do |value, data|
473
+ {:value => value, :color => data[:color]}
474
+ end
475
+ )
414
476
  _row_data.each do |_row|
415
477
  output << _row
416
478
  end
@@ -418,7 +480,9 @@ module Yummi
418
480
  output
419
481
  end
420
482
 
421
- private
483
+ def component
484
+ @components[@current_context]
485
+ end
422
486
 
423
487
  def normalize(row, params = {})
424
488
  params[:extract] ||= proc do |value|
@@ -46,7 +46,7 @@ module Yummi
46
46
  :invoke => :format
47
47
 
48
48
  component [:row_color, :colorize_row], :repository => :row_based_colorizers,
49
- :invoke => :row_colorizer,
49
+ :invoke => :colorize_row,
50
50
  :row_based => true
51
51
 
52
52
  component [:state, :health], :repository => :using_row_colorizers,
@@ -74,6 +74,20 @@ module Yummi
74
74
  table.header = config[:header] if config[:header]
75
75
  table.layout = config[:layout].to_sym if config[:layout]
76
76
 
77
+ build_components table, config
78
+ contexts = config[:contexts]
79
+ if contexts
80
+ contexts.each do |context_config|
81
+ table.context context_config do
82
+ build_components table, context_config
83
+ end
84
+ end
85
+ end
86
+
87
+ table
88
+ end
89
+
90
+ def build_components(table, config)
77
91
  components.each do |key, component_config|
78
92
  block = lambda do |params|
79
93
  if component_config[:using_row]
@@ -90,8 +104,6 @@ module Yummi
90
104
  parse_component config[key], component_config, &block
91
105
  end
92
106
  end
93
-
94
- table
95
107
  end
96
108
 
97
109
  def parse_component(definitions, config)
@@ -111,13 +123,12 @@ module Yummi
111
123
  if definitions
112
124
  if definitions.is_a? Hash
113
125
  definitions.each do |component_name, params|
114
- yield(create_component({component_name => params}, config))
126
+ component = create_component({component_name => params}, config)
127
+ yield([{:using => component}])
115
128
  end
116
129
  else
117
- puts definitions
118
130
  component = create_component(definitions, config)
119
- puts component
120
- yield(component)
131
+ yield([{:using => component}])
121
132
  end
122
133
  end
123
134
  end
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.5.0"
24
+ VERSION = "0.5.1"
25
25
  end
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.5.0
4
+ version: 0.5.1
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-10-13 00:00:00.000000000 Z
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email: