terminal-table 1.4.1 → 1.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +5 -0
- data/examples/examples.rb +7 -11
- data/lib/terminal-table/table.rb +84 -12
- data/lib/terminal-table/version.rb +1 -1
- data/spec/table_spec.rb +131 -23
- data/terminal-table.gemspec +2 -2
- metadata +2 -2
data/History.rdoc
CHANGED
data/examples/examples.rb
CHANGED
@@ -57,15 +57,11 @@ rows << ['Ruby', 70]
|
|
57
57
|
rows << ['JavaScript', 30]
|
58
58
|
puts table(nil, *rows)
|
59
59
|
|
60
|
-
|
61
|
-
$VERBOSE = nil
|
62
|
-
Terminal::Table::X = '='
|
63
|
-
Terminal::Table::Y = '!'
|
64
|
-
Terminal::Table::I = '*'
|
65
|
-
|
66
60
|
rows = []
|
67
|
-
rows << [
|
68
|
-
rows << [
|
69
|
-
rows << [
|
70
|
-
|
71
|
-
|
61
|
+
rows << ['Lines', 100]
|
62
|
+
rows << ['Comments', 20]
|
63
|
+
rows << ['Ruby', 70]
|
64
|
+
rows << ['JavaScript', 30]
|
65
|
+
table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
|
66
|
+
table.align_column 1, :right
|
67
|
+
puts table
|
data/lib/terminal-table/table.rb
CHANGED
@@ -75,7 +75,7 @@ module Terminal
|
|
75
75
|
separator
|
76
76
|
else
|
77
77
|
Y + row.map_with_index do |cell, i|
|
78
|
-
render_cell(cell, i)
|
78
|
+
render_cell(cell, row_to_index(row, i))
|
79
79
|
end.join(Y) + Y
|
80
80
|
end
|
81
81
|
end
|
@@ -138,7 +138,38 @@ module Terminal
|
|
138
138
|
# Return _n_ column including headings.
|
139
139
|
|
140
140
|
def column_with_headings n
|
141
|
-
headings_with_rows.map { |row| row[n] }.compact
|
141
|
+
headings_with_rows.map { |row| row_with_hash(row)[n] }.compact
|
142
|
+
end
|
143
|
+
|
144
|
+
def row_with_hash row
|
145
|
+
# this method duplicates the multi-column columns in each column they are in
|
146
|
+
index = 0
|
147
|
+
row.inject [] do |columns, column|
|
148
|
+
if column.is_a?(Hash) && column[:colspan] && column[:colspan] > 1
|
149
|
+
column[:start_index] = index
|
150
|
+
column[:colspan].times do
|
151
|
+
columns << column
|
152
|
+
index += 1
|
153
|
+
end
|
154
|
+
else
|
155
|
+
columns << column
|
156
|
+
index += 1
|
157
|
+
end
|
158
|
+
columns
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def row_to_index row, index
|
163
|
+
new_index = -1
|
164
|
+
0.upto(index) do |i|
|
165
|
+
column = row[i]
|
166
|
+
if column.is_a?(Hash) && column[:colspan] && column[:colspan] > 1 && index != i
|
167
|
+
new_index = new_index + column[:colspan]
|
168
|
+
else
|
169
|
+
new_index += 1
|
170
|
+
end
|
171
|
+
end
|
172
|
+
return new_index
|
142
173
|
end
|
143
174
|
|
144
175
|
##
|
@@ -149,22 +180,63 @@ module Terminal
|
|
149
180
|
end
|
150
181
|
|
151
182
|
##
|
152
|
-
# Return the largest cell found within column _n_.
|
153
|
-
|
154
|
-
def
|
155
|
-
column_with_headings(n).
|
156
|
-
|
157
|
-
|
183
|
+
# Return the length of the largest cell found within column _n_.
|
184
|
+
|
185
|
+
def length_of_largest_cell_in_column n
|
186
|
+
column_with_headings(n).map do |cell|
|
187
|
+
if cell.is_a? Hash
|
188
|
+
if cell[:colspan] && cell[:colspan] > 1
|
189
|
+
if (cell[:value].to_s.length <= length_of_single_columns_where_multicolumn_is(cell[:start_index],cell[:colspan]))
|
190
|
+
0
|
191
|
+
else
|
192
|
+
spacing_length = (3 * (cell[:colspan] - 1))
|
193
|
+
length_in_columns = (cell[:value].to_s.length - spacing_length)
|
194
|
+
(length_in_columns.to_f / cell[:colspan]).ceil
|
195
|
+
end
|
196
|
+
else
|
197
|
+
cell[:value].to_s.length
|
198
|
+
end
|
199
|
+
else
|
200
|
+
cell.to_s.length
|
201
|
+
end
|
202
|
+
end.sort.last
|
203
|
+
end
|
204
|
+
|
205
|
+
##
|
206
|
+
# Returns the length of the largest single column cell found within column _n_.
|
207
|
+
|
208
|
+
def length_of_largest_single_column_cell_in_column n
|
209
|
+
column_with_headings(n).map do |cell|
|
210
|
+
if cell.is_a? Hash
|
211
|
+
if cell[:colspan] && cell[:colspan] > 1
|
212
|
+
0
|
213
|
+
else
|
214
|
+
cell[:value].to_s.length
|
215
|
+
end
|
216
|
+
else
|
217
|
+
cell.to_s.length
|
218
|
+
end
|
219
|
+
end.sort.last
|
220
|
+
end
|
221
|
+
|
222
|
+
##
|
223
|
+
# Returns the length of the single columns starting from _n_ and going through _length_ columns.
|
224
|
+
|
225
|
+
def length_of_single_columns_where_multicolumn_is(n,length)
|
226
|
+
total_length = 0
|
227
|
+
spacing_length = (3 * (length - 1))
|
228
|
+
total_length = total_length + spacing_length
|
229
|
+
n.upto(n + length - 1) do |i|
|
230
|
+
total_length = total_length + length_of_largest_single_column_cell_in_column(i)
|
231
|
+
end
|
232
|
+
return total_length
|
158
233
|
end
|
159
234
|
|
160
235
|
##
|
161
236
|
# Return length of column _n_.
|
162
237
|
|
163
238
|
def length_of_column n
|
164
|
-
|
165
|
-
Hash === largest_cell ?
|
166
|
-
largest_cell[:value].to_s.length :
|
167
|
-
largest_cell.to_s.length
|
239
|
+
length_of_largest_cell_in_column n
|
168
240
|
end
|
169
241
|
|
170
242
|
##
|
data/spec/table_spec.rb
CHANGED
@@ -13,6 +13,43 @@ module Terminal
|
|
13
13
|
@table.column(1).should == ['bar', 'big foo bar']
|
14
14
|
end
|
15
15
|
|
16
|
+
it "should select the column with headings at an index" do
|
17
|
+
@table << [1,2,3]
|
18
|
+
@table << [4,5,6]
|
19
|
+
@table.column_with_headings(2).should == [3,6]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should select the columns with colspans > 1 in the index" do
|
23
|
+
@table << [1,{:value => 2, :colspan => 2}]
|
24
|
+
@table << [{:value => 3, :colspan => 2}, 4]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should account for the colspan when selecting columns" do
|
28
|
+
@table << [1,2,3]
|
29
|
+
@table << [{:value => "4,5", :colspan => 2}, 6]
|
30
|
+
@table.column_with_headings(0).should == [1,{:start_index => 0, :value => "4,5", :colspan => 2}]
|
31
|
+
@table.column_with_headings(1).should == [2,{:start_index => 0, :value => "4,5", :colspan => 2}]
|
32
|
+
@table.column_with_headings(2).should == [3,6]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should render tables with colspan properly" do
|
36
|
+
@table << [1,2,3]
|
37
|
+
@table << [4,5,6]
|
38
|
+
@table << [{:value => "7", :colspan => 2}, 88]
|
39
|
+
@table.render.should == <<-EOF.deindent
|
40
|
+
+---+---+----+
|
41
|
+
| 1 | 2 | 3 |
|
42
|
+
| 4 | 5 | 6 |
|
43
|
+
| 7 | 88 |
|
44
|
+
+---+---+----+
|
45
|
+
EOF
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should convert rows to indexes" do
|
49
|
+
@table << [{:value => '7', :colspan => 2}, 88]
|
50
|
+
@table.row_to_index(@table.rows[0], 1).should == 2
|
51
|
+
end
|
52
|
+
|
16
53
|
it "should count columns" do
|
17
54
|
@table << [1, 2, 3]
|
18
55
|
@table.number_of_columns.should == 3
|
@@ -37,10 +74,42 @@ module Terminal
|
|
37
74
|
@table.column(0).should == [{ :value => 'a', :align => :left }, 'b', 'c']
|
38
75
|
end
|
39
76
|
|
40
|
-
it "should
|
77
|
+
it "should determine length of largest cell in a column" do
|
41
78
|
@table << ['foo', 'bar']
|
42
79
|
@table << ['big foo', 'big foo bar']
|
43
|
-
@table.
|
80
|
+
@table.length_of_largest_cell_in_column(1).should == 11
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should determine length of largest cell in a column with multi-column rows" do
|
84
|
+
@table << [1,2]
|
85
|
+
@table << [{:value => '123456789', :colspan => 2}]
|
86
|
+
# +-----+-----+
|
87
|
+
# | 1 | 2 |
|
88
|
+
# | 123456789 |
|
89
|
+
#
|
90
|
+
@table.length_of_largest_cell_in_column(0).should == 3
|
91
|
+
@table.length_of_largest_cell_in_column(1).should == 3
|
92
|
+
|
93
|
+
@table.render.should == <<-EOF.deindent
|
94
|
+
+-----+-----+
|
95
|
+
| 1 | 2 |
|
96
|
+
| 123456789 |
|
97
|
+
+-----+-----+
|
98
|
+
EOF
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should determine length of largest cell in a column with multi-column rows, rounding up" do
|
102
|
+
@table << [1,2]
|
103
|
+
@table << [{:value => '1234567890', :colspan => 2}]
|
104
|
+
@table.length_of_largest_cell_in_column(0).should == 4
|
105
|
+
@table.length_of_largest_cell_in_column(1).should == 4
|
106
|
+
|
107
|
+
@table.render.should == <<-EOF.deindent
|
108
|
+
+------+------+
|
109
|
+
| 1 | 2 |
|
110
|
+
| 1234567890 |
|
111
|
+
+------+------+
|
112
|
+
EOF
|
44
113
|
end
|
45
114
|
|
46
115
|
it "should find column length" do
|
@@ -285,27 +354,66 @@ module Terminal
|
|
285
354
|
@table.headings = ['one', { :value => 'two', :alignment => :center, :colspan => 2}]
|
286
355
|
@table.rows = [['a', 1, 2], ['b', 3, 4], ['c', 3, 4]]
|
287
356
|
@table.render.should == <<-EOF.deindent
|
288
|
-
|
289
|
-
| one |
|
290
|
-
|
291
|
-
| a | 1
|
292
|
-
| b | 3
|
293
|
-
| c | 3
|
294
|
-
|
357
|
+
+-----+---+---+
|
358
|
+
| one | two |
|
359
|
+
+-----+---+---+
|
360
|
+
| a | 1 | 2 |
|
361
|
+
| b | 3 | 4 |
|
362
|
+
| c | 3 | 4 |
|
363
|
+
+-----+---+---+
|
295
364
|
EOF
|
296
365
|
end
|
297
366
|
|
298
367
|
it "should handle colspan inside cells" do
|
368
|
+
@table.headings = ['one', 'two', 'three']
|
369
|
+
@table.rows = [['a', 1, 2], ['b', 3, 4], [{:value => "joined", :colspan => 2,:alignment => :center}, 'c']]
|
370
|
+
@table.render.should == <<-EOF.deindent
|
371
|
+
+-----+-----+-------+
|
372
|
+
| one | two | three |
|
373
|
+
+-----+-----+-------+
|
374
|
+
| a | 1 | 2 |
|
375
|
+
| b | 3 | 4 |
|
376
|
+
| joined | c |
|
377
|
+
+-----+-----+-------+
|
378
|
+
EOF
|
379
|
+
end
|
380
|
+
|
381
|
+
it "should handle colspan inside cells regardless of colspan position" do
|
299
382
|
@table.headings = ['one', 'two', 'three']
|
300
383
|
@table.rows = [['a', 1, 2], ['b', 3, 4], ['c', {:value => "joined", :colspan => 2,:alignment => :center}]]
|
301
384
|
@table.render.should == <<-EOF.deindent
|
302
|
-
|
303
|
-
| one | two
|
304
|
-
|
305
|
-
| a | 1
|
306
|
-
| b | 3
|
307
|
-
| c |
|
308
|
-
|
385
|
+
+-----+-----+-------+
|
386
|
+
| one | two | three |
|
387
|
+
+-----+-----+-------+
|
388
|
+
| a | 1 | 2 |
|
389
|
+
| b | 3 | 4 |
|
390
|
+
| c | joined |
|
391
|
+
+-----+-----+-------+
|
392
|
+
EOF
|
393
|
+
end
|
394
|
+
|
395
|
+
it "should handle overflowing colspans" do
|
396
|
+
@table.headings = ['one', 'two', 'three']
|
397
|
+
@table.rows = [['a', 1, 2], ['b', 3, 4], ['c', {:value => "joined that is very very long", :colspan => 2,:alignment => :center}]]
|
398
|
+
@table.render.should == <<-EOF.deindent
|
399
|
+
+-----+---------------+---------------+
|
400
|
+
| one | two | three |
|
401
|
+
+-----+---------------+---------------+
|
402
|
+
| a | 1 | 2 |
|
403
|
+
| b | 3 | 4 |
|
404
|
+
| c | joined that is very very long |
|
405
|
+
+-----+---------------+---------------+
|
406
|
+
EOF
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should only increase column size for multi-column if it is unavoidable" do
|
410
|
+
@table << [12345,2,3]
|
411
|
+
@table << [{:value => 123456789, :colspan => 2}, 4]
|
412
|
+
@table.render.should == <<-EOF.deindent
|
413
|
+
+-------+---+---+
|
414
|
+
| 12345 | 2 | 3 |
|
415
|
+
| 123456789 | 4 |
|
416
|
+
+-------+---+---+
|
309
417
|
EOF
|
310
418
|
end
|
311
419
|
|
@@ -330,13 +438,13 @@ module Terminal
|
|
330
438
|
@table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
|
331
439
|
|
332
440
|
@table.render.should == <<-EOF.deindent
|
333
|
-
|
334
|
-
| name | values
|
335
|
-
|
336
|
-
| a | 1
|
337
|
-
| b | 4
|
338
|
-
| c | 7
|
339
|
-
|
441
|
+
+------+---+---+---+
|
442
|
+
| name | values |
|
443
|
+
+------+---+---+---+
|
444
|
+
| a | 1 | 2 | 3 |
|
445
|
+
| b | 4 | 5 | 6 |
|
446
|
+
| c | 7 | 8 | 9 |
|
447
|
+
+------+---+---+---+
|
340
448
|
EOF
|
341
449
|
end
|
342
450
|
end
|
data/terminal-table.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{terminal-table}
|
5
|
-
s.version = "1.4.
|
5
|
+
s.version = "1.4.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["TJ Holowaychuk"]
|
9
|
-
s.date = %q{
|
9
|
+
s.date = %q{2010-01-14}
|
10
10
|
s.description = %q{Simple, feature rich ascii table generation library}
|
11
11
|
s.email = %q{tj@vision-media.ca}
|
12
12
|
s.extra_rdoc_files = ["README.rdoc", "lib/terminal-table.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/table_helper.rb", "lib/terminal-table/version.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terminal-table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Holowaychuk
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-14 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|