yummi 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/cash_flow_table.rb +5 -3
- data/examples/monitor_table.rb +1 -1
- data/lib/yummi.rb +82 -81
- data/lib/yummi/color_mapping.rb +60 -0
- data/lib/yummi/table.rb +119 -50
- data/lib/yummi/text_box.rb +5 -5
- data/lib/yummi/version.rb +1 -1
- metadata +3 -2
data/examples/cash_flow_table.rb
CHANGED
@@ -36,13 +36,14 @@ opt = OptionParser::new
|
|
36
36
|
@table.format :value, :using => lambda { |value| "%.2f" % value.abs }
|
37
37
|
# shows totals rounded
|
38
38
|
@table.format :total, :using => Yummi::Formatters.round(2)
|
39
|
+
@table.format_null :with => 'none'
|
39
40
|
# table data
|
40
|
-
@table.data = [['Initial', 0, 0, false],
|
41
|
+
@table.data = [['Initial', 0, 0, false, nil],
|
41
42
|
['Deposit', 100.58, 100.58, true, "QAWSEDRFTGH535"],
|
42
43
|
['Withdraw', -50.23, 50.35, true, "34ERDTF6GYU"],
|
43
44
|
['Withdraw', -100, -49.65, true, "2344EDRFT5"],
|
44
|
-
['Deposit', 50, 0.35, false],
|
45
|
-
['Deposit', 600, 600.35, false]]
|
45
|
+
['Deposit', 50, 0.35, false, nil],
|
46
|
+
['Deposit', 600, 600.35, false, nil]]
|
46
47
|
|
47
48
|
opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
|
48
49
|
case type
|
@@ -68,6 +69,7 @@ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
|
|
68
69
|
@table.row_colorizer do |data| # or |data, index| if you need the index
|
69
70
|
:white if data[:value] > data[:total]
|
70
71
|
end
|
72
|
+
@table.colorize_null :with => :red
|
71
73
|
when 'none'
|
72
74
|
@table.no_colors
|
73
75
|
else
|
data/examples/monitor_table.rb
CHANGED
@@ -67,7 +67,7 @@ opt.on '--color TYPE', 'Specify the color type (zebra,row,cell,none)' do |type|
|
|
67
67
|
end
|
68
68
|
opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
|
69
69
|
case layout
|
70
|
-
when '
|
70
|
+
when 'horizontal'
|
71
71
|
@table.layout = :horizontal
|
72
72
|
when 'vertical'
|
73
73
|
@table.layout = :vertical
|
data/lib/yummi.rb
CHANGED
@@ -25,82 +25,82 @@ require_relative "yummi/version"
|
|
25
25
|
module Yummi
|
26
26
|
# Base for colorizing
|
27
27
|
module Color
|
28
|
+
|
29
|
+
# The Color Schema Definition
|
30
|
+
module Schema
|
31
|
+
# Normal Linux Terminal Colors, used by default in normal, underscore, blink and
|
32
|
+
# highlight color types
|
33
|
+
NORMAL_COLORS = {
|
34
|
+
:colors => [:black, :red, :green, :brown, :blue, :purple, :cyan, :gray],
|
35
|
+
:default => :gray
|
36
|
+
}
|
37
|
+
# Intense Linux Terminal Colors, used by default in intense and strong color types
|
38
|
+
ALTERNATE_COLORS = {
|
39
|
+
:colors => [:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white],
|
40
|
+
:default => :gray
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
28
44
|
# Colors from default linux terminal scheme
|
29
|
-
COLORS = {
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
# Types of color
|
78
|
-
TYPES = {
|
79
|
-
:normal => 0,
|
80
|
-
:intense => 1,
|
81
|
-
:underscored => 4,
|
82
|
-
:blink => 5,
|
83
|
-
:highlight => 7
|
84
|
-
}
|
85
|
-
# Parses the key
|
86
|
-
def self.parse key
|
87
|
-
keys = key.to_s.split '_'
|
88
|
-
type = keys[0].to_sym
|
89
|
-
color = keys[1].to_i
|
90
|
-
"#{TYPES[type]};3#{color - 1}"
|
45
|
+
COLORS = {}
|
46
|
+
|
47
|
+
#
|
48
|
+
# Clears all color mappings and add this ones
|
49
|
+
#
|
50
|
+
# see #add_color_map
|
51
|
+
#
|
52
|
+
def self.load_color_map mappings
|
53
|
+
COLORS.clear
|
54
|
+
add_color_map mappings
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Adds the given color mappings, overriding the already defined.
|
59
|
+
#
|
60
|
+
# Colors are printed using a "[#{type_key_code}:3#{color_key_code}m" prefix.
|
61
|
+
#
|
62
|
+
# === Args
|
63
|
+
#
|
64
|
+
# The mappings is a Hash of Hashes, each parent hash must define a type key and the
|
65
|
+
# child hash must contain the following keys:
|
66
|
+
#
|
67
|
+
# +key_code+::
|
68
|
+
# The key code to map this type. If the type name is :default, the mapping will not
|
69
|
+
# use the name "default" (:normal_red will become only :red)
|
70
|
+
# +schema+::
|
71
|
+
# An array with the color names. Each name will be mapped and their positions
|
72
|
+
# (1 based) too. (:intense_red and :intense_2, for example)
|
73
|
+
#
|
74
|
+
def self.add_color_map mappings
|
75
|
+
mappings.each do |type, config|
|
76
|
+
schema = config[:schema]
|
77
|
+
schema[:colors].each_with_index do |color, key_code|
|
78
|
+
# maps the default color for a type
|
79
|
+
COLORS[type] = "#{config[:key_code]};3#{key_code}" if color == schema[:default]
|
80
|
+
# do not use prefix if schema is default
|
81
|
+
prefix = (type == :default ? '' : "#{type}_")
|
82
|
+
# maps the color using color name
|
83
|
+
key = "#{prefix}#{color}"
|
84
|
+
COLORS[key.to_sym] = "#{config[:key_code]};3#{key_code}"
|
85
|
+
# maps the color using color key code
|
86
|
+
key = "#{prefix}#{key_code + 1}"
|
87
|
+
COLORS[key.to_sym] = "#{config[:key_code]};3#{key_code}"
|
88
|
+
# maps the color using color name if default schema does not defines it
|
89
|
+
# example: yellow and white are present only in strong/intense schema
|
90
|
+
COLORS[color.to_sym] = "#{config[:key_code]};3#{key_code}" unless COLORS[color]
|
91
|
+
end
|
92
|
+
end
|
91
93
|
end
|
92
94
|
|
93
95
|
# Escape the given text with the given color code
|
94
96
|
def self.escape key
|
95
|
-
return key unless key
|
96
|
-
|
97
|
-
color ||= parse(key)
|
98
|
-
"\033[#{color}m"
|
97
|
+
return key unless key and COLORS[key.to_sym]
|
98
|
+
"\033[#{COLORS[key.to_sym]}m"
|
99
99
|
end
|
100
100
|
|
101
101
|
# Colorize the given text with the given color
|
102
102
|
def self.colorize string, color
|
103
|
-
color, end_color = [color,
|
103
|
+
color, end_color = [color, "\033[0;0m"].map { |key| Color.escape(key) }
|
104
104
|
color ? "#{color}#{string}#{end_color}" : string
|
105
105
|
end
|
106
106
|
|
@@ -132,7 +132,7 @@ module Yummi
|
|
132
132
|
# context = :max => 10, :curr => 5, ratio => 0.15
|
133
133
|
# percentage = BlockHandler.call_block(context) { |max,curr| curr.to_f / max }
|
134
134
|
#
|
135
|
-
def block_call context, &block
|
135
|
+
def block_call (context, &block)
|
136
136
|
args = []
|
137
137
|
block.parameters.each do |parameter|
|
138
138
|
args << context[parameter[1]]
|
@@ -219,7 +219,7 @@ module Yummi
|
|
219
219
|
# convention, the first argument must be the object to colorize (to_s is called on it
|
220
220
|
# for getting the text to colorize).
|
221
221
|
#
|
222
|
-
def colorize *args
|
222
|
+
def colorize (*args)
|
223
223
|
color = call *args
|
224
224
|
Yummi.colorize args.first.to_s, color
|
225
225
|
end
|
@@ -268,7 +268,7 @@ module Yummi
|
|
268
268
|
include Yummi::Colorizer
|
269
269
|
|
270
270
|
# Creates a new colorizer using the given colors
|
271
|
-
def initialize *colors
|
271
|
+
def initialize (*colors)
|
272
272
|
@colors = colors
|
273
273
|
@count = -1
|
274
274
|
end
|
@@ -299,7 +299,7 @@ module Yummi
|
|
299
299
|
class EvalColorizer
|
300
300
|
include Yummi::Colorizer
|
301
301
|
|
302
|
-
def initialize &block
|
302
|
+
def initialize (&block)
|
303
303
|
@block = block
|
304
304
|
@colors = []
|
305
305
|
@eval_blocks = []
|
@@ -311,17 +311,17 @@ module Yummi
|
|
311
311
|
#
|
312
312
|
# An objtect that responds to :call may also be used.
|
313
313
|
#
|
314
|
-
def use color, component = nil, &eval_block
|
314
|
+
def use (color, component = nil, &eval_block)
|
315
315
|
@colors << color
|
316
316
|
@eval_blocks << (component or eval_block)
|
317
317
|
end
|
318
318
|
|
319
319
|
# Resolves the value using the main block and given arguments
|
320
|
-
def resolve_value *args
|
320
|
+
def resolve_value (*args)
|
321
321
|
@block.call *args
|
322
322
|
end
|
323
323
|
|
324
|
-
def call *args
|
324
|
+
def call (*args)
|
325
325
|
value = resolve_value *args
|
326
326
|
@eval_blocks.each_index do |i|
|
327
327
|
return @colors[i] if @eval_blocks[i].call(value)
|
@@ -349,7 +349,7 @@ module Yummi
|
|
349
349
|
class DataEvalColorizer < EvalColorizer
|
350
350
|
include Yummi::BlockHandler, Yummi::Colorizer
|
351
351
|
|
352
|
-
def resolve_value *args
|
352
|
+
def resolve_value (*args)
|
353
353
|
block_call args.first, &@block # by convention, the first arg is data
|
354
354
|
end
|
355
355
|
|
@@ -361,7 +361,7 @@ module Yummi
|
|
361
361
|
module FormatterBlock
|
362
362
|
|
363
363
|
# Calls the :call: method
|
364
|
-
def format value
|
364
|
+
def format (value)
|
365
365
|
call value
|
366
366
|
end
|
367
367
|
|
@@ -434,12 +434,12 @@ module Yummi
|
|
434
434
|
# A class to expose indexed data by numeric indexes and aliases.
|
435
435
|
class IndexedData
|
436
436
|
|
437
|
-
def initialize aliases, data
|
437
|
+
def initialize (aliases, data)
|
438
438
|
@aliases = aliases
|
439
439
|
@data = data
|
440
440
|
end
|
441
441
|
|
442
|
-
def []
|
442
|
+
def [](value)
|
443
443
|
if value.is_a? Fixnum
|
444
444
|
@data[value]
|
445
445
|
else
|
@@ -463,21 +463,21 @@ module Yummi
|
|
463
463
|
# should be ignored
|
464
464
|
# - message: the message to send. Defaults to :call
|
465
465
|
#
|
466
|
-
def initialize params = {}
|
466
|
+
def initialize (params = {})
|
467
467
|
@components = []
|
468
468
|
@call_all = params[:call_all]
|
469
469
|
@message = (params[:message] or :call)
|
470
470
|
end
|
471
471
|
|
472
472
|
# Adds a new component
|
473
|
-
def << component = nil, &block
|
473
|
+
def << (component = nil, &block)
|
474
474
|
@components << (component or block)
|
475
475
|
end
|
476
476
|
|
477
477
|
#
|
478
478
|
# Calls the added components by sending the configured message and the given args.
|
479
479
|
#
|
480
|
-
def call *args
|
480
|
+
def call (*args)
|
481
481
|
result = nil
|
482
482
|
@components.each do |component|
|
483
483
|
break if result and not @call_all
|
@@ -492,6 +492,7 @@ end
|
|
492
492
|
|
493
493
|
require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
|
494
494
|
|
495
|
+
require_relative 'yummi/color_mapping'
|
495
496
|
require_relative 'yummi/table'
|
496
497
|
require_relative 'yummi/text_box'
|
497
498
|
require_relative 'yummi/logger'
|
@@ -0,0 +1,60 @@
|
|
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
|
+
Yummi::Color::load_color_map(
|
24
|
+
:default => {
|
25
|
+
:key_code => 0,
|
26
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
27
|
+
},
|
28
|
+
:intense => {
|
29
|
+
:key_code => 1,
|
30
|
+
:schema => Yummi::Color::Schema::ALTERNATE_COLORS
|
31
|
+
},
|
32
|
+
:strong => {
|
33
|
+
:key_code => 1,
|
34
|
+
:schema => Yummi::Color::Schema::ALTERNATE_COLORS
|
35
|
+
},
|
36
|
+
:underscore => {
|
37
|
+
:key_code => 4,
|
38
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
39
|
+
},
|
40
|
+
:underscored => {
|
41
|
+
:key_code => 4,
|
42
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
43
|
+
},
|
44
|
+
:blink => {
|
45
|
+
:key_code => 5,
|
46
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
47
|
+
},
|
48
|
+
:blinking => {
|
49
|
+
:key_code => 5,
|
50
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
51
|
+
},
|
52
|
+
:highlight => {
|
53
|
+
:key_code => 7,
|
54
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
55
|
+
},
|
56
|
+
:highlighted => {
|
57
|
+
:key_code => 7,
|
58
|
+
:schema => Yummi::Color::Schema::NORMAL_COLORS
|
59
|
+
}
|
60
|
+
)
|
data/lib/yummi/table.rb
CHANGED
@@ -43,7 +43,7 @@ module Yummi
|
|
43
43
|
# The colors must be supported by #Yummi#Color#parse or defined in #Yummi#Color#COLORS
|
44
44
|
attr_accessor :colors
|
45
45
|
# The table layout (horizontal or vertical)
|
46
|
-
|
46
|
+
attr_reader :layout
|
47
47
|
|
48
48
|
# Creates a new table with the default attributes:
|
49
49
|
#
|
@@ -64,6 +64,7 @@ module Yummi
|
|
64
64
|
|
65
65
|
@colspan = 2
|
66
66
|
@layout = :horizontal
|
67
|
+
@default_align = :right
|
67
68
|
@aliases = []
|
68
69
|
|
69
70
|
@align = [:left]
|
@@ -71,7 +72,8 @@ module Yummi
|
|
71
72
|
@colorizers = []
|
72
73
|
@row_colorizer = nil
|
73
74
|
|
74
|
-
@
|
75
|
+
@predicated_formatters = nil
|
76
|
+
@predicated_colorizers = nil
|
75
77
|
end
|
76
78
|
|
77
79
|
# Indicates that the table should not use colors.
|
@@ -84,6 +86,18 @@ module Yummi
|
|
84
86
|
@no_colors = true
|
85
87
|
end
|
86
88
|
|
89
|
+
def layout=(layout)
|
90
|
+
@layout = layout
|
91
|
+
case layout
|
92
|
+
when :horizontal
|
93
|
+
@default_align = :right
|
94
|
+
when :vertical
|
95
|
+
@default_align = :left
|
96
|
+
else
|
97
|
+
raise 'Unsupported layout'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
87
101
|
#
|
88
102
|
# Sets the table header. If no aliases are defined, they will be defined as the texts
|
89
103
|
# in lowercase with line breaks and spaces replaced by underscores.
|
@@ -102,20 +116,9 @@ module Yummi
|
|
102
116
|
#
|
103
117
|
# This will create the following aliases: :name, :email, :work_phone and :home_phone
|
104
118
|
#
|
105
|
-
def header= header
|
119
|
+
def header= (header)
|
106
120
|
header = [header] unless header.respond_to? :each
|
107
|
-
|
108
|
-
header.each_index do |i|
|
109
|
-
max = [max, header[i].split("\n").size].max
|
110
|
-
end
|
111
|
-
@header = []
|
112
|
-
max.times { @header << [] }
|
113
|
-
header.each_index do |i|
|
114
|
-
names = header[i].split("\n")
|
115
|
-
names.each_index do |j|
|
116
|
-
@header[j][i] = names[j]
|
117
|
-
end
|
118
|
-
end
|
121
|
+
@header = normalize(header)
|
119
122
|
@aliases = header.map { |n| n.downcase.gsub(' ', '_').gsub("\n", '_').to_sym } if @aliases.empty?
|
120
123
|
end
|
121
124
|
|
@@ -134,7 +137,7 @@ module Yummi
|
|
134
137
|
# table.align :description, :left
|
135
138
|
# table.align :value, :right
|
136
139
|
#
|
137
|
-
def align index, type
|
140
|
+
def align (index, type)
|
138
141
|
index = parse_index(index)
|
139
142
|
@align[index] = type
|
140
143
|
end
|
@@ -150,7 +153,7 @@ module Yummi
|
|
150
153
|
#
|
151
154
|
# table.row_colorizer { |i, row| :red if row[:value] < 0 }
|
152
155
|
#
|
153
|
-
def row_colorizer colorizer = nil, &block
|
156
|
+
def row_colorizer (colorizer = nil, &block)
|
154
157
|
@row_colorizer ||= Yummi::GroupedComponent::new
|
155
158
|
@row_colorizer << (colorizer or block)
|
156
159
|
end
|
@@ -195,13 +198,30 @@ module Yummi
|
|
195
198
|
# table.colorize :description, :with => :purple
|
196
199
|
# table.colorize(:value) { |value| :red if value < 0 }
|
197
200
|
#
|
198
|
-
def colorize index, params = {}, &block
|
201
|
+
def colorize (index, params = {}, &block)
|
199
202
|
index = parse_index(index)
|
200
203
|
@colorizers[index] ||= []
|
201
204
|
obj = (params[:using] or block or (proc { |v| params[:with] }))
|
202
205
|
@colorizers[index] << {:use_row => @using_row, :component => obj}
|
203
206
|
end
|
204
207
|
|
208
|
+
#
|
209
|
+
# Defines a colorizer to null values.
|
210
|
+
#
|
211
|
+
# === Args
|
212
|
+
#
|
213
|
+
# +params+::
|
214
|
+
# A hash with params in case a block is not given:
|
215
|
+
# - :using defines the component to use
|
216
|
+
# - :with defines the format to use
|
217
|
+
#
|
218
|
+
def colorize_null (params = {}, &block)
|
219
|
+
@null_colorizer = (params[:using] or block)
|
220
|
+
@null_colorizer ||= proc do |value|
|
221
|
+
params[:with]
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
205
225
|
#
|
206
226
|
# Sets a component to format a column.
|
207
227
|
#
|
@@ -222,7 +242,7 @@ module Yummi
|
|
222
242
|
#
|
223
243
|
# table.format :value, :with => '%.2f'
|
224
244
|
#
|
225
|
-
def format index, params = {}, &block
|
245
|
+
def format (index, params = {}, &block)
|
226
246
|
index = parse_index(index)
|
227
247
|
@formatters[index] = (params[:using] or block)
|
228
248
|
@formatters[index] ||= proc do |value|
|
@@ -230,10 +250,27 @@ module Yummi
|
|
230
250
|
end
|
231
251
|
end
|
232
252
|
|
253
|
+
#
|
254
|
+
# Defines a formatter to null values.
|
255
|
+
#
|
256
|
+
# === Args
|
257
|
+
#
|
258
|
+
# +params+::
|
259
|
+
# A hash with params in case a block is not given:
|
260
|
+
# - :using defines the component to use
|
261
|
+
# - :with defines the format to use
|
262
|
+
#
|
263
|
+
def format_null (params = {}, &block)
|
264
|
+
@null_formatter = (params[:using] or block)
|
265
|
+
@null_formatter ||= proc do |value|
|
266
|
+
params[:with] % value
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
233
270
|
#
|
234
271
|
# Prints the #to_s into the given object.
|
235
272
|
#
|
236
|
-
def print to = $stdout
|
273
|
+
def print (to = $stdout)
|
237
274
|
to.print to_s
|
238
275
|
end
|
239
276
|
|
@@ -241,35 +278,33 @@ module Yummi
|
|
241
278
|
# Return a colorized and formatted table.
|
242
279
|
#
|
243
280
|
def to_s
|
244
|
-
|
245
|
-
|
281
|
+
header_output = build_header_output
|
282
|
+
data_output = build_data_output
|
246
283
|
|
247
284
|
string = ""
|
248
285
|
string << Color.colorize(@title, @colors[:title]) << $/ if @title
|
249
|
-
color_map = header_color_map + data_color_map
|
250
286
|
table_data = header_output + data_output
|
251
287
|
if @layout == :vertical
|
252
288
|
# don't use array transpose because the data may differ in each line size
|
253
|
-
color_map = rotate color_map
|
254
289
|
table_data = rotate table_data
|
255
290
|
end
|
256
|
-
string << content(
|
291
|
+
string << content(table_data)
|
257
292
|
end
|
258
293
|
|
259
294
|
#
|
260
295
|
# Gets the content string for the given color map and content
|
261
296
|
#
|
262
|
-
def content
|
297
|
+
def content (data)
|
263
298
|
string = ""
|
264
299
|
data.each_index do |i|
|
265
300
|
row = data[i]
|
266
301
|
row.each_index do |j|
|
267
302
|
column = row[j]
|
303
|
+
column ||= {:value => nil, :color => nil}
|
268
304
|
width = max_width data, j
|
269
305
|
alignment = (@align[j] or @default_align)
|
270
|
-
|
271
|
-
value =
|
272
|
-
value = Color.colorize value, color unless @no_colors
|
306
|
+
value = Aligner.align alignment, column[:value].to_s, width
|
307
|
+
value = Color.colorize value, column[:color] unless @no_colors
|
273
308
|
string << value
|
274
309
|
string << (' ' * @colspan)
|
275
310
|
end
|
@@ -284,20 +319,16 @@ module Yummi
|
|
284
319
|
# Returns the color map and the header.
|
285
320
|
#
|
286
321
|
def build_header_output
|
287
|
-
color_map = []
|
288
322
|
output = []
|
289
323
|
|
290
324
|
@header.each do |line|
|
291
|
-
_colors = []
|
292
325
|
_data = []
|
293
326
|
line.each do |h|
|
294
|
-
|
295
|
-
_data << h
|
327
|
+
_data << {:value => h, :color => @colors[:header]}
|
296
328
|
end
|
297
|
-
color_map << _colors
|
298
329
|
output << _data
|
299
330
|
end
|
300
|
-
|
331
|
+
output
|
301
332
|
end
|
302
333
|
|
303
334
|
#
|
@@ -306,61 +337,99 @@ module Yummi
|
|
306
337
|
# Returns the color map and the formatted data.
|
307
338
|
#
|
308
339
|
def build_data_output
|
309
|
-
color_map = []
|
310
340
|
output = []
|
311
341
|
|
312
342
|
@data.each_index do |row_index|
|
313
343
|
row = @data[row_index]
|
314
|
-
|
315
|
-
_data = []
|
344
|
+
_row_data = []
|
316
345
|
row = row.to_a if row.is_a? Range
|
317
346
|
row.each_index do |col_index|
|
318
347
|
next if not @header.empty? and @header[0].size < col_index + 1
|
348
|
+
color = nil
|
349
|
+
value = nil
|
319
350
|
column = row[col_index]
|
320
351
|
colorizers = @colorizers[col_index]
|
321
|
-
if
|
352
|
+
if @null_colorizer and column.nil?
|
353
|
+
color = @null_colorizer.call(column)
|
354
|
+
elsif colorizers
|
322
355
|
colorizers.each do |colorizer|
|
323
356
|
arg = colorizer[:use_row] ? IndexedData::new(@aliases, row) : column
|
324
357
|
c = colorizer[:component].call(arg)
|
325
358
|
if c
|
326
|
-
|
359
|
+
color = c
|
327
360
|
break
|
328
361
|
end
|
329
362
|
end
|
330
363
|
else
|
331
|
-
|
364
|
+
color = @colors[:value]
|
332
365
|
end
|
333
366
|
formatter = @formatters[col_index]
|
334
|
-
|
367
|
+
formatter = @null_formatter if column.nil? and @null_formatter
|
368
|
+
value = (formatter ? formatter.call(column) : column)
|
369
|
+
|
370
|
+
_row_data << {:value => value, :color => color}
|
335
371
|
end
|
336
372
|
if @row_colorizer
|
337
373
|
row_data = IndexedData::new @aliases, row
|
338
374
|
row_color = @row_colorizer.call row_data, row_index
|
339
|
-
|
375
|
+
_row_data.collect! { |data| data[:color] = row_color; data } if row_color
|
340
376
|
end
|
341
|
-
color_map << _colors
|
342
|
-
output << _data
|
343
|
-
end
|
344
377
|
|
345
|
-
|
378
|
+
_row_data = normalize(_row_data,
|
379
|
+
:extract => proc do |data|
|
380
|
+
data[:value].to_s
|
381
|
+
end,
|
382
|
+
:new => proc do |value, data|
|
383
|
+
{:value => value, :color => data[:color]}
|
384
|
+
end
|
385
|
+
)
|
386
|
+
_row_data.each do |_row|
|
387
|
+
output << _row
|
388
|
+
end
|
389
|
+
end
|
390
|
+
output
|
346
391
|
end
|
347
392
|
|
348
393
|
private
|
349
394
|
|
395
|
+
def normalize(row, params = {})
|
396
|
+
params[:extract] ||= proc do |value|
|
397
|
+
value.to_s
|
398
|
+
end
|
399
|
+
params[:new] ||= proc do |extracted, value|
|
400
|
+
extracted
|
401
|
+
end
|
402
|
+
max = 0
|
403
|
+
row.each_index do |i|
|
404
|
+
max = [max, params[:extract].call(row[i]).split("\n").size].max
|
405
|
+
end
|
406
|
+
result = []
|
407
|
+
max.times { result << [] }
|
408
|
+
row.each_index do |i|
|
409
|
+
names = params[:extract].call(row[i]).split("\n")
|
410
|
+
names.each_index do |j|
|
411
|
+
result[j][i] = params[:new].call(names[j], row[i])
|
412
|
+
end
|
413
|
+
end
|
414
|
+
result
|
415
|
+
end
|
416
|
+
|
350
417
|
def parse_index(value)
|
351
418
|
return @aliases.index(value) unless value.is_a? Fixnum
|
352
419
|
value
|
353
420
|
end
|
354
421
|
|
355
|
-
def max_width
|
422
|
+
def max_width(data, column)
|
356
423
|
max = 0
|
357
424
|
data.each do |row|
|
358
|
-
|
425
|
+
var = row[column]
|
426
|
+
var ||= {}
|
427
|
+
max = [var[:value].to_s.length, max].max
|
359
428
|
end
|
360
429
|
max
|
361
430
|
end
|
362
431
|
|
363
|
-
def rotate
|
432
|
+
def rotate(data)
|
364
433
|
new_data = []
|
365
434
|
data.each_index do |i|
|
366
435
|
data[i].each_index do |j|
|
data/lib/yummi/text_box.rb
CHANGED
@@ -58,7 +58,7 @@ module Yummi
|
|
58
58
|
# If the #width is set, this will override the box width for this lines.
|
59
59
|
# align: the text alignment (see #Yummi#Aligner)
|
60
60
|
#
|
61
|
-
def add text, params = {}
|
61
|
+
def add (text, params = {})
|
62
62
|
params = {
|
63
63
|
:width => @width,
|
64
64
|
:align => @default_align
|
@@ -87,7 +87,7 @@ module Yummi
|
|
87
87
|
end
|
88
88
|
|
89
89
|
# Adds the given object as it
|
90
|
-
def << object
|
90
|
+
def << (object)
|
91
91
|
text = object.to_s
|
92
92
|
text.each_line do |line|
|
93
93
|
add line
|
@@ -107,7 +107,7 @@ module Yummi
|
|
107
107
|
# width: the separator width (#self#width will be used if unset)
|
108
108
|
# align: the separator alignment (see #Yummi#Aligner)
|
109
109
|
#
|
110
|
-
def separator pattern = @default_separator[:pattern], params = {}
|
110
|
+
def separator (pattern = @default_separator[:pattern], params = {})
|
111
111
|
unless pattern.is_a? String
|
112
112
|
params = pattern
|
113
113
|
pattern = @default_separator[:pattern]
|
@@ -127,7 +127,7 @@ module Yummi
|
|
127
127
|
end
|
128
128
|
|
129
129
|
# Prints the #to_s into the given object.
|
130
|
-
def print to = $stdout
|
130
|
+
def print (to = $stdout)
|
131
131
|
to.print to_s
|
132
132
|
end
|
133
133
|
|
@@ -155,7 +155,7 @@ module Yummi
|
|
155
155
|
|
156
156
|
private
|
157
157
|
|
158
|
-
def _add_ text, params
|
158
|
+
def _add_ (text, params)
|
159
159
|
if params[:align] and params[:width]
|
160
160
|
text = Yummi::Aligner.align params[:align], text, params[:width]
|
161
161
|
end
|
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.4.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-07-
|
12
|
+
date: 2012-07-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tool to colorize your console application.
|
15
15
|
email:
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- examples/logger.rb
|
33
33
|
- examples/monitor_table.rb
|
34
34
|
- lib/yummi.rb
|
35
|
+
- lib/yummi/color_mapping.rb
|
35
36
|
- lib/yummi/logger.rb
|
36
37
|
- lib/yummi/no_colors.rb
|
37
38
|
- lib/yummi/table.rb
|