yummi 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/cash_flow_table.rb +40 -28
- data/examples/list_files.rb +1 -1
- data/examples/monitor_table.rb +12 -2
- data/lib/yummi.rb +16 -5
- data/lib/yummi/table.rb +51 -22
- data/lib/yummi/version.rb +1 -1
- metadata +2 -2
data/examples/cash_flow_table.rb
CHANGED
@@ -30,6 +30,7 @@ opt = OptionParser::new
|
|
30
30
|
@table.header = ['Description', 'Value', 'Total', 'Eletronic', "Authentication\nCode"]
|
31
31
|
# sets the title
|
32
32
|
@table.title = 'Cash Flow'
|
33
|
+
@table.description = 'How you spend your money'
|
33
34
|
# formats booleans using Yes or No
|
34
35
|
@table.format :eletronic, :using => Yummi::Formatters.yes_or_no
|
35
36
|
# shows values without minus signal and rounded
|
@@ -45,45 +46,54 @@ opt = OptionParser::new
|
|
45
46
|
['Deposit', 50, 0.35, false, nil],
|
46
47
|
['Deposit', 600, 600.35, false, nil]]
|
47
48
|
|
49
|
+
def full_colors
|
50
|
+
@table.colorize :description, :with => :purple
|
51
|
+
# Authentication Code will be highlighted
|
52
|
+
@table.colorize :authentication_code, :with => :highlight_gray
|
53
|
+
# colorize booleans based on their values
|
54
|
+
@table.colorize :eletronic do |b|
|
55
|
+
b ? :blue : :cyan
|
56
|
+
end
|
57
|
+
# colorize the values based on comparison
|
58
|
+
red_to_negative = lambda { |value| :red if value < 0 }
|
59
|
+
green_to_positive = lambda { |value| :green if value > 0 }
|
60
|
+
brown_to_zero = lambda { |value| :brown if value == 0 }
|
61
|
+
@table.colorize [:value, :total], :using => Yummi::Colorizers.join(
|
62
|
+
red_to_negative, green_to_positive, brown_to_zero
|
63
|
+
)
|
64
|
+
# colorize rows that Value is greater than Total
|
65
|
+
@table.row_colorizer do |data| # or |data, index| if you need the index
|
66
|
+
:white if data[:value] > data[:total]
|
67
|
+
end
|
68
|
+
@table.colorize_null :with => :red
|
69
|
+
end
|
70
|
+
|
71
|
+
def zebra_colors
|
72
|
+
@table.row_colorizer Yummi::Colorizers.stripe :brown, :purple
|
73
|
+
end
|
74
|
+
|
75
|
+
def no_colors
|
76
|
+
@table.no_colors
|
77
|
+
end
|
78
|
+
|
48
79
|
opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
|
49
80
|
case type
|
50
81
|
when 'zebra'
|
51
|
-
|
82
|
+
zebra_colors
|
52
83
|
when 'full'
|
53
84
|
# colorize all values from the Description column to purple
|
54
|
-
|
55
|
-
# Authentication Code will be highlighted
|
56
|
-
@table.colorize :authentication_code, :with => :highlight_gray
|
57
|
-
# colorize booleans based on their values
|
58
|
-
@table.colorize :eletronic do |b|
|
59
|
-
b ? :blue : :cyan
|
60
|
-
end
|
61
|
-
# colorize the values based on comparison
|
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::Colorizers.join(red_to_negative, green_to_positive, brown_to_zero)
|
66
|
-
@table.colorize :value, :using => colorizer
|
67
|
-
@table.colorize :total, :using => colorizer
|
68
|
-
# colorize rows that Value is greater than Total
|
69
|
-
@table.row_colorizer do |data| # or |data, index| if you need the index
|
70
|
-
:white if data[:value] > data[:total]
|
71
|
-
end
|
72
|
-
@table.colorize_null :with => :red
|
85
|
+
full_colors
|
73
86
|
when 'none'
|
74
|
-
|
87
|
+
no_colors
|
75
88
|
else
|
76
89
|
end
|
90
|
+
@colors = type
|
77
91
|
end
|
92
|
+
|
78
93
|
opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
|
79
|
-
|
80
|
-
when 'horizontal'
|
81
|
-
@table.layout = :horizontal
|
82
|
-
when 'vertical'
|
83
|
-
@table.layout = :vertical
|
84
|
-
else
|
85
|
-
end
|
94
|
+
@table.layout = layout.to_sym
|
86
95
|
end
|
96
|
+
|
87
97
|
opt.on '--box', 'Prints the table inside a box' do
|
88
98
|
@box = Yummi::TextBox::new
|
89
99
|
end
|
@@ -94,6 +104,8 @@ end
|
|
94
104
|
|
95
105
|
opt.parse ARGV
|
96
106
|
|
107
|
+
full_colors unless @colors
|
108
|
+
|
97
109
|
if @box
|
98
110
|
@box << @table
|
99
111
|
@box.print
|
data/examples/list_files.rb
CHANGED
@@ -29,7 +29,7 @@ opt = OptionParser::new
|
|
29
29
|
|
30
30
|
@table = Yummi::Table::new
|
31
31
|
# setting the header sets the aliases automatically
|
32
|
-
@table.header =
|
32
|
+
@table.header = %w(Name Size)
|
33
33
|
@table.aliases << :directory
|
34
34
|
# sets the title
|
35
35
|
@table.title = 'File List'
|
data/examples/monitor_table.rb
CHANGED
@@ -92,10 +92,20 @@ opt.parse ARGV
|
|
92
92
|
['Server 4', 1_000_000, 200_000, 200, 50],
|
93
93
|
['Server 5', 1_000_000, 5_000, 200, 50],
|
94
94
|
['Server 6', 1_000_000, 750_000, 200, 50],
|
95
|
-
['Server 6', 1_000_000, 200_000, 200, 170],
|
96
|
-
['Server 6', 1_000_000, 5_000, 200, 180],
|
97
95
|
]
|
98
96
|
|
97
|
+
@table.add :server_name => 'Server 7',
|
98
|
+
:max_memory => 1_000_000,
|
99
|
+
:free_memory => 200_000,
|
100
|
+
:max_threads => 200,
|
101
|
+
:in_use_threads => 170
|
102
|
+
|
103
|
+
@table.add :server_name => 'Server 8',
|
104
|
+
:max_memory => 1_000_000,
|
105
|
+
:free_memory => 5_000,
|
106
|
+
:max_threads => 200,
|
107
|
+
:in_use_threads => 180
|
108
|
+
|
99
109
|
if @box
|
100
110
|
@box << @table
|
101
111
|
@box.print
|
data/lib/yummi.rb
CHANGED
@@ -95,18 +95,18 @@ module Yummi
|
|
95
95
|
# Escape the given text with the given color code
|
96
96
|
def self.escape key
|
97
97
|
return key unless key and COLORS[key.to_sym]
|
98
|
-
"\
|
98
|
+
"\e[#{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, "\e[0;0m"].map { |key| Color.escape(key) }
|
104
104
|
color ? "#{color}#{string}#{end_color}" : string
|
105
105
|
end
|
106
106
|
|
107
107
|
# Extracts the text from a colorized string
|
108
108
|
def self.raw string
|
109
|
-
string.gsub(/\
|
109
|
+
string.gsub(/\e\[\d;\d{2}m/, '').gsub(/\e\[0;0m/, '')
|
110
110
|
end
|
111
111
|
|
112
112
|
end
|
@@ -220,8 +220,19 @@ module Yummi
|
|
220
220
|
# for getting the text to colorize).
|
221
221
|
#
|
222
222
|
def colorize (*args)
|
223
|
-
|
224
|
-
|
223
|
+
Yummi.colorize args.first.to_s, color_for(args)
|
224
|
+
end
|
225
|
+
|
226
|
+
#
|
227
|
+
# Returns the color for the given value
|
228
|
+
#
|
229
|
+
# === Args
|
230
|
+
#
|
231
|
+
# An array of arguments that will be passed to :call: method to get the color. By
|
232
|
+
# convention, the first argument must be the object to colorize (to_s is called on it
|
233
|
+
# for getting the text to colorize).#
|
234
|
+
def color_for (*args)
|
235
|
+
call *args
|
225
236
|
end
|
226
237
|
|
227
238
|
end
|
data/lib/yummi/table.rb
CHANGED
@@ -27,6 +27,8 @@ module Yummi
|
|
27
27
|
attr_accessor :data
|
28
28
|
# The table title
|
29
29
|
attr_accessor :title
|
30
|
+
# The table description
|
31
|
+
attr_accessor :description
|
30
32
|
# Default align. #Yummi#Aligner should respond to it.
|
31
33
|
attr_accessor :default_align
|
32
34
|
# Aliases that can be used by formatters and colorizers instead of numeric indexes.
|
@@ -56,8 +58,10 @@ module Yummi
|
|
56
58
|
@data = []
|
57
59
|
@header = []
|
58
60
|
@title = nil
|
61
|
+
@description = nil
|
59
62
|
@colors = {
|
60
63
|
:title => :intense_yellow,
|
64
|
+
:description => :intense_gray,
|
61
65
|
:header => :intense_blue,
|
62
66
|
:value => nil
|
63
67
|
}
|
@@ -128,18 +132,20 @@ module Yummi
|
|
128
132
|
# === Args
|
129
133
|
#
|
130
134
|
# +index+::
|
131
|
-
# The column
|
135
|
+
# The column indexes or its aliases
|
132
136
|
# +type+::
|
133
137
|
# The alignment type
|
134
138
|
#
|
135
139
|
# === Example
|
136
140
|
#
|
137
141
|
# table.align :description, :left
|
138
|
-
# table.align :value, :right
|
142
|
+
# table.align [:value, :total], :right
|
139
143
|
#
|
140
|
-
def align (
|
141
|
-
|
142
|
-
|
144
|
+
def align (indexes, type)
|
145
|
+
[*indexes].each do |index|
|
146
|
+
index = parse_index(index)
|
147
|
+
@align[index] = type
|
148
|
+
end
|
143
149
|
end
|
144
150
|
|
145
151
|
#
|
@@ -175,6 +181,23 @@ module Yummi
|
|
175
181
|
@using_row = false
|
176
182
|
end
|
177
183
|
|
184
|
+
#
|
185
|
+
# Adds the given data as a row. If the argument is a hash, its keys will be used
|
186
|
+
# to match header alias for building the row data.
|
187
|
+
#
|
188
|
+
def << (row)
|
189
|
+
if row.is_a? Hash
|
190
|
+
array = []
|
191
|
+
aliases.each do |header_alias|
|
192
|
+
array << row[header_alias]
|
193
|
+
end
|
194
|
+
row = array
|
195
|
+
end
|
196
|
+
@data << row
|
197
|
+
end
|
198
|
+
|
199
|
+
alias_method :add, :<<
|
200
|
+
|
178
201
|
#
|
179
202
|
# Sets a component to colorize a column.
|
180
203
|
#
|
@@ -186,8 +209,8 @@ module Yummi
|
|
186
209
|
#
|
187
210
|
# === Args
|
188
211
|
#
|
189
|
-
# +
|
190
|
-
# The column
|
212
|
+
# +indexes+::
|
213
|
+
# The column indexes or its aliases
|
191
214
|
# +params+::
|
192
215
|
# A hash with params in case a block is not given:
|
193
216
|
# - :using defines the component to use
|
@@ -196,13 +219,15 @@ module Yummi
|
|
196
219
|
# === Example
|
197
220
|
#
|
198
221
|
# table.colorize :description, :with => :purple
|
199
|
-
# table.colorize(:value) { |value| :red if value < 0 }
|
200
|
-
#
|
201
|
-
def colorize (
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
222
|
+
# table.colorize([:value, :total]) { |value| :red if value < 0 }
|
223
|
+
#
|
224
|
+
def colorize (indexes, params = {}, &block)
|
225
|
+
[*indexes].each do |index|
|
226
|
+
index = parse_index(index)
|
227
|
+
@colorizers[index] ||= []
|
228
|
+
obj = (params[:using] or block or (proc { |v| params[:with] }))
|
229
|
+
@colorizers[index] << {:use_row => @using_row, :component => obj}
|
230
|
+
end
|
206
231
|
end
|
207
232
|
|
208
233
|
#
|
@@ -231,8 +256,8 @@ module Yummi
|
|
231
256
|
#
|
232
257
|
# === Args
|
233
258
|
#
|
234
|
-
# +
|
235
|
-
# The column
|
259
|
+
# +indexes+::
|
260
|
+
# The column indexes or its aliases
|
236
261
|
# +params+::
|
237
262
|
# A hash with params in case a block is not given:
|
238
263
|
# - :using defines the component to use
|
@@ -241,12 +266,15 @@ module Yummi
|
|
241
266
|
# === Example
|
242
267
|
#
|
243
268
|
# table.format :value, :with => '%.2f'
|
244
|
-
#
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
params[:
|
269
|
+
# table.format [:value, :total], :with => '%.2f'
|
270
|
+
#
|
271
|
+
def format (indexes, params = {}, &block)
|
272
|
+
[*indexes].each do |index|
|
273
|
+
index = parse_index(index)
|
274
|
+
@formatters[index] = (params[:using] or block)
|
275
|
+
@formatters[index] ||= proc do |value|
|
276
|
+
params[:with] % value
|
277
|
+
end
|
250
278
|
end
|
251
279
|
end
|
252
280
|
|
@@ -283,6 +311,7 @@ module Yummi
|
|
283
311
|
|
284
312
|
string = ""
|
285
313
|
string << Color.colorize(@title, @colors[:title]) << $/ if @title
|
314
|
+
string << Color.colorize(@description, @colors[:description]) << $/ if @description
|
286
315
|
table_data = header_output + data_output
|
287
316
|
if @layout == :vertical
|
288
317
|
# don't use array transpose because the data may differ in each line size
|
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.
|
4
|
+
version: 0.4.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-07-
|
12
|
+
date: 2012-07-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tool to colorize your console application.
|
15
15
|
email:
|