terminal-table 1.4.3 → 1.4.4
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.
- data/History.rdoc +5 -0
- data/README.rdoc +212 -130
- data/Todo.rdoc +0 -1
- data/examples/examples.rb +20 -7
- data/lib/terminal-table.rb +3 -7
- data/lib/terminal-table/cell.rb +49 -14
- data/lib/terminal-table/core_ext.rb +1 -21
- data/lib/terminal-table/row.rb +48 -0
- data/lib/terminal-table/separator.rb +14 -0
- data/lib/terminal-table/style.rb +61 -0
- data/lib/terminal-table/table.rb +138 -188
- data/lib/terminal-table/version.rb +1 -1
- data/spec/cell_spec.rb +38 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/table_spec.rb +139 -65
- data/terminal-table.gemspec +4 -4
- metadata +8 -4
- data/lib/terminal-table/heading.rb +0 -10
data/History.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,138 +1,220 @@
|
|
1
|
-
|
2
1
|
= Terminal Table
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
+
Install http://gemcutter.org and execute:
|
10
|
+
|
11
|
+
$ gem install terminal-table
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
=== Basics
|
16
|
+
|
17
|
+
To use Terminal Table:
|
18
|
+
|
19
|
+
require 'terminal-table'
|
20
|
+
|
21
|
+
To generate a table, provide an array of arrays (which are interpreted as rows):
|
22
|
+
|
23
|
+
rows = []
|
24
|
+
rows << ['One', 1]
|
25
|
+
rows << ['Two', 2]
|
26
|
+
rows << ['Three', 3]
|
27
|
+
table = Terminal::Table.new :rows => rows
|
28
|
+
|
29
|
+
# > puts table
|
30
|
+
#
|
31
|
+
# +-------+---+
|
32
|
+
# | One | 1 |
|
33
|
+
# | Two | 2 |
|
34
|
+
# | Three | 3 |
|
35
|
+
# +-------+---+
|
36
|
+
|
37
|
+
|
38
|
+
The constructor can also be given a block which is either yielded the Table object or instance evaluated:
|
39
|
+
|
40
|
+
table = Terminal::Table.new do |t|
|
41
|
+
t.rows = rows
|
39
42
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
self.headings = 'First Name', 'Last Name', 'Email'
|
44
|
-
add_row ['TJ', 'Holowaychuk', { :value => 'tj@vision-media.ca', :alignment => :right }]
|
45
|
-
add_row ['Bob', 'Someone', 'bob@vision-media.ca']
|
46
|
-
add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
|
47
|
-
align_column 1, :center
|
43
|
+
|
44
|
+
table = Terminal::Table.new do
|
45
|
+
self.rows = rows
|
48
46
|
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
add_row ['
|
55
|
-
add_row ['TJ', 'Holowaychuk', {:value => "No phones", :colspan => 2, :alignment => :center}]
|
56
|
-
add_row ['Joe', 'Whatever', '4324', '343242']
|
47
|
+
|
48
|
+
Adding rows one by one:
|
49
|
+
|
50
|
+
table = Terminal::Table.new do |t|
|
51
|
+
t << ['One', 1]
|
52
|
+
t.add_row ['Two', 2]
|
57
53
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
54
|
+
|
55
|
+
To add separators between rows:
|
56
|
+
|
57
|
+
table = Terminal::Table.new do |t|
|
58
|
+
t << ['One', 1]
|
59
|
+
t << :separator
|
60
|
+
t.add_row ['Two', 2]
|
61
|
+
t.add_separator
|
62
|
+
t.add_row ['Three', 3]
|
63
|
+
end
|
64
|
+
|
65
|
+
# > puts table
|
66
|
+
#
|
67
|
+
# +-------+---+
|
68
|
+
# | One | 1 |
|
69
|
+
# +-------+---+
|
70
|
+
# | Two | 2 |
|
71
|
+
# +-------+---+
|
72
|
+
# | Three | 3 |
|
73
|
+
# +-------+---+
|
74
|
+
|
75
|
+
Cells can handle multiline content:
|
76
|
+
|
77
|
+
table = Terminal::Table.new do |t|
|
78
|
+
t << ['One', 1]
|
79
|
+
t << :separator
|
80
|
+
t.add_row ["Two\nDouble", 2]
|
81
|
+
t.add_separator
|
82
|
+
t.add_row ['Three', 3]
|
83
|
+
end
|
84
|
+
|
85
|
+
# > puts table
|
86
|
+
#
|
87
|
+
# +--------+---+
|
88
|
+
# | One | 1 |
|
89
|
+
# +--------+---+
|
90
|
+
# | Two | 2 |
|
91
|
+
# | Double | |
|
92
|
+
# +--------+---+
|
93
|
+
# | Three | 3 |
|
94
|
+
# +--------+---+
|
95
|
+
|
96
|
+
=== Head
|
97
|
+
|
98
|
+
To add a head to the table:
|
99
|
+
|
100
|
+
table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows
|
101
|
+
|
102
|
+
# > puts table
|
103
|
+
#
|
104
|
+
# +-------+--------+
|
105
|
+
# | Word | Number |
|
106
|
+
# +-------+--------+
|
107
|
+
# | One | 1 |
|
108
|
+
# | Two | 2 |
|
109
|
+
# | Three | 3 |
|
110
|
+
# +-------+--------+
|
111
|
+
|
112
|
+
=== Title
|
113
|
+
|
114
|
+
To add a title to the table:
|
115
|
+
|
116
|
+
table = Terminal::Table.new :title => "Cheatsheet", :headings => ['Word', 'Number'], :rows => rows
|
117
|
+
|
118
|
+
# > puts table
|
119
|
+
#
|
120
|
+
# +------------+--------+
|
121
|
+
# | Cheatsheet |
|
122
|
+
# +------------+--------+
|
123
|
+
# | Word | Number |
|
124
|
+
# +------------+--------+
|
125
|
+
# | One | 1 |
|
126
|
+
# | Two | 2 |
|
127
|
+
# | Three | 3 |
|
128
|
+
# +------------+--------+
|
129
|
+
|
130
|
+
=== Alignment
|
131
|
+
|
132
|
+
To align the second column to the right:
|
133
|
+
|
134
|
+
table.align_column(1, :right)
|
135
|
+
|
136
|
+
# > puts table
|
137
|
+
#
|
138
|
+
# +-------+--------+
|
139
|
+
# | Word | Number |
|
140
|
+
# +-------+--------+
|
141
|
+
# | One | 1 |
|
142
|
+
# | Two | 2 |
|
143
|
+
# | Three | 3 |
|
144
|
+
# +-------+--------+
|
145
|
+
|
146
|
+
To align an individual cell, you specify the cell value in a hash along the alignment:
|
147
|
+
|
148
|
+
table << ["Four", {:value => 4.0, :alignment => :center}]
|
149
|
+
|
150
|
+
# > puts table
|
151
|
+
#
|
152
|
+
# +-------+--------+
|
153
|
+
# | Word | Number |
|
154
|
+
# +-------+--------+
|
155
|
+
# | One | 1 |
|
156
|
+
# | Two | 2 |
|
157
|
+
# | Three | 3 |
|
158
|
+
# | Four | 4.0 |
|
159
|
+
# +-------+--------+
|
160
|
+
|
161
|
+
=== Style
|
162
|
+
|
163
|
+
To specifify style options:
|
164
|
+
|
165
|
+
table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows, :style => {:width => 80}
|
166
|
+
|
167
|
+
# > puts table
|
168
|
+
#
|
169
|
+
# +--------------------------------------+---------------------------------------+
|
170
|
+
# | Word | Number |
|
171
|
+
# +--------------------------------------+---------------------------------------+
|
172
|
+
# | One | 1 |
|
173
|
+
# | Two | 2 |
|
174
|
+
# | Three | 3 |
|
175
|
+
# +--------------------------------------+---------------------------------------+
|
176
|
+
|
177
|
+
And change styles on the fly:
|
178
|
+
|
179
|
+
table.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"}
|
180
|
+
|
181
|
+
# > puts table
|
182
|
+
#
|
183
|
+
# x====================x=================x
|
184
|
+
# | Cheatsheet |
|
185
|
+
# x====================x=================x
|
186
|
+
# | Word | Number |
|
187
|
+
# x====================x=================x
|
188
|
+
# | One | 1 |
|
189
|
+
# | Two | 2 |
|
190
|
+
# | Three | 3 |
|
191
|
+
# x====================x=================x
|
192
|
+
|
193
|
+
To change the default style options:
|
194
|
+
|
195
|
+
Terminal::Style.defaults = {:width => 80}
|
196
|
+
|
197
|
+
All Table objects created afterwards will inherit these defaults.
|
198
|
+
|
199
|
+
=== Constructor options and setter methods
|
200
|
+
|
201
|
+
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:
|
202
|
+
|
203
|
+
table = Terminal::Table.new
|
204
|
+
table.title = "Cheatsheet"
|
205
|
+
table.headings = ['Word', 'Number']
|
206
|
+
table.rows = rows
|
207
|
+
table.style = {:width => 40}
|
208
|
+
|
209
|
+
== More examples
|
210
|
+
|
211
|
+
For more examples, please see the examples/examples.rb file included in the source distribution.
|
212
|
+
|
213
|
+
== Author
|
214
|
+
|
215
|
+
TJ Holowaychuk <tj@vision-media.ca>
|
216
|
+
|
217
|
+
== License
|
136
218
|
|
137
219
|
(The MIT License)
|
138
220
|
|
data/Todo.rdoc
CHANGED
data/examples/examples.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
$:.unshift File.dirname(__FILE__) + '/../lib'
|
3
2
|
require 'terminal-table/import'
|
4
3
|
|
@@ -7,6 +6,7 @@ puts table(['a', 'b'], [1, 2], [3, 4])
|
|
7
6
|
|
8
7
|
puts
|
9
8
|
t = table ['a', 'b']
|
9
|
+
t.style = {:padding_left => 2, :width => 80}
|
10
10
|
t << [1, 2]
|
11
11
|
t << [3, 4]
|
12
12
|
t << :separator
|
@@ -14,11 +14,22 @@ t << [4, 6]
|
|
14
14
|
puts t
|
15
15
|
|
16
16
|
puts
|
17
|
-
user_table = table do |
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
user_table = table do |v|
|
18
|
+
v.title = "Contact Information"
|
19
|
+
v.headings = 'First Name', 'Last Name', 'Email'
|
20
|
+
v << %w( TJ Holowaychuk tj@vision-media.ca )
|
21
|
+
v << %w( Bob Someone bob@vision-media.ca )
|
22
|
+
v << %w( Joe Whatever bob@vision-media.ca )
|
23
|
+
end
|
24
|
+
puts user_table
|
25
|
+
|
26
|
+
puts
|
27
|
+
user_table = table do |v|
|
28
|
+
v.style.width = 80
|
29
|
+
v.headings = 'First Name', 'Last Name', 'Email'
|
30
|
+
v << %w( TJ Holowaychuk tj@vision-media.ca )
|
31
|
+
v << %w( Bob Someone bob@vision-media.ca )
|
32
|
+
v << %w( Joe Whatever bob@vision-media.ca )
|
22
33
|
end
|
23
34
|
puts user_table
|
24
35
|
|
@@ -38,7 +49,9 @@ puts
|
|
38
49
|
user_table = table do
|
39
50
|
self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
|
40
51
|
add_row ['Bob', 'Someone', '123', '456']
|
41
|
-
add_row
|
52
|
+
add_row :separator
|
53
|
+
add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
|
54
|
+
add_row :separator
|
42
55
|
add_row ['Joe', 'Whatever', '4324', '343242']
|
43
56
|
end
|
44
57
|
puts user_table
|
data/lib/terminal-table.rb
CHANGED
@@ -22,10 +22,6 @@
|
|
22
22
|
#++
|
23
23
|
|
24
24
|
$:.unshift File.dirname(__FILE__)
|
25
|
-
|
26
|
-
require
|
27
|
-
|
28
|
-
require 'terminal-table/table'
|
29
|
-
require 'terminal-table/cell'
|
30
|
-
require 'terminal-table/heading'
|
31
|
-
require 'terminal-table/table_helper'
|
25
|
+
%w(version core_ext table cell row separator style table_helper).each do |file|
|
26
|
+
require "terminal-table/#{file}"
|
27
|
+
end
|
data/lib/terminal-table/cell.rb
CHANGED
@@ -13,40 +13,75 @@ module Terminal
|
|
13
13
|
|
14
14
|
attr_reader :value
|
15
15
|
|
16
|
-
##
|
17
|
-
# Cell alignment.
|
18
|
-
|
19
|
-
attr_reader :alignment
|
20
|
-
|
21
16
|
##
|
22
17
|
# Column span.
|
23
18
|
|
24
19
|
attr_reader :colspan
|
25
20
|
|
26
21
|
##
|
27
|
-
# Initialize with
|
22
|
+
# Initialize with _options_.
|
28
23
|
|
29
|
-
def initialize
|
30
|
-
@width = width
|
24
|
+
def initialize options = nil
|
31
25
|
@value, options = options, {} unless Hash === options
|
32
26
|
@value = options.fetch :value, value
|
33
|
-
@alignment = options.fetch :alignment,
|
27
|
+
@alignment = options.fetch :alignment, nil
|
34
28
|
@colspan = options.fetch :colspan, 1
|
29
|
+
@width = options.fetch :width, @value.to_s.size
|
30
|
+
@index = options.fetch :index
|
31
|
+
@table = options.fetch :table
|
32
|
+
end
|
33
|
+
|
34
|
+
def alignment?
|
35
|
+
!@alignment.nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def alignment
|
39
|
+
@alignment || :left
|
40
|
+
end
|
41
|
+
|
42
|
+
def alignment=(val)
|
43
|
+
supported = %w(left center right)
|
44
|
+
if supported.include?(val.to_s)
|
45
|
+
@alignment = val
|
46
|
+
else
|
47
|
+
raise "Aligment must be one of: #{supported.join(' ')}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def lines
|
52
|
+
@value.to_s.split(/\n/)
|
35
53
|
end
|
36
54
|
|
37
55
|
##
|
38
56
|
# Render the cell.
|
39
57
|
|
40
|
-
def render
|
41
|
-
|
58
|
+
def render(line = 0)
|
59
|
+
left = " " * @table.style.padding_left
|
60
|
+
right = " " * @table.style.padding_right
|
61
|
+
"#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding)
|
42
62
|
end
|
43
63
|
alias :to_s :render
|
44
64
|
|
45
65
|
##
|
46
|
-
#
|
66
|
+
# Returns the longest line in the cell and
|
67
|
+
# removes all ANSI escape sequences (e.g. color)
|
68
|
+
|
69
|
+
def value_for_column_width_recalc
|
70
|
+
str = lines.sort_by { |s| s.size }.last.to_s
|
71
|
+
str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
|
72
|
+
str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
|
73
|
+
str.gsub(/[\x03|\x1a]/, '')
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Returns the width of this cell
|
47
78
|
|
48
|
-
def
|
49
|
-
|
79
|
+
def width
|
80
|
+
padding = (colspan - 1) * @table.cell_spacing
|
81
|
+
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
|
82
|
+
w + @table.column_width(@index + counter - 1)
|
83
|
+
end
|
84
|
+
inner_width + padding
|
50
85
|
end
|
51
86
|
end
|
52
87
|
end
|