terminal-table 1.7.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +28 -0
- data/.gitignore +2 -1
- data/Gemfile +2 -0
- data/Gemfile.lock +48 -0
- data/History.rdoc +33 -0
- data/LICENSE.txt +21 -0
- data/README.rdoc +51 -40
- data/Rakefile +10 -4
- data/lib/terminal-table/cell.rb +1 -1
- data/lib/terminal-table/row.rb +4 -0
- data/lib/terminal-table/style.rb +12 -0
- data/lib/terminal-table/table.rb +142 -37
- data/lib/terminal-table/version.rb +1 -1
- data/terminal-table.gemspec +3 -3
- metadata +20 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9e955f9096ab89f2568bd233c53dcb58d38172fceff26123d1ff60a8af951e71
|
4
|
+
data.tar.gz: 5c46d6e5cdeb4d233901374a5a5671768731e01ba246ee87ebc04788ea48de11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d11bc2fbcfec5b822035e97b0c6e2e5393c06b54770b53dd0067f7120fd55a2382f9027186bd0ac2633c926788347df794a0672cdfd476c14754e310f2d1608b
|
7
|
+
data.tar.gz: 7f69a5f7c6282defa0e2d9aebec91ab549a6d577520d8cc2d1d32116831234e36d9def50ee9d5995c900e0c8a82057f2a13e943164f259e85d09e7797008c599
|
@@ -0,0 +1,28 @@
|
|
1
|
+
name: CI
|
2
|
+
on: [push]
|
3
|
+
|
4
|
+
jobs:
|
5
|
+
test:
|
6
|
+
if: "!contains(github.event.head_commit.message, 'ci skip')"
|
7
|
+
|
8
|
+
continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }}
|
9
|
+
|
10
|
+
strategy:
|
11
|
+
fail-fast: false
|
12
|
+
|
13
|
+
matrix:
|
14
|
+
os: [ubuntu]
|
15
|
+
ruby: [2.4, 2.5, 2.6, 2.7]
|
16
|
+
|
17
|
+
runs-on: ${{ matrix.os }}-latest
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v2
|
21
|
+
|
22
|
+
- uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
bundler-cache: true
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
26
|
+
|
27
|
+
- run: bundle install
|
28
|
+
- run: bundle exec rspec
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
terminal-table (2.0.0)
|
5
|
+
unicode-display_width (~> 1.1, >= 1.1.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
method_source (1.0.0)
|
13
|
+
pry (0.13.1)
|
14
|
+
coderay (~> 1.1)
|
15
|
+
method_source (~> 1.0)
|
16
|
+
rake (13.0.1)
|
17
|
+
rspec (3.9.0)
|
18
|
+
rspec-core (~> 3.9.0)
|
19
|
+
rspec-expectations (~> 3.9.0)
|
20
|
+
rspec-mocks (~> 3.9.0)
|
21
|
+
rspec-core (3.9.3)
|
22
|
+
rspec-support (~> 3.9.3)
|
23
|
+
rspec-expectations (3.9.3)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.9.0)
|
26
|
+
rspec-mocks (3.9.1)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.9.0)
|
29
|
+
rspec-support (3.9.4)
|
30
|
+
term-ansicolor (1.7.1)
|
31
|
+
tins (~> 1.0)
|
32
|
+
tins (1.0.1)
|
33
|
+
unicode-display_width (1.7.0)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
ruby
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
bundler (~> 2)
|
40
|
+
pry
|
41
|
+
rake (~> 13.0)
|
42
|
+
rspec (>= 3.0)
|
43
|
+
term-ansicolor
|
44
|
+
terminal-table!
|
45
|
+
tins (~> 1.0.0)
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
2.1.4
|
data/History.rdoc
CHANGED
@@ -1,3 +1,36 @@
|
|
1
|
+
2.0.0 / 2020-10-28
|
2
|
+
==================
|
3
|
+
|
4
|
+
- Drops official support for Ruby 1.9.x with and of life on 2015-02-23
|
5
|
+
- Drops official support for Ruby 2.0.x with and of life on 2016-02-24
|
6
|
+
- Drops official support for Ruby 2.1.x with and of life on 2017-03-31
|
7
|
+
- Drops official support for Ruby 2.2.x with and of life on 2018-03-31
|
8
|
+
- Drops official support for Ruby 2.3.x with and of life on 2019-03-31
|
9
|
+
|
10
|
+
1.8.0 / 2017-05-16
|
11
|
+
==================
|
12
|
+
|
13
|
+
* Top and bottom borders can be disabled (@kubakrzempek, #83)
|
14
|
+
* `unicode-display-width` dependency relaxes (@mvz, #88)
|
15
|
+
|
16
|
+
* Readme and docs fixes (@loualrid, #82 and @leoarnold, #86)
|
17
|
+
* Fixed some test-related warnings (@juanitofatas, #81 and @mvz, #89)
|
18
|
+
|
19
|
+
1.7.3 / 2016-09-21
|
20
|
+
==================
|
21
|
+
|
22
|
+
* Fixed compatibility issues for Ruby 1.9, 2.0, 2.1. (@vivekbisen, #80)
|
23
|
+
|
24
|
+
1.7.2 / 2016-09-09
|
25
|
+
==================
|
26
|
+
|
27
|
+
* Fix packing table to a minimal width (@vizv, #76)
|
28
|
+
|
29
|
+
1.7.1 / 2016-08-29
|
30
|
+
==================
|
31
|
+
|
32
|
+
* Update `unicode-display_width` to fix behavior with signal traps [#78, @jrmhaig]
|
33
|
+
|
1
34
|
1.7.0 / 2016-08-29
|
2
35
|
==================
|
3
36
|
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2008-2017 TJ Holowaychuk <tj@vision-media.ca>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
{<img src="https://github.com/tj/terminal-table/workflows/CI/badge.svg" />}[https://github.com/tj/terminal-table/actions]
|
2
|
+
|
1
3
|
= Terminal Table
|
2
4
|
|
3
5
|
== Description
|
@@ -23,7 +25,7 @@ To generate a table, provide an array of arrays (which are interpreted as rows):
|
|
23
25
|
rows << ['Two', 2]
|
24
26
|
rows << ['Three', 3]
|
25
27
|
table = Terminal::Table.new :rows => rows
|
26
|
-
|
28
|
+
|
27
29
|
# > puts table
|
28
30
|
#
|
29
31
|
# +-------+---+
|
@@ -51,7 +53,7 @@ Adding rows one by one:
|
|
51
53
|
end
|
52
54
|
|
53
55
|
To add separators between rows:
|
54
|
-
|
56
|
+
|
55
57
|
table = Terminal::Table.new do |t|
|
56
58
|
t << ['One', 1]
|
57
59
|
t << :separator
|
@@ -59,7 +61,7 @@ To add separators between rows:
|
|
59
61
|
t.add_separator
|
60
62
|
t.add_row ['Three', 3]
|
61
63
|
end
|
62
|
-
|
64
|
+
|
63
65
|
# > puts table
|
64
66
|
#
|
65
67
|
# +-------+---+
|
@@ -71,7 +73,7 @@ To add separators between rows:
|
|
71
73
|
# +-------+---+
|
72
74
|
|
73
75
|
Cells can handle multiline content:
|
74
|
-
|
76
|
+
|
75
77
|
table = Terminal::Table.new do |t|
|
76
78
|
t << ['One', 1]
|
77
79
|
t << :separator
|
@@ -79,7 +81,7 @@ Cells can handle multiline content:
|
|
79
81
|
t.add_separator
|
80
82
|
t.add_row ['Three', 3]
|
81
83
|
end
|
82
|
-
|
84
|
+
|
83
85
|
# > puts table
|
84
86
|
#
|
85
87
|
# +--------+---+
|
@@ -96,7 +98,7 @@ Cells can handle multiline content:
|
|
96
98
|
To add a head to the table:
|
97
99
|
|
98
100
|
table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows
|
99
|
-
|
101
|
+
|
100
102
|
# > puts table
|
101
103
|
#
|
102
104
|
# +-------+--------+
|
@@ -112,7 +114,7 @@ To add a head to the table:
|
|
112
114
|
To add a title to the table:
|
113
115
|
|
114
116
|
table = Terminal::Table.new :title => "Cheatsheet", :headings => ['Word', 'Number'], :rows => rows
|
115
|
-
|
117
|
+
|
116
118
|
# > puts table
|
117
119
|
#
|
118
120
|
# +------------+--------+
|
@@ -130,7 +132,7 @@ To add a title to the table:
|
|
130
132
|
To align the second column to the right:
|
131
133
|
|
132
134
|
table.align_column(1, :right)
|
133
|
-
|
135
|
+
|
134
136
|
# > puts table
|
135
137
|
#
|
136
138
|
# +-------+--------+
|
@@ -144,7 +146,7 @@ To align the second column to the right:
|
|
144
146
|
To align an individual cell, you specify the cell value in a hash along the alignment:
|
145
147
|
|
146
148
|
table << ["Four", {:value => 4.0, :alignment => :center}]
|
147
|
-
|
149
|
+
|
148
150
|
# > puts table
|
149
151
|
#
|
150
152
|
# +-------+--------+
|
@@ -155,13 +157,13 @@ To align an individual cell, you specify the cell value in a hash along the alig
|
|
155
157
|
# | Three | 3 |
|
156
158
|
# | Four | 4.0 |
|
157
159
|
# +-------+--------+
|
158
|
-
|
160
|
+
|
159
161
|
=== Style
|
160
162
|
|
161
163
|
To specify style options:
|
162
|
-
|
164
|
+
|
163
165
|
table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows, :style => {:width => 80}
|
164
|
-
|
166
|
+
|
165
167
|
# > puts table
|
166
168
|
#
|
167
169
|
# +--------------------------------------+---------------------------------------+
|
@@ -173,9 +175,9 @@ To specify style options:
|
|
173
175
|
# +--------------------------------------+---------------------------------------+
|
174
176
|
|
175
177
|
And change styles on the fly:
|
176
|
-
|
178
|
+
|
177
179
|
table.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"}
|
178
|
-
|
180
|
+
|
179
181
|
# > puts table
|
180
182
|
#
|
181
183
|
# x====================x=================x
|
@@ -187,7 +189,41 @@ And change styles on the fly:
|
|
187
189
|
# | Two | 2 |
|
188
190
|
# | Three | 3 |
|
189
191
|
# x====================x=================x
|
190
|
-
|
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
|
+
|
191
227
|
To change the default style options:
|
192
228
|
|
193
229
|
Terminal::Table::Style.defaults = {:width => 80}
|
@@ -211,28 +247,3 @@ For more examples, please see the examples/examples.rb file included in the sour
|
|
211
247
|
== Author
|
212
248
|
|
213
249
|
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
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.setup
|
3
|
+
Bundler::GemHelper.install_tasks
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
require 'rake'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
desc "Run all examples"
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
10
|
+
t.ruby_opts = %w[-w]
|
11
|
+
t.rspec_opts = %w[--color]
|
6
12
|
end
|
7
13
|
|
8
14
|
desc "Default: Run specs"
|
data/lib/terminal-table/cell.rb
CHANGED
data/lib/terminal-table/row.rb
CHANGED
data/lib/terminal-table/style.rb
CHANGED
@@ -24,6 +24,7 @@ module Terminal
|
|
24
24
|
class Style
|
25
25
|
@@defaults = {
|
26
26
|
:border_x => "-", :border_y => "|", :border_i => "+",
|
27
|
+
:border_top => true, :border_bottom => true,
|
27
28
|
:padding_left => 1, :padding_right => 1,
|
28
29
|
:margin_left => '',
|
29
30
|
:width => nil, :alignment => nil,
|
@@ -33,6 +34,8 @@ module Terminal
|
|
33
34
|
attr_accessor :border_x
|
34
35
|
attr_accessor :border_y
|
35
36
|
attr_accessor :border_i
|
37
|
+
attr_accessor :border_top
|
38
|
+
attr_accessor :border_bottom
|
36
39
|
|
37
40
|
attr_accessor :padding_left
|
38
41
|
attr_accessor :padding_right
|
@@ -62,6 +65,15 @@ module Terminal
|
|
62
65
|
@@defaults = defaults.merge(options)
|
63
66
|
end
|
64
67
|
end
|
68
|
+
|
69
|
+
def on_change attr
|
70
|
+
method_name = :"#{attr}="
|
71
|
+
old_method = method method_name
|
72
|
+
define_singleton_method(method_name) do |value|
|
73
|
+
old_method.call value
|
74
|
+
yield attr.to_sym, value
|
75
|
+
end
|
76
|
+
end
|
65
77
|
end
|
66
78
|
end
|
67
79
|
end
|
data/lib/terminal-table/table.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'unicode/display_width'
|
1
|
+
require 'unicode/display_width/no_string_ext'
|
2
2
|
|
3
3
|
module Terminal
|
4
4
|
class Table
|
@@ -10,12 +10,16 @@ module Terminal
|
|
10
10
|
# Generates a ASCII table with the given _options_.
|
11
11
|
|
12
12
|
def initialize options = {}, &block
|
13
|
+
@headings = []
|
14
|
+
@rows = []
|
13
15
|
@column_widths = []
|
14
16
|
self.style = options.fetch :style, {}
|
15
17
|
self.headings = options.fetch :headings, []
|
16
18
|
self.rows = options.fetch :rows, []
|
17
19
|
self.title = options.fetch :title, nil
|
18
20
|
yield_or_eval(&block) if block
|
21
|
+
|
22
|
+
style.on_change(:width) { require_column_widths_recalc }
|
19
23
|
end
|
20
24
|
|
21
25
|
##
|
@@ -25,6 +29,7 @@ module Terminal
|
|
25
29
|
r = rows
|
26
30
|
column(n).each_with_index do |col, i|
|
27
31
|
cell = r[i][n]
|
32
|
+
next unless cell
|
28
33
|
cell.alignment = alignment unless cell.alignment?
|
29
34
|
end
|
30
35
|
end
|
@@ -35,7 +40,7 @@ module Terminal
|
|
35
40
|
def add_row array
|
36
41
|
row = array == :separator ? Separator.new(self) : Row.new(self, array)
|
37
42
|
@rows << row
|
38
|
-
|
43
|
+
require_column_widths_recalc unless row.is_a?(Separator)
|
39
44
|
end
|
40
45
|
alias :<< :add_row
|
41
46
|
|
@@ -90,8 +95,7 @@ module Terminal
|
|
90
95
|
# Return length of column _n_.
|
91
96
|
|
92
97
|
def column_width n
|
93
|
-
|
94
|
-
width + additional_column_widths[n].to_i
|
98
|
+
column_widths[n] || 0
|
95
99
|
end
|
96
100
|
alias length_of_column column_width # for legacy support
|
97
101
|
|
@@ -99,7 +103,7 @@ module Terminal
|
|
99
103
|
# Return total number of columns available.
|
100
104
|
|
101
105
|
def number_of_columns
|
102
|
-
headings_with_rows.map { |r| r.
|
106
|
+
headings_with_rows.map { |r| r.number_of_columns }.max || 0
|
103
107
|
end
|
104
108
|
|
105
109
|
##
|
@@ -109,7 +113,7 @@ module Terminal
|
|
109
113
|
arrays = [arrays] unless arrays.first.is_a?(Array)
|
110
114
|
@headings = arrays.map do |array|
|
111
115
|
row = Row.new(self, array)
|
112
|
-
|
116
|
+
require_column_widths_recalc
|
113
117
|
row
|
114
118
|
end
|
115
119
|
end
|
@@ -119,7 +123,7 @@ module Terminal
|
|
119
123
|
|
120
124
|
def render
|
121
125
|
separator = Separator.new(self)
|
122
|
-
buffer = [separator]
|
126
|
+
buffer = style.border_top ? [separator] : []
|
123
127
|
unless @title.nil?
|
124
128
|
buffer << Row.new(self, [title_cell_options])
|
125
129
|
buffer << separator
|
@@ -134,7 +138,7 @@ module Terminal
|
|
134
138
|
buffer += @rows.product([separator]).flatten
|
135
139
|
else
|
136
140
|
buffer += @rows
|
137
|
-
buffer << separator
|
141
|
+
buffer << separator if style.border_bottom
|
138
142
|
end
|
139
143
|
buffer.map { |r| style.margin_left + r.render.rstrip }.join("\n")
|
140
144
|
end
|
@@ -162,7 +166,7 @@ module Terminal
|
|
162
166
|
|
163
167
|
def title=(title)
|
164
168
|
@title = title
|
165
|
-
|
169
|
+
require_column_widths_recalc
|
166
170
|
end
|
167
171
|
|
168
172
|
##
|
@@ -178,42 +182,134 @@ module Terminal
|
|
178
182
|
private
|
179
183
|
|
180
184
|
def columns_width
|
181
|
-
|
185
|
+
column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
|
182
186
|
end
|
183
187
|
|
184
|
-
def
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
188
|
+
def recalc_column_widths
|
189
|
+
@require_column_widths_recalc = false
|
190
|
+
n_cols = number_of_columns
|
191
|
+
space_width = cell_spacing
|
192
|
+
return if n_cols == 0
|
193
|
+
|
194
|
+
# prepare rows
|
195
|
+
all_rows = headings_with_rows
|
196
|
+
all_rows << Row.new(self, [title_cell_options]) unless @title.nil?
|
197
|
+
|
198
|
+
# DP states, dp[colspan][index][split_offset] => column_width.
|
199
|
+
dp = []
|
200
|
+
|
201
|
+
# prepare initial value for DP.
|
202
|
+
all_rows.each do |row|
|
203
|
+
index = 0
|
204
|
+
row.cells.each do |cell|
|
205
|
+
cell_value = cell.value_for_column_width_recalc
|
206
|
+
cell_width = Unicode::DisplayWidth.of(cell_value.to_s)
|
207
|
+
colspan = cell.colspan
|
208
|
+
|
209
|
+
# find column width from each single cell.
|
210
|
+
dp[colspan] ||= []
|
211
|
+
dp[colspan][index] ||= [0] # add a fake cell with length 0.
|
212
|
+
dp[colspan][index][colspan] ||= 0 # initialize column length to 0.
|
213
|
+
|
214
|
+
# the last index `colspan` means width of the single column (split
|
215
|
+
# at end of each column), not a width made up of multiple columns.
|
216
|
+
single_column_length = [cell_width, dp[colspan][index][colspan]].max
|
217
|
+
dp[colspan][index][colspan] = single_column_length
|
218
|
+
|
219
|
+
index += colspan
|
220
|
+
end
|
195
221
|
end
|
196
|
-
end
|
197
222
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
223
|
+
# run DP.
|
224
|
+
(1..n_cols).each do |colspan|
|
225
|
+
dp[colspan] ||= []
|
226
|
+
(0..n_cols-colspan).each do |index|
|
227
|
+
dp[colspan][index] ||= [1]
|
228
|
+
(1...colspan).each do |offset|
|
229
|
+
# processed level became reverse map from width => [offset, ...].
|
230
|
+
left_colspan = offset
|
231
|
+
left_index = index
|
232
|
+
left_width = dp[left_colspan][left_index].keys.first
|
233
|
+
|
234
|
+
right_colspan = colspan - left_colspan
|
235
|
+
right_index = index + offset
|
236
|
+
right_width = dp[right_colspan][right_index].keys.first
|
237
|
+
|
238
|
+
dp[colspan][index][offset] = left_width + right_width + space_width
|
239
|
+
end
|
240
|
+
|
241
|
+
# reverse map it for resolution (max width and short offset first).
|
242
|
+
rmap = {}
|
243
|
+
dp[colspan][index].each_with_index do |width, offset|
|
244
|
+
rmap[width] ||= []
|
245
|
+
rmap[width] << offset
|
210
246
|
end
|
211
|
-
|
212
|
-
|
247
|
+
|
248
|
+
# sort reversely and store it back.
|
249
|
+
dp[colspan][index] = Hash[rmap.sort.reverse]
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
resolve = lambda do |colspan, full_width, index = 0|
|
254
|
+
# stop if reaches the bottom level.
|
255
|
+
return @column_widths[index] = full_width if colspan == 1
|
256
|
+
|
257
|
+
# choose best split offset for partition, or second best result
|
258
|
+
# if first one is not dividable.
|
259
|
+
candidate_offsets = dp[colspan][index].collect(&:last).flatten
|
260
|
+
offset = candidate_offsets[0]
|
261
|
+
offset = candidate_offsets[1] if offset == colspan
|
262
|
+
|
263
|
+
# prepare for next round.
|
264
|
+
left_colspan = offset
|
265
|
+
left_index = index
|
266
|
+
left_width = dp[left_colspan][left_index].keys.first
|
267
|
+
|
268
|
+
right_colspan = colspan - left_colspan
|
269
|
+
right_index = index + offset
|
270
|
+
right_width = dp[right_colspan][right_index].keys.first
|
271
|
+
|
272
|
+
# calculate reference column width, give remaining spaces to left.
|
273
|
+
total_non_space_width = full_width - (colspan - 1) * space_width
|
274
|
+
ref_column_width = total_non_space_width / colspan
|
275
|
+
remainder = total_non_space_width % colspan
|
276
|
+
rem_left_width = [remainder, left_colspan].min
|
277
|
+
rem_right_width = remainder - rem_left_width
|
278
|
+
ref_left_width = ref_column_width * left_colspan +
|
279
|
+
(left_colspan - 1) * space_width + rem_left_width
|
280
|
+
ref_right_width = ref_column_width * right_colspan +
|
281
|
+
(right_colspan - 1) * space_width + rem_right_width
|
282
|
+
|
283
|
+
# at most one width can be greater than the reference width.
|
284
|
+
if left_width <= ref_left_width and right_width <= ref_right_width
|
285
|
+
# use refernce width (evenly partition).
|
286
|
+
left_width = ref_left_width
|
287
|
+
right_width = ref_right_width
|
288
|
+
else
|
289
|
+
# the wider one takes its value, shorter one takes the rest.
|
290
|
+
if left_width > ref_left_width
|
291
|
+
right_width = full_width - left_width - space_width
|
292
|
+
else
|
293
|
+
left_width = full_width - right_width - space_width
|
213
294
|
end
|
214
|
-
i = i + 1
|
215
295
|
end
|
296
|
+
|
297
|
+
# run next round.
|
298
|
+
resolve.call(left_colspan, left_width, left_index)
|
299
|
+
resolve.call(right_colspan, right_width, right_index)
|
300
|
+
end
|
301
|
+
|
302
|
+
full_width = dp[n_cols][0].keys.first
|
303
|
+
unless style.width.nil?
|
304
|
+
new_width = style.width - space_width - style.border_y.length
|
305
|
+
if new_width < full_width
|
306
|
+
raise "Table width exceeds wanted width " +
|
307
|
+
"of #{style.width} characters."
|
308
|
+
end
|
309
|
+
full_width = new_width
|
216
310
|
end
|
311
|
+
|
312
|
+
resolve.call(n_cols, full_width)
|
217
313
|
end
|
218
314
|
|
219
315
|
##
|
@@ -235,5 +331,14 @@ module Terminal
|
|
235
331
|
def title_cell_options
|
236
332
|
{:value => @title, :alignment => :center, :colspan => number_of_columns}
|
237
333
|
end
|
334
|
+
|
335
|
+
def require_column_widths_recalc
|
336
|
+
@require_column_widths_recalc = true
|
337
|
+
end
|
338
|
+
|
339
|
+
def column_widths
|
340
|
+
recalc_column_widths if @require_column_widths_recalc
|
341
|
+
@column_widths
|
342
|
+
end
|
238
343
|
end
|
239
344
|
end
|
data/terminal-table.gemspec
CHANGED
@@ -16,11 +16,11 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
|
-
spec.add_development_dependency "bundler", "~>
|
20
|
-
spec.add_development_dependency "rake", "~>
|
19
|
+
spec.add_development_dependency "bundler", "~> 2"
|
20
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
21
21
|
spec.add_development_dependency "rspec", ">= 3.0"
|
22
22
|
spec.add_development_dependency "term-ansicolor"
|
23
23
|
spec.add_development_dependency "pry"
|
24
24
|
|
25
|
-
spec.add_runtime_dependency "unicode-display_width", "~> 1.1"
|
25
|
+
spec.add_runtime_dependency "unicode-display_width", ["~> 1.1", ">= 1.1.1"]
|
26
26
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terminal-table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Holowaychuk
|
8
8
|
- Scott J. Goldman
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2020-11-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -17,28 +17,28 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '2'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '2'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '13.0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '13.0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rspec
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +88,9 @@ dependencies:
|
|
88
88
|
- - "~>"
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
version: '1.1'
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.1.1
|
91
94
|
type: :runtime
|
92
95
|
prerelease: false
|
93
96
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -95,16 +98,22 @@ dependencies:
|
|
95
98
|
- - "~>"
|
96
99
|
- !ruby/object:Gem::Version
|
97
100
|
version: '1.1'
|
98
|
-
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.1.1
|
104
|
+
description:
|
99
105
|
email:
|
100
106
|
- tj@vision-media.ca
|
101
107
|
executables: []
|
102
108
|
extensions: []
|
103
109
|
extra_rdoc_files: []
|
104
110
|
files:
|
111
|
+
- ".github/workflows/ci.yml"
|
105
112
|
- ".gitignore"
|
106
113
|
- Gemfile
|
114
|
+
- Gemfile.lock
|
107
115
|
- History.rdoc
|
116
|
+
- LICENSE.txt
|
108
117
|
- Manifest
|
109
118
|
- README.rdoc
|
110
119
|
- Rakefile
|
@@ -124,7 +133,7 @@ homepage: https://github.com/tj/terminal-table
|
|
124
133
|
licenses:
|
125
134
|
- MIT
|
126
135
|
metadata: {}
|
127
|
-
post_install_message:
|
136
|
+
post_install_message:
|
128
137
|
rdoc_options: []
|
129
138
|
require_paths:
|
130
139
|
- lib
|
@@ -139,9 +148,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
148
|
- !ruby/object:Gem::Version
|
140
149
|
version: '0'
|
141
150
|
requirements: []
|
142
|
-
|
143
|
-
|
144
|
-
signing_key:
|
151
|
+
rubygems_version: 3.1.4
|
152
|
+
signing_key:
|
145
153
|
specification_version: 4
|
146
154
|
summary: Simple, feature rich ascii table generation library
|
147
155
|
test_files: []
|