terminal-table 1.4.2 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +5 -1
- data/lib/terminal-table/table.rb +42 -66
- data/lib/terminal-table/version.rb +2 -2
- data/terminal-table.gemspec +3 -3
- metadata +24 -31
data/History.rdoc
CHANGED
data/lib/terminal-table/table.rb
CHANGED
@@ -13,11 +13,6 @@ module Terminal
|
|
13
13
|
|
14
14
|
X, Y, I = '-', '|', '+'
|
15
15
|
|
16
|
-
##
|
17
|
-
# Headings array.
|
18
|
-
|
19
|
-
attr_accessor :headings
|
20
|
-
|
21
16
|
##
|
22
17
|
# Rows array.
|
23
18
|
|
@@ -27,11 +22,18 @@ module Terminal
|
|
27
22
|
# Generates a ASCII table with the given _options_.
|
28
23
|
|
29
24
|
def initialize options = {}, &block
|
30
|
-
@
|
31
|
-
|
25
|
+
@column_lengths = []
|
26
|
+
self.headings = options.fetch :headings, []
|
27
|
+
@rows = []
|
28
|
+
options.fetch(:rows, []).each { |row| add_row row }
|
32
29
|
yield_or_eval &block if block
|
33
30
|
end
|
34
|
-
|
31
|
+
|
32
|
+
def headings= h
|
33
|
+
@headings = h
|
34
|
+
recalc_column_lengths @headings
|
35
|
+
end
|
36
|
+
|
35
37
|
##
|
36
38
|
# Render the table.
|
37
39
|
|
@@ -53,7 +55,7 @@ module Terminal
|
|
53
55
|
# Render headings.
|
54
56
|
|
55
57
|
def render_headings
|
56
|
-
Y + headings.map_with_index do |heading, i|
|
58
|
+
Y + @headings.map_with_index do |heading, i|
|
57
59
|
width = 0
|
58
60
|
if heading.is_a?(Hash) and !heading[:colspan].nil?
|
59
61
|
i.upto(i + heading[:colspan] - 1) do |col|
|
@@ -110,6 +112,33 @@ module Terminal
|
|
110
112
|
|
111
113
|
def add_row row
|
112
114
|
@rows << row
|
115
|
+
recalc_column_lengths row
|
116
|
+
end
|
117
|
+
|
118
|
+
def recalc_column_lengths row
|
119
|
+
if row.is_a?(Symbol) then return end
|
120
|
+
i = 0
|
121
|
+
row.each do |cell|
|
122
|
+
if cell.is_a?(Hash)
|
123
|
+
colspan = cell[:colspan] || 1
|
124
|
+
cell_value = cell[:value]
|
125
|
+
else
|
126
|
+
colspan = 1
|
127
|
+
cell_value = cell
|
128
|
+
end
|
129
|
+
colspan.downto(1) do |j|
|
130
|
+
cell_length = cell_value.to_s.length
|
131
|
+
if colspan > 1
|
132
|
+
spacing_length = (3 * (colspan - 1))
|
133
|
+
length_in_columns = (cell_length - spacing_length)
|
134
|
+
cell_length = (length_in_columns.to_f / colspan).ceil
|
135
|
+
end
|
136
|
+
if (@column_lengths[i] || 0) < cell_length
|
137
|
+
@column_lengths[i] = cell_length
|
138
|
+
end
|
139
|
+
i = i + 1
|
140
|
+
end
|
141
|
+
end
|
113
142
|
end
|
114
143
|
alias :<< :add_row
|
115
144
|
|
@@ -124,7 +153,7 @@ module Terminal
|
|
124
153
|
# Weither or not any headings are present, since they are optional.
|
125
154
|
|
126
155
|
def has_headings?
|
127
|
-
not headings.empty?
|
156
|
+
not @headings.empty?
|
128
157
|
end
|
129
158
|
|
130
159
|
##
|
@@ -179,64 +208,11 @@ module Terminal
|
|
179
208
|
(0...number_of_columns).map { |n| column n }
|
180
209
|
end
|
181
210
|
|
182
|
-
##
|
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
|
233
|
-
end
|
234
|
-
|
235
211
|
##
|
236
212
|
# Return length of column _n_.
|
237
213
|
|
238
214
|
def length_of_column n
|
239
|
-
|
215
|
+
@column_lengths[n] || 0
|
240
216
|
end
|
241
217
|
|
242
218
|
##
|
@@ -261,7 +237,7 @@ module Terminal
|
|
261
237
|
# Return headings combined with rows.
|
262
238
|
|
263
239
|
def headings_with_rows
|
264
|
-
[headings] + rows
|
240
|
+
[@headings] + rows
|
265
241
|
end
|
266
242
|
|
267
243
|
##
|
@@ -284,7 +260,7 @@ module Terminal
|
|
284
260
|
|
285
261
|
def == other
|
286
262
|
if other.respond_to? :headings and other.respond_to? :rows
|
287
|
-
headings == other.headings and rows == other.rows
|
263
|
+
@headings == other.headings and rows == other.rows
|
288
264
|
end
|
289
265
|
end
|
290
266
|
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.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
-
s.authors = ["TJ Holowaychuk"]
|
9
|
-
s.date = %q{
|
8
|
+
s.authors = ["TJ Holowaychuk", "Scott J. Goldman"]
|
9
|
+
s.date = %q{2011-10-13}
|
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,25 +1,22 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: terminal-table
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.4.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- TJ Holowaychuk
|
9
|
+
- Scott J. Goldman
|
8
10
|
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
|
-
|
12
|
-
date: 2010-01-14 00:00:00 -08:00
|
13
|
-
default_executable:
|
13
|
+
date: 2011-10-13 00:00:00.000000000Z
|
14
14
|
dependencies: []
|
15
|
-
|
16
15
|
description: Simple, feature rich ascii table generation library
|
17
16
|
email: tj@vision-media.ca
|
18
17
|
executables: []
|
19
|
-
|
20
18
|
extensions: []
|
21
|
-
|
22
|
-
extra_rdoc_files:
|
19
|
+
extra_rdoc_files:
|
23
20
|
- README.rdoc
|
24
21
|
- lib/terminal-table.rb
|
25
22
|
- lib/terminal-table/cell.rb
|
@@ -32,7 +29,7 @@ extra_rdoc_files:
|
|
32
29
|
- tasks/docs.rake
|
33
30
|
- tasks/gemspec.rake
|
34
31
|
- tasks/spec.rake
|
35
|
-
files:
|
32
|
+
files:
|
36
33
|
- History.rdoc
|
37
34
|
- Manifest
|
38
35
|
- README.rdoc
|
@@ -57,38 +54,34 @@ files:
|
|
57
54
|
- tasks/gemspec.rake
|
58
55
|
- tasks/spec.rake
|
59
56
|
- terminal-table.gemspec
|
60
|
-
has_rdoc: true
|
61
57
|
homepage: http://github.com/visionmedia/terminal-table
|
62
58
|
licenses: []
|
63
|
-
|
64
59
|
post_install_message:
|
65
|
-
rdoc_options:
|
60
|
+
rdoc_options:
|
66
61
|
- --line-numbers
|
67
62
|
- --inline-source
|
68
63
|
- --title
|
69
64
|
- Terminal-table
|
70
65
|
- --main
|
71
66
|
- README.rdoc
|
72
|
-
require_paths:
|
67
|
+
require_paths:
|
73
68
|
- lib
|
74
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '1.2'
|
86
81
|
requirements: []
|
87
|
-
|
88
82
|
rubyforge_project: terminal-table
|
89
|
-
rubygems_version: 1.
|
83
|
+
rubygems_version: 1.8.11
|
90
84
|
signing_key:
|
91
85
|
specification_version: 3
|
92
86
|
summary: Simple, feature rich ascii table generation library
|
93
87
|
test_files: []
|
94
|
-
|