text-table 1.2.2 → 1.2.3
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.
- checksums.yaml +7 -0
- data/.document +2 -3
- data/.gitignore +6 -24
- data/CHANGELOG.rdoc +6 -2
- data/Gemfile +3 -0
- data/README.rdoc +33 -35
- data/lib/text-table/table.rb +1 -0
- data/lib/text-table/version.rb +5 -0
- data/spec/cell_spec.rb +1 -1
- data/spec/enumerable_spec.rb +15 -15
- data/spec/integration/alignment_spec.rb +183 -0
- data/spec/integration/boundaries_spec.rb +113 -0
- data/spec/integration/column_spans_spec.rb +226 -0
- data/spec/integration/parts_spec.rb +68 -0
- data/spec/integration/performance_spec.rb +19 -0
- data/spec/integration_helper.rb +13 -0
- data/spec/spec_helper.rb +4 -10
- data/text-table.gemspec +19 -65
- metadata +57 -71
- data/Rakefile +0 -46
- data/VERSION +0 -1
- data/spec/spec.opts +0 -2
- data/spec/table_spec.rb +0 -491
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5637e2fb2a77c1c4c6f019b4120e6d1dfc547336
|
4
|
+
data.tar.gz: da83a6185a3c4acac5e857594dd187fe43544b76
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: da85599487daf6768f1a466e20950418cc65905782a22972a27e0b59c70b4d63df5ab41c945a59e378f065b7c224026a9d712e4b18b174523c0276d0bb67307a
|
7
|
+
data.tar.gz: 359c082e3f514a9b706ff5a82a1df813ab82c89ee0802c1883f8630eb8a87eff4e70aa400bfc79936c9bf36ae3b0d8c81e81b5d2585930c7dfbdb769c46bae50
|
data/.document
CHANGED
data/.gitignore
CHANGED
@@ -1,24 +1,6 @@
|
|
1
|
-
|
2
|
-
.
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
## EMACS
|
9
|
-
*~
|
10
|
-
\#*
|
11
|
-
.\#*
|
12
|
-
|
13
|
-
## VIM
|
14
|
-
*.swp
|
15
|
-
|
16
|
-
## NETBEANS
|
17
|
-
nbproject
|
18
|
-
|
19
|
-
## PROJECT::GENERAL
|
20
|
-
coverage
|
21
|
-
rdoc
|
22
|
-
pkg
|
23
|
-
|
24
|
-
## PROJECT::SPECIFIC
|
1
|
+
bin/
|
2
|
+
.bundle/
|
3
|
+
doc/
|
4
|
+
Gemfile.lock
|
5
|
+
tmp/
|
6
|
+
vendor/bundle/
|
data/CHANGELOG.rdoc
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
=== 1.2.3 (2013-04-15)
|
2
|
+
|
3
|
+
* Fixed performance issue with large number of rows (thanks Kaoru Maeda)
|
4
|
+
|
1
5
|
=== 1.2.2 (2010-03-29)
|
2
6
|
|
3
|
-
*
|
7
|
+
* Fixed Ruby 1.9 warnings on shadowed outer local variables (thanks Claudio Bustos)
|
4
8
|
|
5
9
|
=== 1.2.1 (2010-01-05)
|
6
10
|
|
@@ -18,4 +22,4 @@
|
|
18
22
|
|
19
23
|
=== 1.0.1 (2009-12-21)
|
20
24
|
|
21
|
-
* Initial stable release.
|
25
|
+
* Initial stable release.
|
data/Gemfile
ADDED
data/README.rdoc
CHANGED
@@ -17,13 +17,33 @@ Text::Table is compatible with Ruby 1.8.6, 1.8.7 and 1.9.1 and includes a compre
|
|
17
17
|
gem install text-table
|
18
18
|
|
19
19
|
|
20
|
+
== Examples
|
21
|
+
|
22
|
+
require 'text-table'
|
23
|
+
|
24
|
+
table = Text::Table.new
|
25
|
+
table.head = ['A', 'B']
|
26
|
+
table.rows = [['a1', 'b1']]
|
27
|
+
table.rows << ['a2', 'b2']
|
28
|
+
|
29
|
+
table.to_s
|
30
|
+
|
31
|
+
# +----+----+
|
32
|
+
# | A | B |
|
33
|
+
# +----+----+
|
34
|
+
# | a1 | b1 |
|
35
|
+
# | a2 | b2 |
|
36
|
+
# +----+----+
|
37
|
+
|
38
|
+
You can also pass an options hash when instantiating a Text::Table:
|
39
|
+
|
40
|
+
table = Text::Table.new(:head => ['A', 'B'], :rows => [['a1', 'b1'], ['a2', 'b2']])
|
41
|
+
|
42
|
+
|
20
43
|
== Calling the <tt>to_table</tt> Method
|
21
44
|
|
22
45
|
Just call the <tt>to_table</tt> method (or <tt>to_text_table</tt> if the former is already defined) on Arrays (and other Enumerables).
|
23
46
|
|
24
|
-
require 'rubygems'
|
25
|
-
require 'text-table'
|
26
|
-
|
27
47
|
array = [
|
28
48
|
['Student', 'Mid-Terms', 'Finals'],
|
29
49
|
['Sam', 94, 93],
|
@@ -66,30 +86,6 @@ You could also specify that the last row is the table footer.
|
|
66
86
|
# +---------+-----------+--------+
|
67
87
|
|
68
88
|
|
69
|
-
== Creating a New Text::Table Object
|
70
|
-
|
71
|
-
You could create a Text::Table object by passing an options hash:
|
72
|
-
|
73
|
-
table = Text::Table.new(:head => ['A', 'B'], :rows => [['a1', 'b1'], ['a2', 'b2']])
|
74
|
-
|
75
|
-
Or by passing a block:
|
76
|
-
|
77
|
-
table = Text::Table.new do |t|
|
78
|
-
t.head = ['A', 'B']
|
79
|
-
t.rows = [['a1', 'b1']]
|
80
|
-
t.rows << ['a2', 'b2']
|
81
|
-
end
|
82
|
-
|
83
|
-
table.to_s
|
84
|
-
|
85
|
-
# +----+----+
|
86
|
-
# | A | B |
|
87
|
-
# +----+----+
|
88
|
-
# | a1 | b1 |
|
89
|
-
# | a2 | b2 |
|
90
|
-
# +----+----+
|
91
|
-
|
92
|
-
|
93
89
|
== Aligning Cells and Spanning Columns
|
94
90
|
|
95
91
|
Alignment and column span can be specified by passing a cell as a Hash object.
|
@@ -98,13 +94,12 @@ The acceptable aligments are <tt>:left</tt>, <tt>:center</tt> and <tt>:right</tt
|
|
98
94
|
|
99
95
|
Cells and footers are aligned to the left by default, while headers are centered by default.
|
100
96
|
|
101
|
-
table = Text::Table.new
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
end
|
97
|
+
table = Text::Table.new
|
98
|
+
table.head = ['Heading A', 'Heading B']
|
99
|
+
table.rows << ['a1', 'b1']
|
100
|
+
table.rows << ['a2', {:value => 'b2', :align => :right}]
|
101
|
+
table.rows << ['a3', 'b3']
|
102
|
+
table.rows << [{:value => 'a4', :colspan => 2, :align => :center}]
|
108
103
|
|
109
104
|
puts table
|
110
105
|
|
@@ -187,9 +182,12 @@ Thanks to the authors and contributors of these projects.
|
|
187
182
|
|
188
183
|
== Contributors
|
189
184
|
|
190
|
-
* Claudio Bustos (clbustos[
|
185
|
+
* Claudio Bustos (clbustos[http://github.com/clbustos])
|
191
186
|
* Fix Ruby 1.9 warnings on shadowed outer local variables
|
192
187
|
|
188
|
+
* Kaoru Maeda (mad-p[https://github.com/mad-p])
|
189
|
+
* Fixed performance issue with large number of rows
|
190
|
+
|
193
191
|
|
194
192
|
== Copyright
|
195
193
|
|
data/lib/text-table/table.rb
CHANGED
@@ -151,6 +151,7 @@ module Text #:nodoc:
|
|
151
151
|
end
|
152
152
|
|
153
153
|
def column_widths #:nodoc:
|
154
|
+
@column_widths ||= \
|
154
155
|
all_text_table_rows.reject {|row| row.cells == :separator}.map do |row|
|
155
156
|
row.cells.map {|cell| [(cell.value.length/cell.colspan.to_f).ceil] * cell.colspan}.flatten
|
156
157
|
end.transpose.map(&:max)
|
data/spec/cell_spec.rb
CHANGED
data/spec/enumerable_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Enumerable do
|
4
4
|
describe Array do
|
@@ -11,45 +11,45 @@ describe Enumerable do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "to Text::Table" do
|
14
|
-
@arr.to_text_table.to_s.should ==
|
14
|
+
@arr.to_text_table.to_s.should == deindent(%q{
|
15
15
|
+----+-----+------+
|
16
16
|
| 11 | 2 | 3333 |
|
17
17
|
| 44 | 56 | 6 |
|
18
18
|
| 7 | 888 | 99 |
|
19
19
|
+----+-----+------+
|
20
|
-
|
20
|
+
})
|
21
21
|
end
|
22
22
|
|
23
23
|
it "to Text::Table using flat array" do
|
24
|
-
[11, 44, 7].to_text_table.to_s.should ==
|
24
|
+
[11, 44, 7].to_text_table.to_s.should == deindent(%q{
|
25
25
|
+----+
|
26
26
|
| 11 |
|
27
27
|
| 44 |
|
28
28
|
| 7 |
|
29
29
|
+----+
|
30
|
-
|
30
|
+
})
|
31
31
|
end
|
32
32
|
|
33
33
|
it "to Text::Table, setting first row as head" do
|
34
|
-
@arr.to_text_table(:first_row_is_head => true).to_s.should ==
|
34
|
+
@arr.to_text_table(:first_row_is_head => true).to_s.should == deindent(%q{
|
35
35
|
+----+-----+------+
|
36
36
|
| 11 | 2 | 3333 |
|
37
37
|
+----+-----+------+
|
38
38
|
| 44 | 56 | 6 |
|
39
39
|
| 7 | 888 | 99 |
|
40
40
|
+----+-----+------+
|
41
|
-
|
41
|
+
})
|
42
42
|
end
|
43
43
|
|
44
44
|
it "to Text::Table, setting last row as foot" do
|
45
|
-
@arr.to_text_table(:last_row_is_foot => true).to_s.should ==
|
45
|
+
@arr.to_text_table(:last_row_is_foot => true).to_s.should == deindent(%q{
|
46
46
|
+----+-----+------+
|
47
47
|
| 11 | 2 | 3333 |
|
48
48
|
| 44 | 56 | 6 |
|
49
49
|
+----+-----+------+
|
50
50
|
| 7 | 888 | 99 |
|
51
51
|
+----+-----+------+
|
52
|
-
|
52
|
+
})
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -61,32 +61,32 @@ describe Enumerable do
|
|
61
61
|
}
|
62
62
|
end
|
63
63
|
it "to Text::Table" do
|
64
|
-
@hsh.to_text_table.to_s.should ==
|
64
|
+
@hsh.to_text_table.to_s.should == deindent(%q{
|
65
65
|
+---+----+
|
66
66
|
| k | vv |
|
67
67
|
| k | vv |
|
68
68
|
+---+----+
|
69
|
-
|
69
|
+
})
|
70
70
|
end
|
71
71
|
|
72
72
|
it "to Text::Table, setting first row as head" do
|
73
|
-
@hsh.to_text_table(:first_row_is_head => true).to_s.should ==
|
73
|
+
@hsh.to_text_table(:first_row_is_head => true).to_s.should == deindent(%q{
|
74
74
|
+---+----+
|
75
75
|
| k | vv |
|
76
76
|
+---+----+
|
77
77
|
| k | vv |
|
78
78
|
+---+----+
|
79
|
-
|
79
|
+
})
|
80
80
|
end
|
81
81
|
|
82
82
|
it "to Text::Table, setting last row as foot" do
|
83
|
-
@hsh.to_text_table(:last_row_is_foot => true).to_s.should ==
|
83
|
+
@hsh.to_text_table(:last_row_is_foot => true).to_s.should == deindent(%q{
|
84
84
|
+---+----+
|
85
85
|
| k | vv |
|
86
86
|
+---+----+
|
87
87
|
| k | vv |
|
88
88
|
+---+----+
|
89
|
-
|
89
|
+
})
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'integration_helper'
|
2
|
+
|
3
|
+
describe Text::Table do
|
4
|
+
let(:table) { Text::Table.new :rows => rows, :head => head, :foot => foot }
|
5
|
+
let(:rows) { @rows }
|
6
|
+
let(:head) { @head }
|
7
|
+
let(:foot) { @foot }
|
8
|
+
subject { table.to_s }
|
9
|
+
|
10
|
+
describe 'header alignment' do
|
11
|
+
let(:head) { @head.map { |heading| { :value => heading, :align => align } } }
|
12
|
+
let(:foot) { nil }
|
13
|
+
|
14
|
+
context 'when left' do
|
15
|
+
let(:align) { :left }
|
16
|
+
|
17
|
+
it { should == deindent(%q{
|
18
|
+
+-----+------+------+------+
|
19
|
+
| a | bb | ccc | dddd |
|
20
|
+
+-----+------+------+------+
|
21
|
+
| aa | bbb | cccc | d |
|
22
|
+
| aaa | bbbb | c | dd |
|
23
|
+
+-----+------+------+------+
|
24
|
+
}) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when center' do
|
28
|
+
let(:align) { :center }
|
29
|
+
|
30
|
+
it { should == deindent(%q{
|
31
|
+
+-----+------+------+------+
|
32
|
+
| a | bb | ccc | dddd |
|
33
|
+
+-----+------+------+------+
|
34
|
+
| aa | bbb | cccc | d |
|
35
|
+
| aaa | bbbb | c | dd |
|
36
|
+
+-----+------+------+------+
|
37
|
+
}) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when right' do
|
41
|
+
let(:align) { :right }
|
42
|
+
|
43
|
+
it { should == deindent(%q{
|
44
|
+
+-----+------+------+------+
|
45
|
+
| a | bb | ccc | dddd |
|
46
|
+
+-----+------+------+------+
|
47
|
+
| aa | bbb | cccc | d |
|
48
|
+
| aaa | bbbb | c | dd |
|
49
|
+
+-----+------+------+------+
|
50
|
+
}) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'cell alignment' do
|
55
|
+
let(:rows) { @rows.map { |row| row.map { |cell| { :value => cell, :align => align } } } }
|
56
|
+
|
57
|
+
context 'when left' do
|
58
|
+
let(:align) { :left }
|
59
|
+
|
60
|
+
it { should == deindent(%q{
|
61
|
+
+------+------+------+------+
|
62
|
+
| a | bb | ccc | dddd |
|
63
|
+
+------+------+------+------+
|
64
|
+
| aa | bbb | cccc | d |
|
65
|
+
| aaa | bbbb | c | dd |
|
66
|
+
+------+------+------+------+
|
67
|
+
| aaaa | b | cc | ddd |
|
68
|
+
+------+------+------+------+
|
69
|
+
}) }
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when center' do
|
73
|
+
let(:align) { :center }
|
74
|
+
|
75
|
+
it { should == deindent(%q{
|
76
|
+
+------+------+------+------+
|
77
|
+
| a | bb | ccc | dddd |
|
78
|
+
+------+------+------+------+
|
79
|
+
| aa | bbb | cccc | d |
|
80
|
+
| aaa | bbbb | c | dd |
|
81
|
+
+------+------+------+------+
|
82
|
+
| aaaa | b | cc | ddd |
|
83
|
+
+------+------+------+------+
|
84
|
+
}) }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when right' do
|
88
|
+
let(:align) { :right }
|
89
|
+
|
90
|
+
it { should == deindent(%q{
|
91
|
+
+------+------+------+------+
|
92
|
+
| a | bb | ccc | dddd |
|
93
|
+
+------+------+------+------+
|
94
|
+
| aa | bbb | cccc | d |
|
95
|
+
| aaa | bbbb | c | dd |
|
96
|
+
+------+------+------+------+
|
97
|
+
| aaaa | b | cc | ddd |
|
98
|
+
+------+------+------+------+
|
99
|
+
}) }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'footer alignment' do
|
104
|
+
let(:head) { nil }
|
105
|
+
let(:foot) { @foot.map { |footing| { :value => footing, :align => align } } }
|
106
|
+
|
107
|
+
context 'when left' do
|
108
|
+
let(:align) { :left }
|
109
|
+
|
110
|
+
it { should == deindent(%q{
|
111
|
+
+------+------+------+-----+
|
112
|
+
| aa | bbb | cccc | d |
|
113
|
+
| aaa | bbbb | c | dd |
|
114
|
+
+------+------+------+-----+
|
115
|
+
| aaaa | b | cc | ddd |
|
116
|
+
+------+------+------+-----+
|
117
|
+
}) }
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'when center' do
|
121
|
+
let(:align) { :center }
|
122
|
+
|
123
|
+
it { should == deindent(%q{
|
124
|
+
+------+------+------+-----+
|
125
|
+
| aa | bbb | cccc | d |
|
126
|
+
| aaa | bbbb | c | dd |
|
127
|
+
+------+------+------+-----+
|
128
|
+
| aaaa | b | cc | ddd |
|
129
|
+
+------+------+------+-----+
|
130
|
+
}) }
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'when right' do
|
134
|
+
let(:align) { :right }
|
135
|
+
|
136
|
+
it { should == deindent(%q{
|
137
|
+
+------+------+------+-----+
|
138
|
+
| aa | bbb | cccc | d |
|
139
|
+
| aaa | bbbb | c | dd |
|
140
|
+
+------+------+------+-----+
|
141
|
+
| aaaa | b | cc | ddd |
|
142
|
+
+------+------+------+-----+
|
143
|
+
}) }
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'table-wide alignment' do
|
148
|
+
context 'of columns' do
|
149
|
+
before do
|
150
|
+
table.rows[1][2] = {:value => 'c', :align => :center}
|
151
|
+
table.rows << [{:value => 'x', :colspan => 2}, 'c', 'd']
|
152
|
+
table.rows << ['a', 'b', {:value => 'x', :colspan => 2}]
|
153
|
+
table.align_column 2, :right
|
154
|
+
table.align_column 3, :right
|
155
|
+
end
|
156
|
+
|
157
|
+
it { should == deindent(%q{
|
158
|
+
+------+------+------+------+
|
159
|
+
| a | bb | ccc | dddd |
|
160
|
+
+------+------+------+------+
|
161
|
+
| aa | bbb | cccc | d |
|
162
|
+
| aaa | bbbb | c | dd |
|
163
|
+
| x | c | d |
|
164
|
+
| a | b | x |
|
165
|
+
+------+------+------+------+
|
166
|
+
| aaaa | b | cc | ddd |
|
167
|
+
+------+------+------+------+
|
168
|
+
}) }
|
169
|
+
end
|
170
|
+
|
171
|
+
context 'of rows' do
|
172
|
+
pending
|
173
|
+
end
|
174
|
+
|
175
|
+
context 'of headers' do
|
176
|
+
pending
|
177
|
+
end
|
178
|
+
|
179
|
+
context 'of footers' do
|
180
|
+
pending
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|