terminal-table 1.8.0 → 3.0.1
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 +5 -1
- data/Gemfile.lock +49 -0
- data/History.rdoc +52 -0
- data/README.md +417 -0
- data/examples/data.csv +4 -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/issue118.rb +36 -0
- data/examples/issue95.rb +42 -0
- data/examples/show_csv_table.rb +34 -0
- data/examples/strong_separator.rb +23 -0
- data/lib/terminal-table.rb +2 -2
- data/lib/terminal-table/cell.rb +8 -8
- data/lib/terminal-table/row.rb +18 -4
- data/lib/terminal-table/separator.rb +56 -4
- data/lib/terminal-table/style.rb +218 -13
- data/lib/terminal-table/table.rb +46 -15
- data/lib/terminal-table/util.rb +13 -0
- data/lib/terminal-table/version.rb +1 -1
- data/terminal-table.gemspec +3 -3
- metadata +29 -19
- data/README.rdoc +0 -247
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 = []
|
@@ -29,6 +30,7 @@ module Terminal
|
|
29
30
|
r = rows
|
30
31
|
column(n).each_with_index do |col, i|
|
31
32
|
cell = r[i][n]
|
33
|
+
next unless cell
|
32
34
|
cell.alignment = alignment unless cell.alignment?
|
33
35
|
end
|
34
36
|
end
|
@@ -46,12 +48,12 @@ module Terminal
|
|
46
48
|
##
|
47
49
|
# Add a separator.
|
48
50
|
|
49
|
-
def add_separator
|
50
|
-
|
51
|
+
def add_separator(border_type: :div)
|
52
|
+
@rows << Separator.new(self, border_type: border_type)
|
51
53
|
end
|
52
54
|
|
53
55
|
def cell_spacing
|
54
|
-
cell_padding + style.
|
56
|
+
cell_padding + style.border_y_width
|
55
57
|
end
|
56
58
|
|
57
59
|
def cell_padding
|
@@ -118,28 +120,57 @@ module Terminal
|
|
118
120
|
end
|
119
121
|
|
120
122
|
##
|
121
|
-
#
|
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.
|
122
127
|
|
123
|
-
def
|
124
|
-
|
125
|
-
buffer = style.border_top ? [
|
128
|
+
def elaborate_rows
|
129
|
+
|
130
|
+
buffer = style.border_top ? [Separator.new(self, border_type: :top, implicit: true)] : []
|
126
131
|
unless @title.nil?
|
127
132
|
buffer << Row.new(self, [title_cell_options])
|
128
|
-
buffer <<
|
133
|
+
buffer << Separator.new(self, implicit: true)
|
129
134
|
end
|
130
135
|
@headings.each do |row|
|
131
136
|
unless row.cells.empty?
|
132
137
|
buffer << row
|
133
|
-
buffer <<
|
138
|
+
buffer << Separator.new(self, border_type: :double, implicit: true)
|
134
139
|
end
|
135
140
|
end
|
136
141
|
if style.all_separators
|
137
|
-
|
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
|
138
148
|
else
|
139
149
|
buffer += @rows
|
140
|
-
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
|
141
162
|
end
|
142
|
-
|
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")
|
143
174
|
end
|
144
175
|
alias :to_s :render
|
145
176
|
|
@@ -181,7 +212,7 @@ module Terminal
|
|
181
212
|
private
|
182
213
|
|
183
214
|
def columns_width
|
184
|
-
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
|
185
216
|
end
|
186
217
|
|
187
218
|
def recalc_column_widths
|
@@ -300,7 +331,7 @@ module Terminal
|
|
300
331
|
|
301
332
|
full_width = dp[n_cols][0].keys.first
|
302
333
|
unless style.width.nil?
|
303
|
-
new_width = style.width - space_width - style.
|
334
|
+
new_width = style.width - space_width - style.border_y_width
|
304
335
|
if new_width < full_width
|
305
336
|
raise "Table width exceeds wanted width " +
|
306
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
|
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", ["
|
25
|
+
spec.add_runtime_dependency "unicode-display_width", [">= 1.1.1", "< 3"]
|
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: 3.0.1
|
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: 2021-05-10 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
|
@@ -85,38 +85,48 @@ dependencies:
|
|
85
85
|
name: unicode-display_width
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- - "~>"
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
version: '1.1'
|
91
88
|
- - ">="
|
92
89
|
- !ruby/object:Gem::Version
|
93
90
|
version: 1.1.1
|
91
|
+
- - "<"
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '3'
|
94
94
|
type: :runtime
|
95
95
|
prerelease: false
|
96
96
|
version_requirements: !ruby/object:Gem::Requirement
|
97
97
|
requirements:
|
98
|
-
- - "~>"
|
99
|
-
- !ruby/object:Gem::Version
|
100
|
-
version: '1.1'
|
101
98
|
- - ">="
|
102
99
|
- !ruby/object:Gem::Version
|
103
100
|
version: 1.1.1
|
104
|
-
|
101
|
+
- - "<"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3'
|
104
|
+
description:
|
105
105
|
email:
|
106
106
|
- tj@vision-media.ca
|
107
107
|
executables: []
|
108
108
|
extensions: []
|
109
109
|
extra_rdoc_files: []
|
110
110
|
files:
|
111
|
+
- ".github/workflows/ci.yml"
|
111
112
|
- ".gitignore"
|
112
113
|
- Gemfile
|
114
|
+
- Gemfile.lock
|
113
115
|
- History.rdoc
|
114
116
|
- LICENSE.txt
|
115
117
|
- Manifest
|
116
|
-
- README.
|
118
|
+
- README.md
|
117
119
|
- Rakefile
|
118
120
|
- Todo.rdoc
|
121
|
+
- examples/data.csv
|
119
122
|
- examples/examples.rb
|
123
|
+
- examples/examples_unicode.rb
|
124
|
+
- examples/issue100.rb
|
125
|
+
- examples/issue111.rb
|
126
|
+
- examples/issue118.rb
|
127
|
+
- examples/issue95.rb
|
128
|
+
- examples/show_csv_table.rb
|
129
|
+
- examples/strong_separator.rb
|
120
130
|
- lib/terminal-table.rb
|
121
131
|
- lib/terminal-table/cell.rb
|
122
132
|
- lib/terminal-table/import.rb
|
@@ -125,13 +135,14 @@ files:
|
|
125
135
|
- lib/terminal-table/style.rb
|
126
136
|
- lib/terminal-table/table.rb
|
127
137
|
- lib/terminal-table/table_helper.rb
|
138
|
+
- lib/terminal-table/util.rb
|
128
139
|
- lib/terminal-table/version.rb
|
129
140
|
- terminal-table.gemspec
|
130
141
|
homepage: https://github.com/tj/terminal-table
|
131
142
|
licenses:
|
132
143
|
- MIT
|
133
144
|
metadata: {}
|
134
|
-
post_install_message:
|
145
|
+
post_install_message:
|
135
146
|
rdoc_options: []
|
136
147
|
require_paths:
|
137
148
|
- lib
|
@@ -146,9 +157,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
157
|
- !ruby/object:Gem::Version
|
147
158
|
version: '0'
|
148
159
|
requirements: []
|
149
|
-
|
150
|
-
|
151
|
-
signing_key:
|
160
|
+
rubygems_version: 3.2.3
|
161
|
+
signing_key:
|
152
162
|
specification_version: 4
|
153
163
|
summary: Simple, feature rich ascii table generation library
|
154
164
|
test_files: []
|
data/README.rdoc
DELETED
@@ -1,247 +0,0 @@
|
|
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
|
-
You can also use styles to add a separator after every row:
|
192
|
-
|
193
|
-
table = Terminal::Table.new do |t|
|
194
|
-
t.add_row [1, 'One']
|
195
|
-
t.add_row [2, 'Two']
|
196
|
-
t.add_row [3, 'Three']
|
197
|
-
t.style = {:all_separators => true}
|
198
|
-
end
|
199
|
-
|
200
|
-
# > puts table
|
201
|
-
#
|
202
|
-
# +---+-------+
|
203
|
-
# | 1 | One |
|
204
|
-
# +---+-------+
|
205
|
-
# | 2 | Two |
|
206
|
-
# +---+-------+
|
207
|
-
# | 3 | Three |
|
208
|
-
# +---+-------+
|
209
|
-
|
210
|
-
You can also use styles to disable top and bottom borders of the table
|
211
|
-
|
212
|
-
table = Terminal::Table.new do |t|
|
213
|
-
t.headings = ['id', 'name']
|
214
|
-
t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]
|
215
|
-
t.style = { :border_top => false, :border_bottom => false }
|
216
|
-
end
|
217
|
-
|
218
|
-
# > puts table
|
219
|
-
# | id | name |
|
220
|
-
# +----+-------+
|
221
|
-
# | 1 | One |
|
222
|
-
# | 2 | Two |
|
223
|
-
# | 3 | Three |
|
224
|
-
|
225
|
-
To change the default style options:
|
226
|
-
|
227
|
-
Terminal::Table::Style.defaults = {:width => 80}
|
228
|
-
|
229
|
-
All Table objects created afterwards will inherit these defaults.
|
230
|
-
|
231
|
-
=== Constructor options and setter methods
|
232
|
-
|
233
|
-
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:
|
234
|
-
|
235
|
-
table = Terminal::Table.new
|
236
|
-
table.title = "Cheatsheet"
|
237
|
-
table.headings = ['Word', 'Number']
|
238
|
-
table.rows = rows
|
239
|
-
table.style = {:width => 40}
|
240
|
-
|
241
|
-
== More examples
|
242
|
-
|
243
|
-
For more examples, please see the examples/examples.rb file included in the source distribution.
|
244
|
-
|
245
|
-
== Author
|
246
|
-
|
247
|
-
TJ Holowaychuk <tj@vision-media.ca>
|