yummi 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/cash_flow_table.rb +5 -5
- data/examples/list_files.rb +3 -3
- data/examples/logger.rb +2 -2
- data/examples/monitor_table.rb +5 -5
- data/lib/yummi.rb +102 -48
- data/lib/yummi/logger.rb +14 -4
- data/lib/yummi/table.rb +1 -1
- data/lib/yummi/version.rb +1 -1
- metadata +2 -2
data/examples/cash_flow_table.rb
CHANGED
@@ -31,11 +31,11 @@ opt = OptionParser::new
|
|
31
31
|
# sets the title
|
32
32
|
@table.title = 'Cash Flow'
|
33
33
|
# formats booleans using Yes or No
|
34
|
-
@table.format :eletronic, :using => Yummi::
|
34
|
+
@table.format :eletronic, :using => Yummi::Formatters.yes_or_no
|
35
35
|
# shows values without minus signal and rounded
|
36
36
|
@table.format :value, :using => lambda { |value| "%.2f" % value.abs }
|
37
37
|
# shows totals rounded
|
38
|
-
@table.format :total, :using => Yummi::
|
38
|
+
@table.format :total, :using => Yummi::Formatters.round(2)
|
39
39
|
# table data
|
40
40
|
@table.data = [['Initial', 0, 0, false],
|
41
41
|
['Deposit', 100.58, 100.58, true, "QAWSEDRFTGH535"],
|
@@ -47,7 +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::
|
50
|
+
@table.row_colorizer Yummi::Colorizers.stripe :brown, :purple
|
51
51
|
when 'full'
|
52
52
|
# colorize all values from the Description column to purple
|
53
53
|
@table.colorize :description, :with => :purple
|
@@ -61,11 +61,11 @@ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
|
|
61
61
|
red_to_negative = lambda { |value| :red if value < 0 }
|
62
62
|
green_to_positive = lambda { |value| :green if value > 0 }
|
63
63
|
brown_to_zero = lambda { |value| :brown if value == 0 }
|
64
|
-
colorizer = Yummi::
|
64
|
+
colorizer = Yummi::Colorizers.join(red_to_negative, green_to_positive, brown_to_zero)
|
65
65
|
@table.colorize :value, :using => colorizer
|
66
66
|
@table.colorize :total, :using => colorizer
|
67
67
|
# colorize rows that Value is greater than Total
|
68
|
-
@table.row_colorizer do |
|
68
|
+
@table.row_colorizer do |data| # or |data, index| if you need the index
|
69
69
|
:white if data[:value] > data[:total]
|
70
70
|
end
|
71
71
|
when 'none'
|
data/examples/list_files.rb
CHANGED
@@ -34,14 +34,14 @@ opt = OptionParser::new
|
|
34
34
|
# sets the title
|
35
35
|
@table.title = 'File List'
|
36
36
|
# formats size for easily reading
|
37
|
-
@table.format :size, :using => Yummi::
|
37
|
+
@table.format :size, :using => Yummi::Formatters.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::
|
42
|
+
@table.row_colorizer Yummi::Colorizers.stripe :intense_gray, :intense_white
|
43
43
|
when 'file'
|
44
|
-
@table.row_colorizer do |
|
44
|
+
@table.row_colorizer do |data| # or |data, index| if you need the index
|
45
45
|
data[:directory] ? :intense_gray : :intense_white
|
46
46
|
end
|
47
47
|
when 'none'
|
data/examples/logger.rb
CHANGED
@@ -25,8 +25,8 @@ require_relative '../lib/yummi'
|
|
25
25
|
|
26
26
|
logger = Logger::new STDOUT
|
27
27
|
logger.level = Logger::DEBUG
|
28
|
-
logger.formatter = Yummi::Formatter::LogFormatter.new do |severity,
|
29
|
-
|
28
|
+
logger.formatter = Yummi::Formatter::LogFormatter.new do |severity, message|
|
29
|
+
"[#{Yummi::Aligner.left severity, 5}] #{message}"
|
30
30
|
end
|
31
31
|
|
32
32
|
logger.debug __FILE__
|
data/examples/monitor_table.rb
CHANGED
@@ -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::
|
35
|
-
@table.format :free_memory, :using => Yummi::
|
34
|
+
@table.format :max_memory, :using => Yummi::Formatters.byte
|
35
|
+
@table.format :free_memory, :using => Yummi::Formatters.byte
|
36
36
|
|
37
37
|
# colorizer for memory
|
38
|
-
memory_colorizer = Yummi::
|
38
|
+
memory_colorizer = Yummi::Colorizers.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::
|
45
|
+
thread_colorizer = Yummi::Colorizers.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,7 +51,7 @@ 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::
|
54
|
+
@table.row_colorizer Yummi::Colorizers.stripe :brown, :purple
|
55
55
|
when 'row'
|
56
56
|
@table.row_colorizer memory_colorizer
|
57
57
|
@table.row_colorizer thread_colorizer
|
data/lib/yummi.rb
CHANGED
@@ -21,9 +21,6 @@
|
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
23
|
require_relative "yummi/version"
|
24
|
-
require_relative "yummi/table"
|
25
|
-
require_relative "yummi/text_box"
|
26
|
-
require_relative "yummi/logger"
|
27
24
|
|
28
25
|
module Yummi
|
29
26
|
# Base for colorizing
|
@@ -205,14 +202,50 @@ module Yummi
|
|
205
202
|
|
206
203
|
end
|
207
204
|
|
208
|
-
#
|
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
|
+
#
|
209
210
|
module Colorizer
|
210
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
|
+
color = call *args
|
224
|
+
Yummi.colorize args.first.to_s, color
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
#
|
230
|
+
# Adds the #Colorizer module to the given block, so you can use it to colorize texts.
|
231
|
+
#
|
232
|
+
# === Example
|
233
|
+
#
|
234
|
+
# colorizer = Yummi.to_colorize { |value| value % 2 == 0 ? :green : :blue }
|
235
|
+
# 10.times { |n| puts colorizer.colorize n }
|
236
|
+
#
|
237
|
+
def self.to_colorize &block
|
238
|
+
block.extend Colorizer
|
239
|
+
end
|
240
|
+
|
241
|
+
# A module with useful colorizers
|
242
|
+
module Colorizers
|
243
|
+
|
211
244
|
# Joins the given colorizers to work as one
|
212
245
|
def self.join *colorizers
|
213
246
|
join = Yummi::GroupedComponent::new
|
214
247
|
colorizers.each { |c| join << c }
|
215
|
-
join
|
248
|
+
join.extend Colorizer
|
216
249
|
end
|
217
250
|
|
218
251
|
# Returns a new instance of #DataEvalColorizer
|
@@ -232,6 +265,7 @@ module Yummi
|
|
232
265
|
|
233
266
|
# A colorizer that cycles through colors to create a striped effect
|
234
267
|
class StripeColorizer
|
268
|
+
include Yummi::Colorizer
|
235
269
|
|
236
270
|
# Creates a new colorizer using the given colors
|
237
271
|
def initialize *colors
|
@@ -263,6 +297,7 @@ module Yummi
|
|
263
297
|
# table.using_row.colorize :current, :using => colorizer
|
264
298
|
#
|
265
299
|
class EvalColorizer
|
300
|
+
include Yummi::Colorizer
|
266
301
|
|
267
302
|
def initialize &block
|
268
303
|
@block = block
|
@@ -312,70 +347,85 @@ module Yummi
|
|
312
347
|
# table.using_row.colorize :current, :using => colorizer
|
313
348
|
#
|
314
349
|
class DataEvalColorizer < EvalColorizer
|
315
|
-
include Yummi::BlockHandler
|
350
|
+
include Yummi::BlockHandler, Yummi::Colorizer
|
316
351
|
|
317
352
|
def resolve_value *args
|
318
|
-
block_call args.
|
353
|
+
block_call args.first, &@block # by convention, the first arg is data
|
319
354
|
end
|
320
355
|
|
321
356
|
end
|
322
357
|
|
323
358
|
end
|
324
359
|
|
325
|
-
# A module
|
326
|
-
module
|
327
|
-
|
328
|
-
# A module for formatting units in a way that makes the value easy to read
|
329
|
-
module Unit
|
330
|
-
# Holds the information about the units that are supported in #format
|
331
|
-
UNITS = {
|
332
|
-
:byte => {:range => %w{B KB MB GB TB}, :step => 1024}
|
333
|
-
}
|
360
|
+
# A module used to create an alias method in a formatter block
|
361
|
+
module FormatterBlock
|
334
362
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
# === Args
|
339
|
-
#
|
340
|
-
# +unit+::
|
341
|
-
# A unit defined in #UNITS or a definition
|
342
|
-
# +value+::
|
343
|
-
# The value to format
|
344
|
-
# +params+::
|
345
|
-
# Additional parameters:
|
346
|
-
# * precision: the number of fractional digits to display (defaults to 1)
|
347
|
-
#
|
348
|
-
def self.format unit, value, params = {}
|
349
|
-
unit = UNITS[unit] if unit.is_a? Symbol
|
350
|
-
params[:precision] ||= 1
|
351
|
-
result = value
|
352
|
-
units = unit[:range]
|
353
|
-
units.each_index do |i|
|
354
|
-
minimun = (unit[:step] ** i)
|
355
|
-
result = "%.#{params[:precision]}f #{units[i]}" % (value.to_f / minimun) if value >= minimun
|
356
|
-
end
|
357
|
-
result
|
358
|
-
end
|
363
|
+
# Calls the :call: method
|
364
|
+
def format value
|
365
|
+
call value
|
359
366
|
end
|
360
367
|
|
361
|
-
|
368
|
+
end
|
369
|
+
|
370
|
+
# Extends the given block with #FormatterBlock
|
371
|
+
def self.to_format &block
|
372
|
+
block.extend FormatterBlock
|
373
|
+
end
|
374
|
+
|
375
|
+
# A module with useful formatters
|
376
|
+
module Formatters
|
377
|
+
|
378
|
+
# A formatter for boolean values that uses 'Yes' or 'No'
|
362
379
|
def self.yes_or_no
|
363
|
-
|
380
|
+
Yummi::to_format do |value|
|
364
381
|
value ? "Yes" : "No"
|
365
382
|
end
|
366
383
|
end
|
367
384
|
|
368
|
-
#
|
385
|
+
# A formatter to float values that uses the precision to round the value
|
369
386
|
def self.round precision
|
370
|
-
|
387
|
+
Yummi::to_format do |value|
|
371
388
|
"%.#{precision}f" % value
|
372
389
|
end
|
373
390
|
end
|
374
391
|
|
375
|
-
#
|
376
|
-
|
377
|
-
|
378
|
-
|
392
|
+
# Defines the modes to format a byte value
|
393
|
+
BYTE_MODES = {
|
394
|
+
:iec => {
|
395
|
+
:range => %w{B KiB MiB GiB TiB PiB EiB ZiB YiB},
|
396
|
+
:step => 1024
|
397
|
+
},
|
398
|
+
:si => {
|
399
|
+
:range => %w{B KB MB GB TB PB EB ZB YB},
|
400
|
+
:step => 1000
|
401
|
+
}
|
402
|
+
}
|
403
|
+
|
404
|
+
#
|
405
|
+
# Formats a byte value to ensure easily reading
|
406
|
+
#
|
407
|
+
# === Hash Args
|
408
|
+
#
|
409
|
+
# +precision+::
|
410
|
+
# How many decimal digits should be displayed. (Defaults to 1)
|
411
|
+
# +mode+::
|
412
|
+
# Which mode should be used to display unit symbols. (Defaults to :iec)
|
413
|
+
#
|
414
|
+
# See #BYTE_MODES
|
415
|
+
#
|
416
|
+
def self.byte params = {}
|
417
|
+
Yummi::to_format do |value|
|
418
|
+
value = value.to_i if value.is_a? String
|
419
|
+
mode = (params[:mode] or :iec)
|
420
|
+
range = BYTE_MODES[mode][:range]
|
421
|
+
step = BYTE_MODES[mode][:step]
|
422
|
+
params[:precision] ||= 1
|
423
|
+
result = value
|
424
|
+
range.each_index do |i|
|
425
|
+
minimun = (step ** i)
|
426
|
+
result = "%.#{params[:precision]}f #{range[i]}" % (value.to_f / minimun) if value >= minimun
|
427
|
+
end
|
428
|
+
result
|
379
429
|
end
|
380
430
|
end
|
381
431
|
|
@@ -441,3 +491,7 @@ module Yummi
|
|
441
491
|
end
|
442
492
|
|
443
493
|
require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
|
494
|
+
|
495
|
+
require_relative 'yummi/table'
|
496
|
+
require_relative 'yummi/text_box'
|
497
|
+
require_relative 'yummi/logger'
|
data/lib/yummi/logger.rb
CHANGED
@@ -30,7 +30,7 @@ module Yummi
|
|
30
30
|
# A colorful log formatter
|
31
31
|
#
|
32
32
|
class LogFormatter < Logger::Formatter
|
33
|
-
|
33
|
+
include Yummi::BlockHandler
|
34
34
|
#
|
35
35
|
# Colors for each severity:
|
36
36
|
#
|
@@ -53,7 +53,8 @@ module Yummi
|
|
53
53
|
# * +FATAL+: red (intense)
|
54
54
|
# * +ANY+: gray (intense)
|
55
55
|
#
|
56
|
-
# If a block is passed, it will be used to format the message
|
56
|
+
# If a block is passed, it will be used to format the message. The block can use
|
57
|
+
# the following variables: severity, time, program_name and message.
|
57
58
|
#
|
58
59
|
def initialize &block
|
59
60
|
@colors = {
|
@@ -76,8 +77,17 @@ module Yummi
|
|
76
77
|
|
77
78
|
# Formats the message, override this method instead of #call
|
78
79
|
def output severity, time, program_name, message
|
79
|
-
|
80
|
-
|
80
|
+
if @format_block
|
81
|
+
context = {
|
82
|
+
:severity => severity,
|
83
|
+
:time => time,
|
84
|
+
:program_name => program_name,
|
85
|
+
:message => message
|
86
|
+
}
|
87
|
+
block_call(context, &@format_block) << $/
|
88
|
+
else
|
89
|
+
super_call severity, time, program_name, message
|
90
|
+
end
|
81
91
|
end
|
82
92
|
|
83
93
|
end
|
data/lib/yummi/table.rb
CHANGED
@@ -335,7 +335,7 @@ module Yummi
|
|
335
335
|
end
|
336
336
|
if @row_colorizer
|
337
337
|
row_data = IndexedData::new @aliases, row
|
338
|
-
row_color = @row_colorizer.call
|
338
|
+
row_color = @row_colorizer.call row_data, row_index
|
339
339
|
_colors.collect! { row_color } if row_color
|
340
340
|
end
|
341
341
|
color_map << _colors
|
data/lib/yummi/version.rb
CHANGED
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
|
+
version: 0.3.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-05-
|
12
|
+
date: 2012-05-17 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tool to colorize your console application.
|
15
15
|
email:
|