yummi 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,13 +34,12 @@ opt = OptionParser::new
34
34
  # sets the title
35
35
  @table.title = 'Files in home folder'
36
36
  # formats size for easily reading
37
- @table.format :size, :using => Yummi::Formatter.bytes
37
+ @table.format :size, :using => Yummi::Formatter.unit(:byte)
38
38
 
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::IndexedDataColorizer.odd :with => :intense_gray
43
- @table.row_colorizer Yummi::IndexedDataColorizer.even :with => :intense_white
42
+ @table.row_colorizer Yummi::IndexedDataColorizer.zebra :intense_gray, :intense_white
44
43
  when 'file'
45
44
  @table.row_colorizer do |i, data|
46
45
  data[:directory] ? :intense_gray : :intense_white
@@ -31,18 +31,18 @@ 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::Formatter.bytes
35
- @table.format :free_memory, :using => Yummi::Formatter.bytes
34
+ @table.format :max_memory, :using => Yummi::Formatter.unit(:byte)
35
+ @table.format :free_memory, :using => Yummi::Formatter.unit(:byte)
36
36
 
37
37
  # colorizer for memory
38
- memory_colorizer = Yummi::Colorizer.by_eval do |free_memory, max_memory|
38
+ memory_colorizer = Yummi::Colorizer.by_data_eval do |free_memory, max_memory|
39
39
  free_memory.to_f / max_memory
40
40
  end
41
41
  memory_colorizer.use(:red) { |value| value > 0.1 and value < 0.3 }
42
42
  memory_colorizer.use(:intense_red) { |value| value <= 0.1 }
43
43
 
44
44
  # colorizer for threads
45
- thread_colorizer = Yummi::Colorizer.by_eval do |max_threads, in_use_threads|
45
+ thread_colorizer = Yummi::Colorizer.by_data_eval do |max_threads, in_use_threads|
46
46
  in_use_threads.to_f / max_threads
47
47
  end
48
48
  thread_colorizer.use(:brown) { |value| value > 0.7 and value < 0.9 }
@@ -51,14 +51,15 @@ thread_colorizer.use(:intense_cyan) { |value| value >= 0.9 }
51
51
  opt.on '--color TYPE', 'Specify the color type (zebra,row,cell,none)' do |type|
52
52
  case type
53
53
  when 'zebra'
54
- @table.row_colorizer Yummi::IndexedDataColorizer.odd :with => :brown
55
- @table.row_colorizer Yummi::IndexedDataColorizer.even :with => :purple
54
+ @table.row_colorizer Yummi::Colorizer.by_index.zebra :brown, :purple
56
55
  when 'row'
57
56
  @table.row_colorizer memory_colorizer
58
57
  @table.row_colorizer thread_colorizer
59
58
  when 'cell'
60
- @table.using_row.colorize :free_memory, :using => memory_colorizer
61
- @table.using_row.colorize :in_use_threads, :using => thread_colorizer
59
+ @table.using_row do
60
+ @table.colorize :free_memory, :using => memory_colorizer
61
+ @table.colorize :in_use_threads, :using => thread_colorizer
62
+ end
62
63
  when 'none'
63
64
  @table.no_colors
64
65
  else
@@ -47,8 +47,7 @@ opt = OptionParser::new
47
47
  opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
48
48
  case type
49
49
  when 'zebra'
50
- @table.row_colorizer Yummi::IndexedDataColorizer.odd :with => :brown
51
- @table.row_colorizer Yummi::IndexedDataColorizer.even :with => :purple
50
+ @table.row_colorizer Yummi::Colorizer.by_index.zebra :brown, :purple
52
51
  when 'full'
53
52
  # colorize all values from the Description column to purple
54
53
  @table.colorize :description, :with => :purple
data/lib/yummi/table.rb CHANGED
@@ -82,8 +82,6 @@ module Yummi
82
82
  @no_colors = true
83
83
  end
84
84
 
85
- #
86
- # === Description
87
85
  #
88
86
  # Sets the table header. If no aliases are defined, they will be defined as the texts
89
87
  # in lowercase with line breaks and spaces replaced by underscores.
@@ -118,8 +116,6 @@ module Yummi
118
116
  @aliases = header.map { |n| n.downcase.gsub(' ', '_').gsub("\n", '_').to_sym } if @aliases.empty?
119
117
  end
120
118
 
121
- #
122
- # === Description
123
119
  #
124
120
  # Sets the align for a column in the table. #Yummi#Aligner should respond to it.
125
121
  #
@@ -140,8 +136,6 @@ module Yummi
140
136
  @align[index] = type
141
137
  end
142
138
 
143
- #
144
- # === Description
145
139
  #
146
140
  # Adds a component to colorize the entire row (overrides column color).
147
141
  # The component must respond to +call+ with the index and the row as the arguments and
@@ -154,32 +148,27 @@ module Yummi
154
148
  # table.row_colorizer { |i, row| :red if row[:value] < 0 }
155
149
  #
156
150
  def row_colorizer colorizer = nil, &block
157
- @row_colorizer ||= Yummi::LinkedBlocks::new
151
+ @row_colorizer ||= Yummi::GroupedComponent::new
158
152
  @row_colorizer << (colorizer or block)
159
153
  end
160
154
 
161
- #
162
- # === Description
163
155
  #
164
156
  # Indicates that the column colorizer (#colorize) should receive the entire row as the
165
- # argument instead of just the column value.
166
- #
167
- # === Return
168
- #
169
- # +self+
157
+ # argument instead of just the column value for all definitions inside of the given
158
+ # block.
170
159
  #
171
160
  # === Example
172
161
  #
173
- # table.using_row.colorize(:value) { |row| :red if row[:value] < row[:average] }
174
- #
162
+ # table.using_row do
163
+ # table.colorize(:value) { |row| :red if row[:value] < row[:average] }
164
+ # end
175
165
  #
176
166
  def using_row
177
167
  @using_row = true
178
- self
168
+ yield
169
+ @using_row = false
179
170
  end
180
171
 
181
- #
182
- # === Description
183
172
  #
184
173
  # Sets a component to colorize a column.
185
174
  #
@@ -208,11 +197,8 @@ module Yummi
208
197
  @colorizers[index] ||= []
209
198
  obj = (params[:using] or block or (proc { |v| params[:with] }))
210
199
  @colorizers[index] << {:use_row => @using_row, :component => obj}
211
- @using_row = false
212
200
  end
213
201
 
214
- #
215
- # === Description
216
202
  #
217
203
  # Sets a component to format a column.
218
204
  #
@@ -255,16 +241,22 @@ module Yummi
255
241
  header_color_map, header_output = build_header_output
256
242
  data_color_map, data_output = build_data_output
257
243
 
258
- color_map = header_color_map + data_color_map
259
- output = header_output + data_output
260
-
261
244
  string = ""
262
245
  string << Color.colorize(@title, @colors[:title]) << $/ if @title
263
- output.each_index do |i|
264
- row = output[i]
246
+ string << content(header_color_map + data_color_map,
247
+ header_output + data_output)
248
+ end
249
+
250
+ #
251
+ # Gets the content string for the given color map and content
252
+ #
253
+ def content color_map, data
254
+ string = ""
255
+ data.each_index do |i|
256
+ row = data[i]
265
257
  row.each_index do |j|
266
258
  column = row[j]
267
- width = max_width output, j
259
+ width = max_width data, j
268
260
  align = (@align[j] or @default_align)
269
261
  color = color_map[i][j]
270
262
  value = Aligner.send align, column.to_s, width
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.0.2"
24
+ VERSION = "0.0.3"
25
25
  end
data/lib/yummi.rb CHANGED
@@ -109,46 +109,90 @@ module Yummi
109
109
  end
110
110
  end
111
111
 
112
- module Aligner
113
-
114
- def self.right text, width
115
- text.rjust(width)
112
+ #
113
+ # A module to handle blocks by dynamically resolving parameters
114
+ #
115
+ # see #DataEvalColorizer
116
+ #
117
+ module BlockHandler
118
+
119
+ #
120
+ # Calls the block resolving the parameters by getting the parameter name from the
121
+ # given context.
122
+ #
123
+ # === Example
124
+ #
125
+ # context = :max => 10, :curr => 5, ratio => 0.15
126
+ # percentage = BlockHandler.call_block(context) { |max,curr| curr.to_f / max }
127
+ #
128
+ def block_call context, &block
129
+ args = []
130
+ block.parameters.each do |parameter|
131
+ args << context[parameter[1]]
132
+ end
133
+ block.call *args
116
134
  end
117
135
 
118
- def self.left text, width
119
- text.ljust(width)
120
- end
136
+ module_function :block_call
121
137
 
122
138
  end
123
139
 
124
- module IndexedDataColorizer
140
+ # A module to align texts based on a reference width
141
+ module Aligner
125
142
 
126
- def self.odd params
127
- lambda do |index, data|
128
- params[:with] if index.odd?
129
- end
143
+ # Aligns the text to the right
144
+ def self.right text, width
145
+ text.rjust(width)
130
146
  end
131
147
 
132
- def self.even params
133
- lambda do |index, data|
134
- params[:with] if index.even?
135
- end
148
+ # Aligns the text to the left
149
+ def self.left text, width
150
+ text.ljust(width)
136
151
  end
137
152
 
138
153
  end
139
154
 
155
+ # A module with useful colorizers
140
156
  module Colorizer
141
157
 
158
+ # Joins the given colorizers to work as one
142
159
  def self.join *colorizers
143
- join = Yummi::LinkedBlocks::new
160
+ join = Yummi::GroupedComponent::new
144
161
  colorizers.each { |c| join << c }
145
162
  join
146
163
  end
147
164
 
165
+ # Returns a new instance of #DataEvalColorizer
166
+ def self.by_data_eval &block
167
+ DataEvalColorizer::new &block
168
+ end
169
+
170
+ # Returns a new instance of #EvalColorizer
148
171
  def self.by_eval &block
149
172
  EvalColorizer::new &block
150
173
  end
151
174
 
175
+ # Returns the #IndexedDataColorizer module
176
+ def self.by_index
177
+ IndexedDataColorizer
178
+ end
179
+
180
+ #
181
+ # A colorizer that evaluates a main block and returns a color based on other blocks.
182
+ #
183
+ # The main block must be compatible with the colorizing type (receiving a column
184
+ # value in case of a table column colorizer or the row index and row value in case
185
+ # of a table row colorizer).
186
+ #
187
+ # === Example
188
+ #
189
+ # # assuming that the table has :max and :current aliases
190
+ # colorizer = DataEvalColorizer::new { |index, data| data[:current] / data[:max] }
191
+ # # the result of the expression above will be passed to this block
192
+ # colorizer.use(:red) { |value| value >= 0.9 }
193
+ #
194
+ # table.using_row.colorize :current, :using => colorizer
195
+ #
152
196
  class EvalColorizer
153
197
 
154
198
  def initialize &block
@@ -157,13 +201,24 @@ module Yummi
157
201
  @eval_blocks = []
158
202
  end
159
203
 
160
- def use color, &eval_block
204
+ #
205
+ # Uses the given color if the given block returns something when evaluated with the
206
+ # result of main block.
207
+ #
208
+ # An objtect that responds to :call may also be used.
209
+ #
210
+ def use color, component = nil, &eval_block
161
211
  @colors << color
162
- @eval_blocks << eval_block
212
+ @eval_blocks << (component or eval_block)
213
+ end
214
+
215
+ # Resolves the value using the main block and given arguments
216
+ def resolve_value *args
217
+ @block.call *args
163
218
  end
164
219
 
165
220
  def call *args
166
- value = Yummi.call_block args.last, &@block # by convention, the last arg is data
221
+ value = resolve_value *args
167
222
  @eval_blocks.each_index do |i|
168
223
  return @colors[i] if @eval_blocks[i].call(value)
169
224
  end
@@ -172,40 +227,117 @@ module Yummi
172
227
 
173
228
  end
174
229
 
230
+ #
231
+ # A colorizer that evaluates a main block and returns a color based on other blocks.
232
+ #
233
+ # The main block can receive any parameters and the names must be aliases the current
234
+ # evaluated data.
235
+ #
236
+ # === Example
237
+ #
238
+ # # assuming that the table has :max and :current aliases
239
+ # colorizer = DataEvalColorizer::new { |max, current| current / max }
240
+ # # the result of the expression above will be passed to this block
241
+ # colorizer.use(:red) { |value| value >= 0.9 }
242
+ #
243
+ # table.using_row.colorize :current, :using => colorizer
244
+ #
245
+ class DataEvalColorizer < EvalColorizer
246
+ include Yummi::BlockHandler
247
+
248
+ def resolve_value *args
249
+ block_call args.last, &@block # by convention, the last arg is data
250
+ end
251
+
252
+ end
253
+
254
+ # A module with colorizers that uses indexes
255
+ module IndexedDataColorizer
256
+
257
+ # Returns a colorizer that uses the given color in odd indexes
258
+ def self.odd color
259
+ lambda do |index, data|
260
+ color if index.odd?
261
+ end
262
+ end
263
+
264
+ # Returns a colorizer that uses the given color in even indexes
265
+ def self.even color
266
+ lambda do |index, data|
267
+ color if index.even?
268
+ end
269
+ end
270
+
271
+ # Returns a colorizer that uses the first color for odd indexes and the second for
272
+ # even indexes.
273
+ def self.zebra first_color, second_color
274
+ Yummi::Colorizer.join odd(first_color), even(second_color)
275
+ end
276
+
277
+ end
278
+
175
279
  end
176
280
 
281
+ # A module with useful formatters
177
282
  module Formatter
178
283
 
284
+ # A module for formatting units in a way that makes the value easy to read
285
+ module Unit
286
+ # Holds the information about the units that are supported in #format
287
+ UNITS = {
288
+ :byte => {:range => %w{B KB MB GB TB}, :step => 1024}
289
+ }
290
+
291
+ #
292
+ # Formats the value using the given unit.
293
+ #
294
+ # === Args
295
+ #
296
+ # +unit+::
297
+ # A unit defined in #UNITS or a definition
298
+ # +value+::
299
+ # The value to format
300
+ # +params+::
301
+ # Additional parameters:
302
+ # * precision: the number of fractional digits to display (defaults to 1)
303
+ #
304
+ def self.format unit, value, params = {}
305
+ unit = UNITS[unit] if unit.is_a? Symbol
306
+ params[:precision] ||= 1
307
+ result = value
308
+ units = unit[:range]
309
+ units.each_index do |i|
310
+ minimun = (unit[:step] ** i)
311
+ result = "%.#{params[:precision]}f #{units[i]}" % (value.to_f / minimun) if value >= minimun
312
+ end
313
+ result
314
+ end
315
+ end
316
+
317
+ # Formats boolean values using 'Yes' or 'No'
179
318
  def self.yes_or_no
180
319
  lambda do |value|
181
320
  value ? "Yes" : "No"
182
321
  end
183
322
  end
184
323
 
324
+ # Formats a float value by rounding to the given decinal digits
185
325
  def self.round precision
186
326
  lambda do |value|
187
327
  "%.#{precision}f" % value
188
328
  end
189
329
  end
190
330
 
191
- def self.unit params
331
+ # see #Unit#format
332
+ def self.unit unit, params = {}
192
333
  lambda do |value|
193
- result = value
194
- units = params[:range]
195
- units.each_index do |i|
196
- minimun = (params[:step] ** i)
197
- result = "%.#{params[:precision]}f #{units[i]}" % (value.to_f / minimun) if value >= minimun
198
- end
199
- result
334
+ Unit.format unit, value, params
200
335
  end
201
336
  end
202
337
 
203
- def self.bytes precision = 1
204
- unit :range => %w{B KB MB GB TB}, :step => 1024, :precision => precision
205
- end
206
-
207
338
  end
208
339
 
340
+ # A class to expose indexed data by numeric indexes and aliases.
209
341
  class IndexedData
210
342
 
211
343
  def initialize aliases, data
@@ -223,36 +355,45 @@ module Yummi
223
355
 
224
356
  end
225
357
 
226
- class LinkedBlocks
227
-
358
+ # A class to group components and blocks
359
+ class GroupedComponent
360
+
361
+ #
362
+ # Creates a new GroupedComponent
363
+ #
364
+ # === Args
365
+ #
366
+ # +params+::
367
+ # Hash parameters:
368
+ # - call_all: indicates if all components must be called. For use if the return
369
+ # should be ignored
370
+ # - message: the message to send. Defaults to :call
371
+ #
228
372
  def initialize params = {}
229
- @blocks = []
373
+ @components = []
230
374
  @call_all = params[:call_all]
375
+ @message = (params[:message] or :call)
231
376
  end
232
377
 
233
- def << block
234
- @blocks << block
378
+ # Adds a new component
379
+ def << component = nil, &block
380
+ @components << (component or block)
235
381
  end
236
382
 
383
+ #
384
+ # Calls the added components by sending the configured message and the given args.
385
+ #
237
386
  def call *args
238
387
  result = nil
239
- @blocks.each do |block|
388
+ @components.each do |component|
240
389
  break if result and not @call_all
241
- result = block.call *args
390
+ result = component.send @message, *args
242
391
  end
243
392
  result
244
393
  end
245
394
 
246
395
  end
247
396
 
248
- def self.call_block params, &block
249
- args = []
250
- block.parameters.each do |parameter|
251
- args << params[parameter[1]]
252
- end
253
- block.call *args
254
- end
255
-
256
397
  end
257
398
 
258
399
  require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
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.0.2
4
+ version: 0.0.3
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-04-10 00:00:00.000000000Z
12
+ date: 2012-04-15 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email: