yummi 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/list_files.rb +1 -3
- data/examples/logger.rb +5 -3
- data/examples/monitor_table.rb +86 -0
- data/examples/table_cash_flow.rb +16 -19
- data/lib/yummi/logger.rb +30 -2
- data/lib/yummi/table.rb +218 -27
- data/lib/yummi/version.rb +1 -1
- data/lib/yummi.rb +60 -10
- metadata +3 -2
data/examples/list_files.rb
CHANGED
@@ -28,13 +28,11 @@ opt = OptionParser::new
|
|
28
28
|
@basedir = ENV['HOME']
|
29
29
|
|
30
30
|
@table = Yummi::Table::new
|
31
|
-
# setting the header sets the aliases
|
31
|
+
# setting the header sets the aliases automatically
|
32
32
|
@table.header = ['Name', 'Size']
|
33
33
|
@table.aliases << :directory
|
34
34
|
# sets the title
|
35
35
|
@table.title = 'Files in home folder'
|
36
|
-
# aligns the first column to the left
|
37
|
-
@table.align :name, :left
|
38
36
|
# formats size for easily reading
|
39
37
|
@table.format :size, :using => Yummi::Formatter.bytes
|
40
38
|
|
data/examples/logger.rb
CHANGED
@@ -25,11 +25,13 @@ 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
|
28
|
+
logger.formatter = Yummi::Formatter::LogFormatter.new do |severity, time, program_name, message|
|
29
|
+
message << $/
|
30
|
+
end
|
29
31
|
|
30
|
-
logger.debug
|
32
|
+
logger.debug __FILE__
|
31
33
|
logger.info "Example started"
|
32
|
-
logger.warn "
|
34
|
+
logger.warn "Warning message"
|
33
35
|
logger.error "An error has occurred"
|
34
36
|
logger.fatal "A fatal exception has occurred"
|
35
37
|
logger.unknown "Unknown severity message"
|
@@ -0,0 +1,86 @@
|
|
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
|
+
require 'optparse'
|
24
|
+
require_relative '../lib/yummi'
|
25
|
+
|
26
|
+
opt = OptionParser::new
|
27
|
+
|
28
|
+
@table = Yummi::Table::new
|
29
|
+
# setting the header sets the aliases automatically
|
30
|
+
@table.header = ['Server Name', 'Max Memory', 'Free Memory', "Max\nThreads", "In Use\nThreads"]
|
31
|
+
# sets the title
|
32
|
+
@table.title = 'Server Runtime Info'
|
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
|
36
|
+
|
37
|
+
# colorizer for memory
|
38
|
+
memory_colorizer = Yummi::Colorizer.by_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 }
|
43
|
+
|
44
|
+
# colorizer for threads
|
45
|
+
thread_colorizer = Yummi::Colorizer.by_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 }
|
50
|
+
|
51
|
+
opt.on '--color TYPE', 'Specify the color type (zebra,row,cell,none)' do |type|
|
52
|
+
case type
|
53
|
+
when 'zebra'
|
54
|
+
@table.row_colorizer Yummi::IndexedDataColorizer.odd :with => :brown
|
55
|
+
@table.row_colorizer Yummi::IndexedDataColorizer.even :with => :purple
|
56
|
+
when 'row'
|
57
|
+
@table.row_colorizer memory_colorizer
|
58
|
+
@table.row_colorizer thread_colorizer
|
59
|
+
when 'cell'
|
60
|
+
@table.using_row.colorize :free_memory, :using => memory_colorizer
|
61
|
+
@table.using_row.colorize :in_use_threads, :using => thread_colorizer
|
62
|
+
when 'none'
|
63
|
+
@table.no_colors
|
64
|
+
else
|
65
|
+
end
|
66
|
+
end
|
67
|
+
opt.on '--help', 'Prints this message' do
|
68
|
+
puts opt
|
69
|
+
exit 0
|
70
|
+
end
|
71
|
+
|
72
|
+
opt.parse ARGV
|
73
|
+
|
74
|
+
# sets the table data
|
75
|
+
@table.data = [
|
76
|
+
['Server 1', 1_000_000, 750_000, 200, 170],
|
77
|
+
['Server 2', 1_000_000, 700_000, 200, 180],
|
78
|
+
['Server 3', 1_000_000, 50_000, 200, 50],
|
79
|
+
['Server 4', 1_000_000, 200_000, 200, 50],
|
80
|
+
['Server 5', 1_000_000, 5_000, 200, 50],
|
81
|
+
['Server 6', 1_000_000, 750_000, 200, 50],
|
82
|
+
['Server 6', 1_000_000, 200_000, 200, 170],
|
83
|
+
['Server 6', 1_000_000, 5_000, 200, 180],
|
84
|
+
]
|
85
|
+
|
86
|
+
@table.print
|
data/examples/table_cash_flow.rb
CHANGED
@@ -26,26 +26,23 @@ require_relative '../lib/yummi'
|
|
26
26
|
opt = OptionParser::new
|
27
27
|
|
28
28
|
@table = Yummi::Table::new
|
29
|
-
# setting the header sets the aliases
|
30
|
-
@table.header = ['Description', 'Value', 'Total', 'Eletronic']
|
29
|
+
# setting the header sets the aliases automatically
|
30
|
+
@table.header = ['Description', 'Value', 'Total', 'Eletronic', "Authentication\nCode"]
|
31
31
|
# sets the title
|
32
32
|
@table.title = 'Cash Flow'
|
33
|
-
# aligns the first column to the left
|
34
|
-
@table.align :description, :left
|
35
|
-
# colorize all values from the Description column to purple
|
36
33
|
# formats booleans using Yes or No
|
37
34
|
@table.format :eletronic, :using => Yummi::Formatter.yes_or_no
|
38
35
|
# shows values without minus signal and rounded
|
39
36
|
@table.format :value, :using => lambda { |value| "%.2f" % value.abs }
|
40
37
|
# shows totals rounded
|
41
|
-
@table.format :total, :
|
38
|
+
@table.format :total, :using => Yummi::Formatter.round(2)
|
42
39
|
# table data
|
43
40
|
@table.data = [['Initial', 0, 0, false],
|
44
|
-
['Deposit', 100, 100, true],
|
45
|
-
['Withdraw', -50, 50, true],
|
46
|
-
['Withdraw', -100, -
|
47
|
-
['Deposit', 50, 0, false],
|
48
|
-
['Deposit', 600, 600, false]]
|
41
|
+
['Deposit', 100.58, 100.58, true, "QAWSEDRFTGH535"],
|
42
|
+
['Withdraw', -50.23, 50.35, true, "34ERDTF6GYU"],
|
43
|
+
['Withdraw', -100, -49.65, true, "2344EDRFT5"],
|
44
|
+
['Deposit', 50, 0.35, false],
|
45
|
+
['Deposit', 600, 600.35, false]]
|
49
46
|
|
50
47
|
opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
|
51
48
|
case type
|
@@ -53,22 +50,22 @@ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
|
|
53
50
|
@table.row_colorizer Yummi::IndexedDataColorizer.odd :with => :brown
|
54
51
|
@table.row_colorizer Yummi::IndexedDataColorizer.even :with => :purple
|
55
52
|
when 'full'
|
53
|
+
# colorize all values from the Description column to purple
|
56
54
|
@table.colorize :description, :with => :purple
|
55
|
+
# Authentication Code will be highlighted
|
56
|
+
@table.colorize :authentication_code, :with => :highlight_gray
|
57
57
|
# colorize booleans based on their values
|
58
58
|
@table.colorize :eletronic do |b|
|
59
59
|
b ? :blue : :cyan
|
60
60
|
end
|
61
61
|
# colorize the values based on comparison
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
value > 0 ? :green : :brown
|
67
|
-
end
|
68
|
-
end
|
62
|
+
red_to_negative = lambda { |value| :red if value < 0 }
|
63
|
+
green_to_positive = lambda { |value| :green if value > 0 }
|
64
|
+
brown_to_zero = lambda { |value| :brown if value == 0 }
|
65
|
+
colorizer = Yummi::Colorizer.join(red_to_negative, green_to_positive, brown_to_zero)
|
69
66
|
@table.colorize :value, :using => colorizer
|
70
67
|
@table.colorize :total, :using => colorizer
|
71
|
-
# colorize rows that Value is
|
68
|
+
# colorize rows that Value is greater than Total
|
72
69
|
@table.row_colorizer do |i, data|
|
73
70
|
:white if data[:value] > data[:total]
|
74
71
|
end
|
data/lib/yummi/logger.rb
CHANGED
@@ -26,19 +26,45 @@ module Yummi
|
|
26
26
|
|
27
27
|
module Formatter
|
28
28
|
|
29
|
+
#
|
30
|
+
# A colorful log formatter
|
31
|
+
#
|
29
32
|
class LogFormatter < Logger::Formatter
|
30
33
|
|
34
|
+
#
|
35
|
+
# Colors for each severity:
|
36
|
+
#
|
37
|
+
# * :debug
|
38
|
+
# * :info
|
39
|
+
# * :warn
|
40
|
+
# * :error
|
41
|
+
# * :fatal
|
42
|
+
# * :any
|
43
|
+
#
|
31
44
|
attr_accessor :colors
|
32
45
|
|
33
|
-
|
46
|
+
#
|
47
|
+
# Creates a new formatter with the colors (assuming Linux default terminal colors):
|
48
|
+
#
|
49
|
+
# * +DEBUG+: no color
|
50
|
+
# * +INFO+: green
|
51
|
+
# * +WARN+: brown
|
52
|
+
# * +ERROR+: red
|
53
|
+
# * +FATAL+: red (intense)
|
54
|
+
# * +ANY+: gray (intense)
|
55
|
+
#
|
56
|
+
# If a block is passed, it will be used to format the message
|
57
|
+
#
|
58
|
+
def initialize &block
|
34
59
|
@colors = {
|
35
60
|
:debug => nil,
|
36
61
|
:info => :green,
|
37
|
-
:warn => :
|
62
|
+
:warn => :brown,
|
38
63
|
:error => :red,
|
39
64
|
:fatal => :intense_red,
|
40
65
|
:any => :intense_gray
|
41
66
|
}
|
67
|
+
@format_block = block
|
42
68
|
end
|
43
69
|
|
44
70
|
alias_method :super_call, :call
|
@@ -48,7 +74,9 @@ module Yummi
|
|
48
74
|
Yummi::Color.colorize output(severity, time, program_name, message), color
|
49
75
|
end
|
50
76
|
|
77
|
+
# Formats the message, override this method instead of #call
|
51
78
|
def output severity, time, program_name, message
|
79
|
+
return @format_block.call severity, time, program_name, message if @format_block
|
52
80
|
super_call severity, time, program_name, message
|
53
81
|
end
|
54
82
|
|
data/lib/yummi/table.rb
CHANGED
@@ -21,11 +21,35 @@
|
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
23
|
module Yummi
|
24
|
-
|
24
|
+
# A Table that supports colorizing title, header, values and also formatting the values.
|
25
25
|
class Table
|
26
|
+
# The table data. It holds a two dimensional array.
|
27
|
+
attr_accessor :data
|
28
|
+
# The table title
|
29
|
+
attr_accessor :title
|
30
|
+
# Default align. #Yummi#Aligner should respond to it.
|
31
|
+
attr_accessor :default_align
|
32
|
+
# Aliases that can be used by formatters and colorizers instead of numeric indexes.
|
33
|
+
# The aliases are directed mapped to their respective index in this array
|
34
|
+
attr_accessor :aliases
|
35
|
+
# The table colspan
|
36
|
+
attr_accessor :colspan
|
37
|
+
# The table colors. This Map should have colors for the following elements:
|
38
|
+
#
|
39
|
+
# * Title: using :title key
|
40
|
+
# * Header: using :header key
|
41
|
+
# * Values: using :value key
|
42
|
+
#
|
43
|
+
# The colors must be supported by #Yummi#Color#parse or defined in #Yummi#Color#COLORS
|
44
|
+
attr_accessor :colors
|
26
45
|
|
27
|
-
|
28
|
-
|
46
|
+
# Creates a new table with the default attributes:
|
47
|
+
#
|
48
|
+
# * Title color: intense_yellow
|
49
|
+
# * Header color: intense_blue
|
50
|
+
# * Values color: none
|
51
|
+
# * Colspan: 2
|
52
|
+
# * Default Align: right and first element to left
|
29
53
|
def initialize
|
30
54
|
@data = []
|
31
55
|
@header = []
|
@@ -40,7 +64,7 @@ module Yummi
|
|
40
64
|
|
41
65
|
@aliases = []
|
42
66
|
|
43
|
-
@align = []
|
67
|
+
@align = [:left]
|
44
68
|
@formatters = []
|
45
69
|
@colorizers = []
|
46
70
|
@row_colorizer = nil
|
@@ -48,6 +72,7 @@ module Yummi
|
|
48
72
|
@default_align = :right
|
49
73
|
end
|
50
74
|
|
75
|
+
# Indicates that the table should not use colors.
|
51
76
|
def no_colors
|
52
77
|
@colors = {
|
53
78
|
:title => nil,
|
@@ -57,29 +82,157 @@ module Yummi
|
|
57
82
|
@no_colors = true
|
58
83
|
end
|
59
84
|
|
85
|
+
#
|
86
|
+
# === Description
|
87
|
+
#
|
88
|
+
# Sets the table header. If no aliases are defined, they will be defined as the texts
|
89
|
+
# in lowercase with line breaks and spaces replaced by underscores.
|
90
|
+
#
|
91
|
+
# Defining headers also limits the printed column to only columns that has a header
|
92
|
+
# (even if it is empty).
|
93
|
+
#
|
94
|
+
# === Args
|
95
|
+
#
|
96
|
+
# +header+::
|
97
|
+
# Array containing the texts for displaying the header. Line breaks are supported
|
98
|
+
#
|
99
|
+
# === Examples
|
100
|
+
#
|
101
|
+
# table.header = ['Name', 'Email', 'Work Phone', "Home\nPhone"]
|
102
|
+
#
|
103
|
+
# This will create the following aliases: :name, :email, :work_phone and :home_phone
|
104
|
+
#
|
60
105
|
def header= header
|
61
|
-
|
62
|
-
|
106
|
+
max = 0
|
107
|
+
header.each_index do |i|
|
108
|
+
max = [max, header[i].split("\n").size].max
|
109
|
+
end
|
110
|
+
@header = []
|
111
|
+
max.times { @header << [] }
|
112
|
+
header.each_index do |i|
|
113
|
+
names = header[i].split("\n")
|
114
|
+
names.each_index do |j|
|
115
|
+
@header[j][i] = names[j]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
@aliases = header.map { |n| n.downcase.gsub(' ', '_').gsub("\n", '_').to_sym } if @aliases.empty?
|
63
119
|
end
|
64
120
|
|
121
|
+
#
|
122
|
+
# === Description
|
123
|
+
#
|
124
|
+
# Sets the align for a column in the table. #Yummi#Aligner should respond to it.
|
125
|
+
#
|
126
|
+
# === Args
|
127
|
+
#
|
128
|
+
# +index+::
|
129
|
+
# The column index or its alias
|
130
|
+
# +type+::
|
131
|
+
# The alignment type
|
132
|
+
#
|
133
|
+
# === Example
|
134
|
+
#
|
135
|
+
# table.align :description, :left
|
136
|
+
# table.align :value, :right
|
137
|
+
#
|
65
138
|
def align index, type
|
66
139
|
index = parse_index(index)
|
67
140
|
@align[index] = type
|
68
141
|
end
|
69
142
|
|
143
|
+
#
|
144
|
+
# === Description
|
145
|
+
#
|
146
|
+
# Adds a component to colorize the entire row (overrides column color).
|
147
|
+
# The component must respond to +call+ with the index and the row as the arguments and
|
148
|
+
# return a color or +nil+ if default color should be used. A block can also be used.
|
149
|
+
#
|
150
|
+
# You can add as much colorizers as you want. The first color returned will be used.
|
151
|
+
#
|
152
|
+
# === Example
|
153
|
+
#
|
154
|
+
# table.row_colorizer { |i, row| :red if row[:value] < 0 }
|
155
|
+
#
|
70
156
|
def row_colorizer colorizer = nil, &block
|
71
157
|
@row_colorizer ||= Yummi::LinkedBlocks::new
|
72
158
|
@row_colorizer << (colorizer or block)
|
73
159
|
end
|
74
160
|
|
161
|
+
#
|
162
|
+
# === Description
|
163
|
+
#
|
164
|
+
# 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+
|
170
|
+
#
|
171
|
+
# === Example
|
172
|
+
#
|
173
|
+
# table.using_row.colorize(:value) { |row| :red if row[:value] < row[:average] }
|
174
|
+
#
|
175
|
+
#
|
176
|
+
def using_row
|
177
|
+
@using_row = true
|
178
|
+
self
|
179
|
+
end
|
180
|
+
|
181
|
+
#
|
182
|
+
# === Description
|
183
|
+
#
|
184
|
+
# Sets a component to colorize a column.
|
185
|
+
#
|
186
|
+
# The component must respond to +call+ with the column value (or row if used with
|
187
|
+
# #using_row) as the arguments and return a color or +nil+ if default color should be
|
188
|
+
# used. A block can also be used.
|
189
|
+
#
|
190
|
+
# You can add as much colorizers as you want. The first color returned will be used.
|
191
|
+
#
|
192
|
+
# === Args
|
193
|
+
#
|
194
|
+
# +index+::
|
195
|
+
# The column index or its alias
|
196
|
+
# +params+::
|
197
|
+
# A hash with params in case a block is not given:
|
198
|
+
# - :using defines the component to use
|
199
|
+
# - :with defines the color to use (to use the same color for all columns)
|
200
|
+
#
|
201
|
+
# === Example
|
202
|
+
#
|
203
|
+
# table.colorize :description, :with => :purple
|
204
|
+
# table.colorize(:value) { |value| :red if value < 0 }
|
205
|
+
#
|
75
206
|
def colorize index, params = {}, &block
|
76
207
|
index = parse_index(index)
|
77
|
-
@colorizers[index]
|
78
|
-
|
79
|
-
|
80
|
-
|
208
|
+
@colorizers[index] ||= []
|
209
|
+
obj = (params[:using] or block or (proc { |v| params[:with] }))
|
210
|
+
@colorizers[index] << {:use_row => @using_row, :component => obj}
|
211
|
+
@using_row = false
|
81
212
|
end
|
82
213
|
|
214
|
+
#
|
215
|
+
# === Description
|
216
|
+
#
|
217
|
+
# Sets a component to format a column.
|
218
|
+
#
|
219
|
+
# The component must respond to +call+ with the column value
|
220
|
+
# as the arguments and return a color or +nil+ if default color should be used.
|
221
|
+
# A block can also be used.
|
222
|
+
#
|
223
|
+
# === Args
|
224
|
+
#
|
225
|
+
# +index+::
|
226
|
+
# The column index or its alias
|
227
|
+
# +params+::
|
228
|
+
# A hash with params in case a block is not given:
|
229
|
+
# - :using defines the component to use
|
230
|
+
# - :with defines the format to use (to use the same format for all columns)
|
231
|
+
#
|
232
|
+
# === Example
|
233
|
+
#
|
234
|
+
# table.format :value, :with => '%.2f'
|
235
|
+
#
|
83
236
|
def format index, params = {}, &block
|
84
237
|
index = parse_index(index)
|
85
238
|
@formatters[index] = (params[:using] or block)
|
@@ -88,12 +241,23 @@ module Yummi
|
|
88
241
|
end
|
89
242
|
end
|
90
243
|
|
244
|
+
#
|
245
|
+
# Prints the #to_s into the given object.
|
246
|
+
#
|
91
247
|
def print to = $stdout
|
92
248
|
to.print to_s
|
93
249
|
end
|
94
250
|
|
251
|
+
#
|
252
|
+
# Return a colorized and formatted table.
|
253
|
+
#
|
95
254
|
def to_s
|
96
|
-
|
255
|
+
header_color_map, header_output = build_header_output
|
256
|
+
data_color_map, data_output = build_data_output
|
257
|
+
|
258
|
+
color_map = header_color_map + data_color_map
|
259
|
+
output = header_output + data_output
|
260
|
+
|
97
261
|
string = ""
|
98
262
|
string << Color.colorize(@title, @colors[:title]) << $/ if @title
|
99
263
|
output.each_index do |i|
|
@@ -106,28 +270,43 @@ module Yummi
|
|
106
270
|
value = Aligner.send align, column.to_s, width
|
107
271
|
value = Color.colorize value, color unless @no_colors
|
108
272
|
string << value
|
109
|
-
string << (
|
273
|
+
string << (' ' * @colspan)
|
110
274
|
end
|
111
275
|
string.strip! << $/
|
112
276
|
end
|
113
277
|
string
|
114
278
|
end
|
115
279
|
|
116
|
-
|
117
|
-
|
118
|
-
|
280
|
+
#
|
281
|
+
# Builds the header output for this table.
|
282
|
+
#
|
283
|
+
# Returns the color map and the header.
|
284
|
+
#
|
285
|
+
def build_header_output
|
119
286
|
color_map = []
|
120
287
|
output = []
|
121
288
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
289
|
+
@header.each do |line|
|
290
|
+
_colors = []
|
291
|
+
_data = []
|
292
|
+
line.each do |h|
|
293
|
+
_colors << @colors[:header]
|
294
|
+
_data << h
|
295
|
+
end
|
296
|
+
color_map << _colors
|
297
|
+
output << _data
|
128
298
|
end
|
129
|
-
color_map
|
130
|
-
|
299
|
+
[color_map, output]
|
300
|
+
end
|
301
|
+
|
302
|
+
#
|
303
|
+
# Builds the data output for this table.
|
304
|
+
#
|
305
|
+
# Returns the color map and the formatted data.
|
306
|
+
#
|
307
|
+
def build_data_output
|
308
|
+
color_map = []
|
309
|
+
output = []
|
131
310
|
|
132
311
|
@data.each_index do |row_index|
|
133
312
|
row = @data[row_index]
|
@@ -135,11 +314,21 @@ module Yummi
|
|
135
314
|
_data = []
|
136
315
|
|
137
316
|
row.each_index do |col_index|
|
138
|
-
next if @header and
|
317
|
+
next if @header and @header[0].size < col_index + 1
|
139
318
|
column = row[col_index]
|
140
|
-
|
141
|
-
|
142
|
-
|
319
|
+
colorizers = @colorizers[col_index]
|
320
|
+
if colorizers
|
321
|
+
colorizers.each do |colorizer|
|
322
|
+
arg = colorizer[:use_row] ? IndexedData::new(@aliases, row) : column
|
323
|
+
c = colorizer[:component].call(arg)
|
324
|
+
if c
|
325
|
+
_colors << c
|
326
|
+
break
|
327
|
+
end
|
328
|
+
end
|
329
|
+
else
|
330
|
+
_colors << @colors[:value]
|
331
|
+
end
|
143
332
|
formatter = @formatters[col_index]
|
144
333
|
_data << (formatter ? formatter.call(column) : column)
|
145
334
|
end
|
@@ -155,6 +344,8 @@ module Yummi
|
|
155
344
|
[color_map, output]
|
156
345
|
end
|
157
346
|
|
347
|
+
private
|
348
|
+
|
158
349
|
def parse_index(value)
|
159
350
|
return @aliases.index(value) unless value.is_a? Fixnum
|
160
351
|
value
|
data/lib/yummi/version.rb
CHANGED
data/lib/yummi.rb
CHANGED
@@ -25,7 +25,7 @@ require_relative "yummi/table"
|
|
25
25
|
require_relative "yummi/logger"
|
26
26
|
|
27
27
|
module Yummi
|
28
|
-
|
28
|
+
# Base for colorizing
|
29
29
|
module Color
|
30
30
|
# Colors from default linux terminal scheme
|
31
31
|
COLORS = {
|
@@ -58,14 +58,14 @@ module Yummi
|
|
58
58
|
:blink_cyan => '5;36',
|
59
59
|
:blink_gray => '5;37',
|
60
60
|
|
61
|
-
:highlight_black => '
|
62
|
-
:highlight_red => '
|
63
|
-
:highlight_green => '
|
64
|
-
:highlight_brown => '
|
65
|
-
:highlight_blue => '
|
66
|
-
:highlight_purple => '
|
67
|
-
:highlight_cyan => '
|
68
|
-
:highlight_gray => '
|
61
|
+
:highlight_black => '7;30',
|
62
|
+
:highlight_red => '7;31',
|
63
|
+
:highlight_green => '7;32',
|
64
|
+
:highlight_brown => '7;33',
|
65
|
+
:highlight_blue => '7;34',
|
66
|
+
:highlight_purple => '7;35',
|
67
|
+
:highlight_cyan => '7;36',
|
68
|
+
:highlight_gray => '7;37',
|
69
69
|
|
70
70
|
:intense_gray => '1;30',
|
71
71
|
:intense_red => '1;31',
|
@@ -107,7 +107,6 @@ module Yummi
|
|
107
107
|
col, nocol = [color, :nothing].map { |key| Color.escape(key) }
|
108
108
|
col ? "#{col}#{str}#{nocol}" : str
|
109
109
|
end
|
110
|
-
|
111
110
|
end
|
112
111
|
|
113
112
|
module Aligner
|
@@ -138,6 +137,43 @@ module Yummi
|
|
138
137
|
|
139
138
|
end
|
140
139
|
|
140
|
+
module Colorizer
|
141
|
+
|
142
|
+
def self.join *colorizers
|
143
|
+
join = Yummi::LinkedBlocks::new
|
144
|
+
colorizers.each { |c| join << c }
|
145
|
+
join
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.by_eval &block
|
149
|
+
EvalColorizer::new &block
|
150
|
+
end
|
151
|
+
|
152
|
+
class EvalColorizer
|
153
|
+
|
154
|
+
def initialize &block
|
155
|
+
@block = block
|
156
|
+
@colors = []
|
157
|
+
@eval_blocks = []
|
158
|
+
end
|
159
|
+
|
160
|
+
def use color, &eval_block
|
161
|
+
@colors << color
|
162
|
+
@eval_blocks << eval_block
|
163
|
+
end
|
164
|
+
|
165
|
+
def call *args
|
166
|
+
value = Yummi.call_block args.last, &@block # by convention, the last arg is data
|
167
|
+
@eval_blocks.each_index do |i|
|
168
|
+
return @colors[i] if @eval_blocks[i].call(value)
|
169
|
+
end
|
170
|
+
nil
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
141
177
|
module Formatter
|
142
178
|
|
143
179
|
def self.yes_or_no
|
@@ -146,6 +182,12 @@ module Yummi
|
|
146
182
|
end
|
147
183
|
end
|
148
184
|
|
185
|
+
def self.round precision
|
186
|
+
lambda do |value|
|
187
|
+
"%.#{precision}f" % value
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
149
191
|
def self.unit params
|
150
192
|
lambda do |value|
|
151
193
|
result = value
|
@@ -203,6 +245,14 @@ module Yummi
|
|
203
245
|
|
204
246
|
end
|
205
247
|
|
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
|
+
|
206
256
|
end
|
207
257
|
|
208
258
|
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.
|
4
|
+
version: 0.0.2
|
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-
|
12
|
+
date: 2012-04-10 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tool to colorize your console application.
|
15
15
|
email:
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- examples/list_files.rb
|
28
28
|
- examples/logger.rb
|
29
|
+
- examples/monitor_table.rb
|
29
30
|
- examples/table_cash_flow.rb
|
30
31
|
- lib/yummi.rb
|
31
32
|
- lib/yummi/logger.rb
|