terminal-table 2.0.0 → 3.0.0
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 +4 -4
- data/.gitignore +3 -0
- data/Gemfile.lock +14 -13
- data/History.rdoc +36 -0
- data/README.md +391 -0
- data/examples/examples.rb +0 -0
- data/examples/examples_unicode.rb +89 -0
- data/examples/issue100.rb +34 -0
- data/examples/issue111.rb +4 -0
- data/examples/issue95.rb +42 -0
- data/examples/strong_separator.rb +23 -0
- data/lib/terminal-table.rb +2 -2
- data/lib/terminal-table/cell.rb +2 -10
- data/lib/terminal-table/row.rb +17 -3
- data/lib/terminal-table/separator.rb +56 -4
- data/lib/terminal-table/style.rb +218 -13
- data/lib/terminal-table/table.rb +45 -15
- data/lib/terminal-table/util.rb +13 -0
- data/lib/terminal-table/version.rb +1 -1
- metadata +9 -3
- data/README.rdoc +0 -249
data/lib/terminal-table/table.rb
CHANGED
@@ -7,9 +7,10 @@ module Terminal
|
|
7
7
|
attr_reader :headings
|
8
8
|
|
9
9
|
##
|
10
|
-
# Generates a ASCII table with the given _options_.
|
10
|
+
# Generates a ASCII/Unicode table with the given _options_.
|
11
11
|
|
12
12
|
def initialize options = {}, &block
|
13
|
+
@elaborated = false
|
13
14
|
@headings = []
|
14
15
|
@rows = []
|
15
16
|
@column_widths = []
|
@@ -47,12 +48,12 @@ module Terminal
|
|
47
48
|
##
|
48
49
|
# Add a separator.
|
49
50
|
|
50
|
-
def add_separator
|
51
|
-
|
51
|
+
def add_separator(border_type: :div)
|
52
|
+
@rows << Separator.new(self, border_type: border_type)
|
52
53
|
end
|
53
54
|
|
54
55
|
def cell_spacing
|
55
|
-
cell_padding + style.
|
56
|
+
cell_padding + style.border_y_width
|
56
57
|
end
|
57
58
|
|
58
59
|
def cell_padding
|
@@ -119,28 +120,57 @@ module Terminal
|
|
119
120
|
end
|
120
121
|
|
121
122
|
##
|
122
|
-
#
|
123
|
+
# Elaborate rows to form an Array of Rows and Separators with adjacency properties added.
|
124
|
+
#
|
125
|
+
# This is separated from the String rendering so that certain features may be tweaked
|
126
|
+
# before the String is built.
|
123
127
|
|
124
|
-
def
|
125
|
-
|
126
|
-
buffer = style.border_top ? [
|
128
|
+
def elaborate_rows
|
129
|
+
|
130
|
+
buffer = style.border_top ? [Separator.new(self, border_type: :top, implicit: true)] : []
|
127
131
|
unless @title.nil?
|
128
132
|
buffer << Row.new(self, [title_cell_options])
|
129
|
-
buffer <<
|
133
|
+
buffer << Separator.new(self, implicit: true)
|
130
134
|
end
|
131
135
|
@headings.each do |row|
|
132
136
|
unless row.cells.empty?
|
133
137
|
buffer << row
|
134
|
-
buffer <<
|
138
|
+
buffer << Separator.new(self, border_type: :double, implicit: true)
|
135
139
|
end
|
136
140
|
end
|
137
141
|
if style.all_separators
|
138
|
-
|
142
|
+
@rows.each_with_index do |row, idx|
|
143
|
+
# last separator is bottom, others are :div
|
144
|
+
border_type = (idx == @rows.size - 1) ? :bot : :div
|
145
|
+
buffer << row
|
146
|
+
buffer << Separator.new(self, border_type: border_type, implicit: true)
|
147
|
+
end
|
139
148
|
else
|
140
149
|
buffer += @rows
|
141
|
-
buffer <<
|
150
|
+
buffer << Separator.new(self, border_type: :bot, implicit: true) if style.border_bottom
|
151
|
+
end
|
152
|
+
|
153
|
+
# After all implicit Separators are inserted we need to save off the
|
154
|
+
# adjacent rows so that we can decide what type of intersections to use
|
155
|
+
# based on column spans in the adjacent row(s).
|
156
|
+
buffer.each_with_index do |r, idx|
|
157
|
+
if r.is_a?(Separator)
|
158
|
+
prev_row = idx > 0 ? buffer[idx - 1] : nil
|
159
|
+
next_row = buffer.fetch(idx + 1, nil)
|
160
|
+
r.save_adjacent_rows(prev_row, next_row)
|
161
|
+
end
|
142
162
|
end
|
143
|
-
|
163
|
+
|
164
|
+
@elaborated = true
|
165
|
+
@rows = buffer
|
166
|
+
end
|
167
|
+
|
168
|
+
##
|
169
|
+
# Render the table.
|
170
|
+
|
171
|
+
def render
|
172
|
+
elaborate_rows unless @elaborated
|
173
|
+
@rows.map { |r| style.margin_left + r.render.rstrip }.join("\n")
|
144
174
|
end
|
145
175
|
alias :to_s :render
|
146
176
|
|
@@ -182,7 +212,7 @@ module Terminal
|
|
182
212
|
private
|
183
213
|
|
184
214
|
def columns_width
|
185
|
-
column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.
|
215
|
+
column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y_width
|
186
216
|
end
|
187
217
|
|
188
218
|
def recalc_column_widths
|
@@ -301,7 +331,7 @@ module Terminal
|
|
301
331
|
|
302
332
|
full_width = dp[n_cols][0].keys.first
|
303
333
|
unless style.width.nil?
|
304
|
-
new_width = style.width - space_width - style.
|
334
|
+
new_width = style.width - space_width - style.border_y_width
|
305
335
|
if new_width < full_width
|
306
336
|
raise "Table width exceeds wanted width " +
|
307
337
|
"of #{style.width} characters."
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Terminal
|
2
|
+
class Table
|
3
|
+
module Util
|
4
|
+
# removes all ANSI escape sequences (e.g. color)
|
5
|
+
def ansi_escape(line)
|
6
|
+
line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
|
7
|
+
gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
|
8
|
+
gsub(/(\x03|\x1a)/, '')
|
9
|
+
end
|
10
|
+
module_function :ansi_escape
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
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:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Holowaychuk
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-01-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -115,10 +115,15 @@ files:
|
|
115
115
|
- History.rdoc
|
116
116
|
- LICENSE.txt
|
117
117
|
- Manifest
|
118
|
-
- README.
|
118
|
+
- README.md
|
119
119
|
- Rakefile
|
120
120
|
- Todo.rdoc
|
121
121
|
- examples/examples.rb
|
122
|
+
- examples/examples_unicode.rb
|
123
|
+
- examples/issue100.rb
|
124
|
+
- examples/issue111.rb
|
125
|
+
- examples/issue95.rb
|
126
|
+
- examples/strong_separator.rb
|
122
127
|
- lib/terminal-table.rb
|
123
128
|
- lib/terminal-table/cell.rb
|
124
129
|
- lib/terminal-table/import.rb
|
@@ -127,6 +132,7 @@ files:
|
|
127
132
|
- lib/terminal-table/style.rb
|
128
133
|
- lib/terminal-table/table.rb
|
129
134
|
- lib/terminal-table/table_helper.rb
|
135
|
+
- lib/terminal-table/util.rb
|
130
136
|
- lib/terminal-table/version.rb
|
131
137
|
- terminal-table.gemspec
|
132
138
|
homepage: https://github.com/tj/terminal-table
|
data/README.rdoc
DELETED
@@ -1,249 +0,0 @@
|
|
1
|
-
{<img src="https://github.com/tj/terminal-table/workflows/CI/badge.svg" />}[https://github.com/tj/terminal-table/actions]
|
2
|
-
|
3
|
-
= Terminal Table
|
4
|
-
|
5
|
-
== Description
|
6
|
-
|
7
|
-
Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby.
|
8
|
-
|
9
|
-
== Installation
|
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
|
42
|
-
end
|
43
|
-
|
44
|
-
table = Terminal::Table.new do
|
45
|
-
self.rows = rows
|
46
|
-
end
|
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]
|
53
|
-
end
|
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 specify 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
|
-
You can also use styles to add a separator after every row:
|
194
|
-
|
195
|
-
table = Terminal::Table.new do |t|
|
196
|
-
t.add_row [1, 'One']
|
197
|
-
t.add_row [2, 'Two']
|
198
|
-
t.add_row [3, 'Three']
|
199
|
-
t.style = {:all_separators => true}
|
200
|
-
end
|
201
|
-
|
202
|
-
# > puts table
|
203
|
-
#
|
204
|
-
# +---+-------+
|
205
|
-
# | 1 | One |
|
206
|
-
# +---+-------+
|
207
|
-
# | 2 | Two |
|
208
|
-
# +---+-------+
|
209
|
-
# | 3 | Three |
|
210
|
-
# +---+-------+
|
211
|
-
|
212
|
-
You can also use styles to disable top and bottom borders of the table
|
213
|
-
|
214
|
-
table = Terminal::Table.new do |t|
|
215
|
-
t.headings = ['id', 'name']
|
216
|
-
t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]
|
217
|
-
t.style = { :border_top => false, :border_bottom => false }
|
218
|
-
end
|
219
|
-
|
220
|
-
# > puts table
|
221
|
-
# | id | name |
|
222
|
-
# +----+-------+
|
223
|
-
# | 1 | One |
|
224
|
-
# | 2 | Two |
|
225
|
-
# | 3 | Three |
|
226
|
-
|
227
|
-
To change the default style options:
|
228
|
-
|
229
|
-
Terminal::Table::Style.defaults = {:width => 80}
|
230
|
-
|
231
|
-
All Table objects created afterwards will inherit these defaults.
|
232
|
-
|
233
|
-
=== Constructor options and setter methods
|
234
|
-
|
235
|
-
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:
|
236
|
-
|
237
|
-
table = Terminal::Table.new
|
238
|
-
table.title = "Cheatsheet"
|
239
|
-
table.headings = ['Word', 'Number']
|
240
|
-
table.rows = rows
|
241
|
-
table.style = {:width => 40}
|
242
|
-
|
243
|
-
== More examples
|
244
|
-
|
245
|
-
For more examples, please see the examples/examples.rb file included in the source distribution.
|
246
|
-
|
247
|
-
== Author
|
248
|
-
|
249
|
-
TJ Holowaychuk <tj@vision-media.ca>
|