leifcr-terminal-table 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/History.rdoc +48 -0
- data/Manifest +27 -0
- data/README.rdoc +238 -0
- data/Rakefile +9 -0
- data/Todo.rdoc +14 -0
- data/examples/examples.rb +83 -0
- data/lib/terminal-table.rb +26 -0
- data/lib/terminal-table/cell.rb +91 -0
- data/lib/terminal-table/import.rb +3 -0
- data/lib/terminal-table/row.rb +48 -0
- data/lib/terminal-table/separator.rb +14 -0
- data/lib/terminal-table/style.rb +67 -0
- data/lib/terminal-table/table.rb +229 -0
- data/lib/terminal-table/table_helper.rb +9 -0
- data/lib/terminal-table/version.rb +5 -0
- data/terminal-table.gemspec +24 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5dc15db190a43814881b04b4123a7e4835284a84
|
4
|
+
data.tar.gz: c495a8001a536e37956d8ed89547ce27188986c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ce60a3490cff64a3f654db3e30fa46aebb21a977446ae57131cc171d570ee9f97b81c533601d5eb044f3318ca91a151cb95b915edaa68bad70cb9247ead02a24
|
7
|
+
data.tar.gz: 2f8b8dd1167a68d590f56afec6848d4d71b2e99e9488d5e91a5607cc9daa16eb6b0fc85c8e838e35213911d618f4fa42c23fecd72d34540646410c7349144d0d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/History.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
1.4.3 / 2011-10-13
|
2
|
+
==================
|
3
|
+
|
4
|
+
* Optimize for faster table output.
|
5
|
+
|
6
|
+
1.4.2 / 2010-01-14
|
7
|
+
==================
|
8
|
+
|
9
|
+
* Fixed some bugs with colspan
|
10
|
+
|
11
|
+
=== 1.4.1 / 2009-12-18
|
12
|
+
|
13
|
+
* Fix column alignment with separators.
|
14
|
+
|
15
|
+
=== 1.4.0 / 2009-12-18
|
16
|
+
|
17
|
+
* Can now add :seperator arbitrarily in a table [thanks splattael]
|
18
|
+
* Fix common typo: seperator -> separator [thanks splattael]
|
19
|
+
|
20
|
+
=== 1.3.0 / 2009-10-16
|
21
|
+
|
22
|
+
* Major refactoring (functionality remains the same)
|
23
|
+
|
24
|
+
=== 1.2.0 / 2009-08-06
|
25
|
+
|
26
|
+
* Added colspan support to table
|
27
|
+
|
28
|
+
=== 1.1.0 / 2009-08-06
|
29
|
+
|
30
|
+
* Added colspan support to table
|
31
|
+
|
32
|
+
=== 1.1.0 / 2009-07-13
|
33
|
+
|
34
|
+
* Added Table#==
|
35
|
+
|
36
|
+
=== 1.0.5 / 2009-03-14
|
37
|
+
|
38
|
+
* Allowing nil to be passed to table for headings
|
39
|
+
* Revised doc to show that rows can be splatted now
|
40
|
+
* Misc refactoring
|
41
|
+
|
42
|
+
=== 1.0.3 / 2009-01-15
|
43
|
+
|
44
|
+
* Moved yield or eval to Terminal::Table initialize where it belongs
|
45
|
+
|
46
|
+
=== 1.0.0 / 2009-01-13
|
47
|
+
|
48
|
+
* Initial release
|
data/Manifest
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Gemfile
|
2
|
+
History.rdoc
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
Todo.rdoc
|
6
|
+
examples/examples.rb
|
7
|
+
lib/terminal-table.rb
|
8
|
+
lib/terminal-table/cell.rb
|
9
|
+
lib/terminal-table/import.rb
|
10
|
+
lib/terminal-table/row.rb
|
11
|
+
lib/terminal-table/separator.rb
|
12
|
+
lib/terminal-table/style.rb
|
13
|
+
lib/terminal-table/table.rb
|
14
|
+
lib/terminal-table/table_helper.rb
|
15
|
+
lib/terminal-table/version.rb
|
16
|
+
spec/cell_spec.rb
|
17
|
+
spec/row_spec.rb
|
18
|
+
spec/spec_helper.rb
|
19
|
+
spec/table_helper_spec.rb
|
20
|
+
spec/table_spec.rb
|
21
|
+
tasks/docs.rake
|
22
|
+
tasks/gemspec.rake
|
23
|
+
tasks/spec.rake
|
24
|
+
terminal-table.gemspec
|
25
|
+
terminal-table.sublime-project
|
26
|
+
terminal-table.sublime-workspace
|
27
|
+
Manifest
|
data/README.rdoc
ADDED
@@ -0,0 +1,238 @@
|
|
1
|
+
= Terminal Table
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby.
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
$ gem install terminal-table
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
=== Basics
|
14
|
+
|
15
|
+
To use Terminal Table:
|
16
|
+
|
17
|
+
require 'terminal-table'
|
18
|
+
|
19
|
+
To generate a table, provide an array of arrays (which are interpreted as rows):
|
20
|
+
|
21
|
+
rows = []
|
22
|
+
rows << ['One', 1]
|
23
|
+
rows << ['Two', 2]
|
24
|
+
rows << ['Three', 3]
|
25
|
+
table = Terminal::Table.new :rows => rows
|
26
|
+
|
27
|
+
# > puts table
|
28
|
+
#
|
29
|
+
# +-------+---+
|
30
|
+
# | One | 1 |
|
31
|
+
# | Two | 2 |
|
32
|
+
# | Three | 3 |
|
33
|
+
# +-------+---+
|
34
|
+
|
35
|
+
|
36
|
+
The constructor can also be given a block which is either yielded the Table object or instance evaluated:
|
37
|
+
|
38
|
+
table = Terminal::Table.new do |t|
|
39
|
+
t.rows = rows
|
40
|
+
end
|
41
|
+
|
42
|
+
table = Terminal::Table.new do
|
43
|
+
self.rows = rows
|
44
|
+
end
|
45
|
+
|
46
|
+
Adding rows one by one:
|
47
|
+
|
48
|
+
table = Terminal::Table.new do |t|
|
49
|
+
t << ['One', 1]
|
50
|
+
t.add_row ['Two', 2]
|
51
|
+
end
|
52
|
+
|
53
|
+
To add separators between rows:
|
54
|
+
|
55
|
+
table = Terminal::Table.new do |t|
|
56
|
+
t << ['One', 1]
|
57
|
+
t << :separator
|
58
|
+
t.add_row ['Two', 2]
|
59
|
+
t.add_separator
|
60
|
+
t.add_row ['Three', 3]
|
61
|
+
end
|
62
|
+
|
63
|
+
# > puts table
|
64
|
+
#
|
65
|
+
# +-------+---+
|
66
|
+
# | One | 1 |
|
67
|
+
# +-------+---+
|
68
|
+
# | Two | 2 |
|
69
|
+
# +-------+---+
|
70
|
+
# | Three | 3 |
|
71
|
+
# +-------+---+
|
72
|
+
|
73
|
+
Cells can handle multiline content:
|
74
|
+
|
75
|
+
table = Terminal::Table.new do |t|
|
76
|
+
t << ['One', 1]
|
77
|
+
t << :separator
|
78
|
+
t.add_row ["Two\nDouble", 2]
|
79
|
+
t.add_separator
|
80
|
+
t.add_row ['Three', 3]
|
81
|
+
end
|
82
|
+
|
83
|
+
# > puts table
|
84
|
+
#
|
85
|
+
# +--------+---+
|
86
|
+
# | One | 1 |
|
87
|
+
# +--------+---+
|
88
|
+
# | Two | 2 |
|
89
|
+
# | Double | |
|
90
|
+
# +--------+---+
|
91
|
+
# | Three | 3 |
|
92
|
+
# +--------+---+
|
93
|
+
|
94
|
+
=== Head
|
95
|
+
|
96
|
+
To add a head to the table:
|
97
|
+
|
98
|
+
table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows
|
99
|
+
|
100
|
+
# > puts table
|
101
|
+
#
|
102
|
+
# +-------+--------+
|
103
|
+
# | Word | Number |
|
104
|
+
# +-------+--------+
|
105
|
+
# | One | 1 |
|
106
|
+
# | Two | 2 |
|
107
|
+
# | Three | 3 |
|
108
|
+
# +-------+--------+
|
109
|
+
|
110
|
+
=== Title
|
111
|
+
|
112
|
+
To add a title to the table:
|
113
|
+
|
114
|
+
table = Terminal::Table.new :title => "Cheatsheet", :headings => ['Word', 'Number'], :rows => rows
|
115
|
+
|
116
|
+
# > puts table
|
117
|
+
#
|
118
|
+
# +------------+--------+
|
119
|
+
# | Cheatsheet |
|
120
|
+
# +------------+--------+
|
121
|
+
# | Word | Number |
|
122
|
+
# +------------+--------+
|
123
|
+
# | One | 1 |
|
124
|
+
# | Two | 2 |
|
125
|
+
# | Three | 3 |
|
126
|
+
# +------------+--------+
|
127
|
+
|
128
|
+
=== Alignment
|
129
|
+
|
130
|
+
To align the second column to the right:
|
131
|
+
|
132
|
+
table.align_column(1, :right)
|
133
|
+
|
134
|
+
# > puts table
|
135
|
+
#
|
136
|
+
# +-------+--------+
|
137
|
+
# | Word | Number |
|
138
|
+
# +-------+--------+
|
139
|
+
# | One | 1 |
|
140
|
+
# | Two | 2 |
|
141
|
+
# | Three | 3 |
|
142
|
+
# +-------+--------+
|
143
|
+
|
144
|
+
To align an individual cell, you specify the cell value in a hash along the alignment:
|
145
|
+
|
146
|
+
table << ["Four", {:value => 4.0, :alignment => :center}]
|
147
|
+
|
148
|
+
# > puts table
|
149
|
+
#
|
150
|
+
# +-------+--------+
|
151
|
+
# | Word | Number |
|
152
|
+
# +-------+--------+
|
153
|
+
# | One | 1 |
|
154
|
+
# | Two | 2 |
|
155
|
+
# | Three | 3 |
|
156
|
+
# | Four | 4.0 |
|
157
|
+
# +-------+--------+
|
158
|
+
|
159
|
+
=== Style
|
160
|
+
|
161
|
+
To specify style options:
|
162
|
+
|
163
|
+
table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows, :style => {:width => 80}
|
164
|
+
|
165
|
+
# > puts table
|
166
|
+
#
|
167
|
+
# +--------------------------------------+---------------------------------------+
|
168
|
+
# | Word | Number |
|
169
|
+
# +--------------------------------------+---------------------------------------+
|
170
|
+
# | One | 1 |
|
171
|
+
# | Two | 2 |
|
172
|
+
# | Three | 3 |
|
173
|
+
# +--------------------------------------+---------------------------------------+
|
174
|
+
|
175
|
+
And change styles on the fly:
|
176
|
+
|
177
|
+
table.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"}
|
178
|
+
|
179
|
+
# > puts table
|
180
|
+
#
|
181
|
+
# x====================x=================x
|
182
|
+
# | Cheatsheet |
|
183
|
+
# x====================x=================x
|
184
|
+
# | Word | Number |
|
185
|
+
# x====================x=================x
|
186
|
+
# | One | 1 |
|
187
|
+
# | Two | 2 |
|
188
|
+
# | Three | 3 |
|
189
|
+
# x====================x=================x
|
190
|
+
|
191
|
+
To change the default style options:
|
192
|
+
|
193
|
+
Terminal::Style.defaults = {:width => 80}
|
194
|
+
|
195
|
+
All Table objects created afterwards will inherit these defaults.
|
196
|
+
|
197
|
+
=== Constructor options and setter methods
|
198
|
+
|
199
|
+
Valid options for the constructor are :rows, :headings, :style and :title - and all options can also be set on the created table object by their setter method:
|
200
|
+
|
201
|
+
table = Terminal::Table.new
|
202
|
+
table.title = "Cheatsheet"
|
203
|
+
table.headings = ['Word', 'Number']
|
204
|
+
table.rows = rows
|
205
|
+
table.style = {:width => 40}
|
206
|
+
|
207
|
+
== More examples
|
208
|
+
|
209
|
+
For more examples, please see the examples/examples.rb file included in the source distribution.
|
210
|
+
|
211
|
+
== Author
|
212
|
+
|
213
|
+
TJ Holowaychuk <tj@vision-media.ca>
|
214
|
+
|
215
|
+
== License
|
216
|
+
|
217
|
+
(The MIT License)
|
218
|
+
|
219
|
+
Copyright (c) 2008-2009 TJ Holowaychuk <tj@vision-media.ca>
|
220
|
+
|
221
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
222
|
+
a copy of this software and associated documentation files (the
|
223
|
+
'Software'), to deal in the Software without restriction, including
|
224
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
225
|
+
distribute, sublicense, an d/or sell copies of the Software, and to
|
226
|
+
permit persons to whom the Software is furnished to do so, subject to
|
227
|
+
the following conditions:
|
228
|
+
|
229
|
+
The above copyright notice and this permission notice shall be
|
230
|
+
included in all copies or substantial portions of the Software.
|
231
|
+
|
232
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
233
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
234
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
235
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
236
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
237
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
238
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/Todo.rdoc
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
require 'terminal-table/import'
|
3
|
+
|
4
|
+
puts
|
5
|
+
puts table(['a', 'b'], [1, 2], [3, 4])
|
6
|
+
|
7
|
+
puts
|
8
|
+
puts table(['name', 'content'], ['ftp.example.com', '1.1.1.1'], ['www.example.com', '|lalalala|lalala|'])
|
9
|
+
|
10
|
+
puts
|
11
|
+
t = table ['a', 'b']
|
12
|
+
t.style = {:padding_left => 2, :width => 80}
|
13
|
+
t << [1, 2]
|
14
|
+
t << [3, 4]
|
15
|
+
t << :separator
|
16
|
+
t << [4, 6]
|
17
|
+
puts t
|
18
|
+
|
19
|
+
puts
|
20
|
+
user_table = table do |v|
|
21
|
+
v.title = "Contact Information"
|
22
|
+
v.headings = 'First Name', 'Last Name', 'Email'
|
23
|
+
v << %w( TJ Holowaychuk tj@vision-media.ca )
|
24
|
+
v << %w( Bob Someone bob@vision-media.ca )
|
25
|
+
v << %w( Joe Whatever bob@vision-media.ca )
|
26
|
+
end
|
27
|
+
puts user_table
|
28
|
+
|
29
|
+
puts
|
30
|
+
user_table = table do |v|
|
31
|
+
v.style.width = 80
|
32
|
+
v.headings = 'First Name', 'Last Name', 'Email'
|
33
|
+
v << %w( TJ Holowaychuk tj@vision-media.ca )
|
34
|
+
v << %w( Bob Someone bob@vision-media.ca )
|
35
|
+
v << %w( Joe Whatever bob@vision-media.ca )
|
36
|
+
end
|
37
|
+
puts user_table
|
38
|
+
|
39
|
+
puts
|
40
|
+
user_table = table do
|
41
|
+
self.headings = 'First Name', 'Last Name', 'Email'
|
42
|
+
add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
|
43
|
+
add_row ['Bob', 'Someone', 'bob@vision-media.ca']
|
44
|
+
add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
|
45
|
+
add_separator
|
46
|
+
add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
|
47
|
+
align_column 1, :center
|
48
|
+
end
|
49
|
+
puts user_table
|
50
|
+
|
51
|
+
puts
|
52
|
+
user_table = table do
|
53
|
+
self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
|
54
|
+
add_row ['Bob', 'Someone', '123', '456']
|
55
|
+
add_row :separator
|
56
|
+
add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
|
57
|
+
add_row :separator
|
58
|
+
add_row ['Joe', 'Whatever', '4324', '343242']
|
59
|
+
end
|
60
|
+
puts user_table
|
61
|
+
|
62
|
+
rows = []
|
63
|
+
rows << ['Lines', 100]
|
64
|
+
rows << ['Comments', 20]
|
65
|
+
rows << ['Ruby', 70]
|
66
|
+
rows << ['JavaScript', 30]
|
67
|
+
puts table([nil, 'Lines'], *rows)
|
68
|
+
|
69
|
+
rows = []
|
70
|
+
rows << ['Lines', 100]
|
71
|
+
rows << ['Comments', 20]
|
72
|
+
rows << ['Ruby', 70]
|
73
|
+
rows << ['JavaScript', 30]
|
74
|
+
puts table(nil, *rows)
|
75
|
+
|
76
|
+
rows = []
|
77
|
+
rows << ['Lines', 100]
|
78
|
+
rows << ['Comments', 20]
|
79
|
+
rows << ['Ruby', 70]
|
80
|
+
rows << ['JavaScript', 30]
|
81
|
+
table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
|
82
|
+
table.align_column 1, :right
|
83
|
+
puts table
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008-2009 TJ Holowaychuk <tj@vision-media.ca>
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
%w(cell row separator style table table_helper version).each do |file|
|
25
|
+
require "terminal-table/#{file}"
|
26
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
class Cell
|
4
|
+
##
|
5
|
+
# Cell value.
|
6
|
+
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
##
|
10
|
+
# Column span.
|
11
|
+
|
12
|
+
attr_reader :colspan
|
13
|
+
|
14
|
+
##
|
15
|
+
# Initialize with _options_.
|
16
|
+
|
17
|
+
def initialize options = nil
|
18
|
+
@value, options = options, {} unless Hash === options
|
19
|
+
@value = options.fetch :value, value
|
20
|
+
@alignment = options.fetch :alignment, nil
|
21
|
+
@colspan = options.fetch :colspan, 1
|
22
|
+
@width = options.fetch :width, @value.to_s.size
|
23
|
+
@index = options.fetch :index
|
24
|
+
@table = options.fetch :table
|
25
|
+
end
|
26
|
+
|
27
|
+
def alignment?
|
28
|
+
!@alignment.nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
def alignment
|
32
|
+
@alignment || @table.style.alignment || :left
|
33
|
+
end
|
34
|
+
|
35
|
+
def alignment=(val)
|
36
|
+
supported = %w(left center right)
|
37
|
+
if supported.include?(val.to_s)
|
38
|
+
@alignment = val
|
39
|
+
else
|
40
|
+
raise "Aligment must be one of: #{supported.join(' ')}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def align(val, position, length)
|
45
|
+
positions = { :left => :ljust, :right => :rjust, :center => :center }
|
46
|
+
val.public_send(positions[position], length)
|
47
|
+
end
|
48
|
+
def lines
|
49
|
+
@value.to_s.split(/\n/)
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Render the cell.
|
54
|
+
|
55
|
+
def render(line = 0)
|
56
|
+
left = " " * @table.style.padding_left
|
57
|
+
right = " " * @table.style.padding_right
|
58
|
+
render_width = lines[line].to_s.size - escape(lines[line]).size + width
|
59
|
+
align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
|
60
|
+
end
|
61
|
+
alias :to_s :render
|
62
|
+
|
63
|
+
##
|
64
|
+
# Returns the longest line in the cell and
|
65
|
+
# removes all ANSI escape sequences (e.g. color)
|
66
|
+
|
67
|
+
def value_for_column_width_recalc
|
68
|
+
lines.map{ |s| escape(s) }.max_by{ |s| s.size }
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Returns the width of this cell
|
73
|
+
|
74
|
+
def width
|
75
|
+
padding = (colspan - 1) * @table.cell_spacing
|
76
|
+
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
|
77
|
+
w + @table.column_width(@index + counter - 1)
|
78
|
+
end
|
79
|
+
inner_width + padding
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# removes all ANSI escape sequences (e.g. color)
|
84
|
+
def escape(line)
|
85
|
+
line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
|
86
|
+
gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
|
87
|
+
gsub(/(\x03|\x1a)/, '')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
class Row
|
4
|
+
|
5
|
+
##
|
6
|
+
# Row cells
|
7
|
+
|
8
|
+
attr_reader :cells
|
9
|
+
|
10
|
+
attr_reader :table
|
11
|
+
|
12
|
+
##
|
13
|
+
# Initialize with _width_ and _options_.
|
14
|
+
|
15
|
+
def initialize table, array = []
|
16
|
+
@cell_index = 0
|
17
|
+
@table = table
|
18
|
+
@cells = []
|
19
|
+
array.each { |item| self << item }
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_cell item
|
23
|
+
options = item.is_a?(Hash) ? item : {:value => item}
|
24
|
+
cell = Cell.new(options.merge(:index => @cell_index, :table => @table))
|
25
|
+
@cell_index += cell.colspan
|
26
|
+
@cells << cell
|
27
|
+
end
|
28
|
+
alias << add_cell
|
29
|
+
|
30
|
+
def [] index
|
31
|
+
cells[index]
|
32
|
+
end
|
33
|
+
|
34
|
+
def height
|
35
|
+
cells.map { |c| c.lines.count }.max || 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def render
|
39
|
+
y = @table.style.border_y
|
40
|
+
(0...height).to_a.map do |line|
|
41
|
+
y + cells.map do |cell|
|
42
|
+
cell.render(line)
|
43
|
+
end.join(y) + y
|
44
|
+
end.join("\n")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
class Separator < Row
|
4
|
+
|
5
|
+
def render
|
6
|
+
arr_x = (0...@table.number_of_columns).to_a.map do |i|
|
7
|
+
@table.style.border_x * (@table.column_width(i) + @table.cell_padding)
|
8
|
+
end
|
9
|
+
border_i = @table.style.border_i
|
10
|
+
border_i + arr_x.join(border_i) + border_i
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
# A Style object holds all the formatting information for a Table object
|
4
|
+
#
|
5
|
+
# To create a table with a certain style, use either the constructor
|
6
|
+
# option <tt>:style</tt>, the Table#style object or the Table#style= method
|
7
|
+
#
|
8
|
+
# All these examples have the same effect:
|
9
|
+
#
|
10
|
+
# # by constructor
|
11
|
+
# @table = Table.new(:style => {:padding_left => 2, :width => 40})
|
12
|
+
#
|
13
|
+
# # by object
|
14
|
+
# @table.style.padding_left = 2
|
15
|
+
# @table.style.width = 40
|
16
|
+
#
|
17
|
+
# # by method
|
18
|
+
# @table.style = {:padding_left => 2, :width => 40}
|
19
|
+
#
|
20
|
+
# To set a default style for all tables created afterwards use Style.defaults=
|
21
|
+
#
|
22
|
+
# Terminal::Table::Style.defaults = {:width => 80}
|
23
|
+
#
|
24
|
+
class Style
|
25
|
+
@@defaults = {
|
26
|
+
:border_x => "-", :border_y => "|", :border_i => "+",
|
27
|
+
:padding_left => 1, :padding_right => 1,
|
28
|
+
:margin_left => '',
|
29
|
+
:width => nil, :alignment => nil,
|
30
|
+
:all_separators => false
|
31
|
+
}
|
32
|
+
|
33
|
+
attr_accessor :border_x
|
34
|
+
attr_accessor :border_y
|
35
|
+
attr_accessor :border_i
|
36
|
+
|
37
|
+
attr_accessor :padding_left
|
38
|
+
attr_accessor :padding_right
|
39
|
+
|
40
|
+
attr_accessor :margin_left
|
41
|
+
|
42
|
+
attr_accessor :width
|
43
|
+
attr_accessor :alignment
|
44
|
+
|
45
|
+
attr_accessor :all_separators
|
46
|
+
|
47
|
+
|
48
|
+
def initialize options = {}
|
49
|
+
apply self.class.defaults.merge(options)
|
50
|
+
end
|
51
|
+
|
52
|
+
def apply options
|
53
|
+
options.each { |m, v| __send__ "#{m}=", v }
|
54
|
+
end
|
55
|
+
|
56
|
+
class << self
|
57
|
+
def defaults
|
58
|
+
@@defaults
|
59
|
+
end
|
60
|
+
|
61
|
+
def defaults= options
|
62
|
+
@@defaults = defaults.merge(options)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,229 @@
|
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
|
4
|
+
attr_reader :title
|
5
|
+
attr_reader :headings
|
6
|
+
|
7
|
+
##
|
8
|
+
# Generates a ASCII table with the given _options_.
|
9
|
+
|
10
|
+
def initialize options = {}, &block
|
11
|
+
@column_widths = []
|
12
|
+
self.style = options.fetch :style, {}
|
13
|
+
self.headings = options.fetch :headings, []
|
14
|
+
self.rows = options.fetch :rows, []
|
15
|
+
self.title = options.fetch :title, nil
|
16
|
+
yield_or_eval(&block) if block
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Align column _n_ to the given _alignment_ of :center, :left, or :right.
|
21
|
+
|
22
|
+
def align_column n, alignment
|
23
|
+
r = rows
|
24
|
+
column(n).each_with_index do |col, i|
|
25
|
+
cell = r[i][n]
|
26
|
+
cell.alignment = alignment unless cell.alignment?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# Add a row.
|
32
|
+
|
33
|
+
def add_row array
|
34
|
+
row = array == :separator ? Separator.new(self) : Row.new(self, array)
|
35
|
+
@rows << row
|
36
|
+
recalc_column_widths row
|
37
|
+
end
|
38
|
+
alias :<< :add_row
|
39
|
+
|
40
|
+
##
|
41
|
+
# Add a separator.
|
42
|
+
|
43
|
+
def add_separator
|
44
|
+
self << :separator
|
45
|
+
end
|
46
|
+
|
47
|
+
def cell_spacing
|
48
|
+
cell_padding + style.border_y.length
|
49
|
+
end
|
50
|
+
|
51
|
+
def cell_padding
|
52
|
+
style.padding_left + style.padding_right
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Return column _n_.
|
57
|
+
|
58
|
+
def column n, method = :value, array = rows
|
59
|
+
array.map { |row|
|
60
|
+
cell = row[n]
|
61
|
+
cell && method ? cell.__send__(method) : cell
|
62
|
+
}.compact
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# Return _n_ column including headings.
|
67
|
+
|
68
|
+
def column_with_headings n, method = :value
|
69
|
+
column n, method, headings_with_rows
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# Return columns.
|
74
|
+
|
75
|
+
def columns
|
76
|
+
(0...number_of_columns).map { |n| column n }
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Return length of column _n_.
|
81
|
+
|
82
|
+
def column_width n
|
83
|
+
width = @column_widths[n] || 0
|
84
|
+
width + additional_column_widths[n].to_i
|
85
|
+
end
|
86
|
+
alias length_of_column column_width # for legacy support
|
87
|
+
|
88
|
+
##
|
89
|
+
# Return total number of columns available.
|
90
|
+
|
91
|
+
def number_of_columns
|
92
|
+
headings_with_rows.map { |r| r.cells.size }.max
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Set the headings
|
97
|
+
|
98
|
+
def headings= arrays
|
99
|
+
arrays = [arrays] unless arrays.first.is_a?(Array)
|
100
|
+
@headings = arrays.map do |array|
|
101
|
+
row = Row.new(self, array)
|
102
|
+
recalc_column_widths row
|
103
|
+
row
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# Render the table.
|
109
|
+
|
110
|
+
def render
|
111
|
+
separator = Separator.new(self)
|
112
|
+
buffer = [separator]
|
113
|
+
unless @title.nil?
|
114
|
+
buffer << Row.new(self, [title_cell_options])
|
115
|
+
buffer << separator
|
116
|
+
end
|
117
|
+
@headings.each do |row|
|
118
|
+
unless row.cells.empty?
|
119
|
+
buffer << row
|
120
|
+
buffer << separator
|
121
|
+
end
|
122
|
+
end
|
123
|
+
if style.all_separators
|
124
|
+
buffer += @rows.product([separator]).flatten
|
125
|
+
else
|
126
|
+
buffer += @rows
|
127
|
+
buffer << separator
|
128
|
+
end
|
129
|
+
buffer.map { |r| style.margin_left + r.render }.join("\n")
|
130
|
+
end
|
131
|
+
alias :to_s :render
|
132
|
+
|
133
|
+
##
|
134
|
+
# Return rows without separator rows.
|
135
|
+
|
136
|
+
def rows
|
137
|
+
@rows.reject { |row| row.is_a? Separator }
|
138
|
+
end
|
139
|
+
|
140
|
+
def rows= array
|
141
|
+
@rows = []
|
142
|
+
array.each { |arr| self << arr }
|
143
|
+
end
|
144
|
+
|
145
|
+
def style=(options)
|
146
|
+
style.apply options
|
147
|
+
end
|
148
|
+
|
149
|
+
def style
|
150
|
+
@style ||= Style.new
|
151
|
+
end
|
152
|
+
|
153
|
+
def title=(title)
|
154
|
+
@title = title
|
155
|
+
recalc_column_widths Row.new(self, [title_cell_options])
|
156
|
+
end
|
157
|
+
|
158
|
+
##
|
159
|
+
# Check if _other_ is equal to self. _other_ is considered equal
|
160
|
+
# if it contains the same headings and rows.
|
161
|
+
|
162
|
+
def == other
|
163
|
+
if other.respond_to? :render and other.respond_to? :rows
|
164
|
+
self.headings == other.headings and self.rows == other.rows
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def columns_width
|
171
|
+
@column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
|
172
|
+
end
|
173
|
+
|
174
|
+
def additional_column_widths
|
175
|
+
return [] if style.width.nil?
|
176
|
+
spacing = style.width - columns_width
|
177
|
+
if spacing < 0
|
178
|
+
raise "Table width exceeds wanted width of #{style.width} characters."
|
179
|
+
else
|
180
|
+
per_col = spacing / number_of_columns
|
181
|
+
arr = (1...number_of_columns).to_a.map { |i| per_col }
|
182
|
+
other_cols = arr.inject(0) { |s, i| s + i }
|
183
|
+
arr << spacing - other_cols
|
184
|
+
arr
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def recalc_column_widths row
|
189
|
+
return if row.is_a? Separator
|
190
|
+
i = 0
|
191
|
+
row.cells.each do |cell|
|
192
|
+
colspan = cell.colspan
|
193
|
+
cell_value = cell.value_for_column_width_recalc
|
194
|
+
colspan.downto(1) do |j|
|
195
|
+
cell_length = cell_value.to_s.length
|
196
|
+
if colspan > 1
|
197
|
+
spacing_length = cell_spacing * (colspan - 1)
|
198
|
+
length_in_columns = (cell_length - spacing_length)
|
199
|
+
cell_length = (length_in_columns.to_f / colspan).ceil
|
200
|
+
end
|
201
|
+
if @column_widths[i].to_i < cell_length
|
202
|
+
@column_widths[i] = cell_length
|
203
|
+
end
|
204
|
+
i = i + 1
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
##
|
210
|
+
# Return headings combined with rows.
|
211
|
+
|
212
|
+
def headings_with_rows
|
213
|
+
@headings + rows
|
214
|
+
end
|
215
|
+
|
216
|
+
def yield_or_eval &block
|
217
|
+
return unless block
|
218
|
+
if block.arity > 0
|
219
|
+
yield self
|
220
|
+
else
|
221
|
+
self.instance_eval(&block)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def title_cell_options
|
226
|
+
{:value => @title, :alignment => :center, :colspan => number_of_columns}
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'terminal-table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "leifcr-terminal-table"
|
8
|
+
spec.version = Terminal::Table::VERSION
|
9
|
+
spec.authors = ["TJ Holowaychuk", "Scott J. Goldman"]
|
10
|
+
spec.email = ["tj@vision-media.ca"]
|
11
|
+
|
12
|
+
spec.summary = "Simple, feature rich ascii table generation library"
|
13
|
+
spec.homepage = "https://github.com/tj/terminal-table"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "rspec", ">= 3.0"
|
22
|
+
spec.add_development_dependency "term-ansicolor"
|
23
|
+
spec.add_development_dependency "pry"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leifcr-terminal-table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.5.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TJ Holowaychuk
|
8
|
+
- Scott J. Goldman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-04-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.10'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.10'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: term-ansicolor
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: pry
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description:
|
85
|
+
email:
|
86
|
+
- tj@vision-media.ca
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- Gemfile
|
93
|
+
- History.rdoc
|
94
|
+
- Manifest
|
95
|
+
- README.rdoc
|
96
|
+
- Rakefile
|
97
|
+
- Todo.rdoc
|
98
|
+
- examples/examples.rb
|
99
|
+
- lib/terminal-table.rb
|
100
|
+
- lib/terminal-table/cell.rb
|
101
|
+
- lib/terminal-table/import.rb
|
102
|
+
- lib/terminal-table/row.rb
|
103
|
+
- lib/terminal-table/separator.rb
|
104
|
+
- lib/terminal-table/style.rb
|
105
|
+
- lib/terminal-table/table.rb
|
106
|
+
- lib/terminal-table/table_helper.rb
|
107
|
+
- lib/terminal-table/version.rb
|
108
|
+
- terminal-table.gemspec
|
109
|
+
homepage: https://github.com/tj/terminal-table
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.5.1
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Simple, feature rich ascii table generation library
|
133
|
+
test_files: []
|