table-formatter 0.5.0 → 0.7.1
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 +5 -5
- checksums.yaml.gz.sig +0 -0
- data/lib/table-formatter.rb +73 -62
- data.tar.gz.sig +0 -0
- metadata +48 -24
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c7e3972d61d31bb0e5f4a8530f98bf4a76f8000985e0fddc646d200df5c3bd8c
|
4
|
+
data.tar.gz: c12315a1c23b7900bfa0afc3150948cccd7072c50643d2089ca156d5f9a52937
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecb25c9968b96b7d472bae86d05aac743756ad20f974b70732fb672cc3430a7102b9535ebecda1f21740a69dc0ad736596e25afa4eb1eb8a7e79bd1649392efc
|
7
|
+
data.tar.gz: 4215518c58acfe6e2513b30cfc65fa4b3090ef38f8b17e2e78bbebf9b386665cde4d8eee424abd877697eff7e529456f94b1617a88aee65736428d154e3426a3
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/table-formatter.rb
CHANGED
@@ -2,14 +2,15 @@
|
|
2
2
|
|
3
3
|
# file: table-formatter.rb
|
4
4
|
|
5
|
+
require 'c32'
|
5
6
|
require 'rdiscount'
|
6
7
|
|
7
|
-
|
8
8
|
class TableFormatter
|
9
|
+
using ColouredText
|
9
10
|
|
10
11
|
attr_accessor :source, :labels, :border, :divider, :markdown, :col_justify
|
11
|
-
|
12
|
-
def initialize(source: nil, labels: nil, border: true, wrap: true,
|
12
|
+
|
13
|
+
def initialize(source: nil, labels: nil, border: true, wrap: true,
|
13
14
|
divider: nil, markdown: false, innermarkdown: false, debug: false)
|
14
15
|
|
15
16
|
super()
|
@@ -22,15 +23,15 @@ class TableFormatter
|
|
22
23
|
@markdown = markdown
|
23
24
|
@innermarkdown = innermarkdown
|
24
25
|
@debug = debug
|
25
|
-
|
26
|
+
|
26
27
|
end
|
27
|
-
|
28
|
+
|
28
29
|
def justify(s, ajust)
|
29
|
-
|
30
|
+
|
30
31
|
a = s.split('|')
|
31
|
-
|
32
|
+
|
32
33
|
a2 = ajust.map.with_index do |x,i|
|
33
|
-
|
34
|
+
|
34
35
|
s = a[i+1]
|
35
36
|
|
36
37
|
case x
|
@@ -43,13 +44,15 @@ class TableFormatter
|
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
46
|
-
"|%s|\n" % a2.join('|')
|
47
|
+
"|%s|\n" % a2.join('|')
|
47
48
|
|
48
49
|
end
|
49
|
-
|
50
|
+
|
50
51
|
def display(width=nil, widths: nil, markdown: @markdown)
|
51
|
-
|
52
|
+
|
52
53
|
@align_cols = []
|
54
|
+
@labels ||= [''] * @source.first.length
|
55
|
+
|
53
56
|
if @labels then
|
54
57
|
|
55
58
|
labels = []
|
@@ -66,12 +69,14 @@ class TableFormatter
|
|
66
69
|
{ljust: :l, rjust: :r, center: :c}[col]
|
67
70
|
end
|
68
71
|
|
69
|
-
return display_markdown(@source, labels) if markdown
|
72
|
+
return display_markdown(@source, labels) if markdown
|
70
73
|
|
71
74
|
#width ||= @maxwidth
|
72
75
|
@width = width
|
73
76
|
@maxwidth = width if width
|
74
|
-
a = @source.map
|
77
|
+
a = @source.map do |x|
|
78
|
+
x.map{|y| col = y.to_s; col.length > 0 ? col : ' ' }
|
79
|
+
end.to_a
|
75
80
|
|
76
81
|
column_widths = widths ? widths : fetch_column_widths(a)
|
77
82
|
|
@@ -81,7 +86,7 @@ class TableFormatter
|
|
81
86
|
|
82
87
|
div = (border == true ? '-' : '') * records[0].length + "\n"
|
83
88
|
label_buffer = ''
|
84
|
-
|
89
|
+
|
85
90
|
label_buffer = format_cols(labels, column_widths) + "\n" + div if labels
|
86
91
|
|
87
92
|
div + label_buffer + records.join("\n") + "\n" + div
|
@@ -92,35 +97,35 @@ class TableFormatter
|
|
92
97
|
|
93
98
|
|
94
99
|
private
|
95
|
-
|
100
|
+
|
96
101
|
def display_markdown(a, fields)
|
97
|
-
|
102
|
+
|
98
103
|
print_row = -> (row, widths) do
|
99
104
|
'| ' + row.map\
|
100
105
|
.with_index {|y,i| y.to_s.ljust(widths[i])}.join(' | ') + " |\n"
|
101
|
-
end
|
106
|
+
end
|
102
107
|
|
103
108
|
print_thline = -> (row, widths) do
|
104
109
|
'|:' + row.map\
|
105
110
|
.with_index {|y,i| y.to_s.ljust(widths[i])}.join('|:') + "|\n"
|
106
111
|
end
|
107
|
-
|
112
|
+
|
108
113
|
if @debug then
|
109
|
-
|
114
|
+
|
110
115
|
puts '@labels: ' + @labels.inspect
|
111
116
|
puts 'thline: ' + print_thline.inspect
|
112
|
-
|
117
|
+
|
113
118
|
end
|
114
|
-
|
119
|
+
|
115
120
|
print_rows = -> (rows, widths) do
|
116
121
|
rows.map {|x| print_row.call(x,widths)}.join
|
117
122
|
end
|
118
123
|
|
119
|
-
|
124
|
+
|
120
125
|
raw_vals = a
|
121
126
|
|
122
127
|
# create Markdown hyperlinks for any URLs
|
123
|
-
|
128
|
+
|
124
129
|
vals = raw_vals.map do |row|
|
125
130
|
|
126
131
|
row.map do |col|
|
@@ -136,46 +141,49 @@ class TableFormatter
|
|
136
141
|
url_title = (a.join('.') + path)[0..39] + '...'
|
137
142
|
|
138
143
|
"[%s](%s)" % [url_title, col]
|
139
|
-
|
144
|
+
|
140
145
|
else
|
141
|
-
|
142
|
-
if @innermarkdown then
|
143
|
-
"{::nomarkdown}" +
|
146
|
+
|
147
|
+
if @innermarkdown then
|
148
|
+
"{::nomarkdown}" +
|
144
149
|
RDiscount.new(col).to_html.strip.gsub("\n",'') + "{:/}"
|
145
150
|
else
|
146
151
|
|
147
152
|
col
|
148
|
-
|
153
|
+
|
149
154
|
end
|
150
|
-
|
155
|
+
|
151
156
|
end
|
152
|
-
|
157
|
+
|
153
158
|
r
|
154
159
|
end
|
155
|
-
end
|
160
|
+
end
|
156
161
|
|
157
|
-
|
162
|
+
puts ('fields: ' + fields.inspect).debug if @debug
|
163
|
+
if fields then
|
164
|
+
widths = ([(fields + ['']).take(a.first.length)] +
|
158
165
|
vals).transpose.map{|x| x.max_by(&:length).length}
|
166
|
+
end
|
159
167
|
|
160
|
-
th = if @labels then
|
168
|
+
th = if @labels.map(&:strip).join.length > 0 then
|
161
169
|
print_row.call(fields, widths)
|
162
170
|
else
|
163
171
|
''
|
164
172
|
end
|
165
|
-
|
166
|
-
th_line = print_thline.call widths.map {|x| '-' * (x+1)}, widths
|
167
|
-
tb = print_rows.call(vals, widths)
|
168
|
-
th_line2 = justify(th_line, @col_justify)
|
169
|
-
|
170
|
-
table = th + th_line2 + tb
|
173
|
+
|
174
|
+
th_line = print_thline.call widths.map {|x| '-' * (x+1)}, widths
|
175
|
+
tb = print_rows.call(vals, widths)
|
176
|
+
th_line2 = justify(th_line, @col_justify)
|
177
|
+
|
178
|
+
table = th + th_line2 + tb
|
171
179
|
end
|
172
180
|
|
173
181
|
def display_markdown2(a, fields)
|
174
|
-
|
182
|
+
|
175
183
|
print_row = -> (row, widths) do
|
176
|
-
|
177
|
-
'| ' + row.map.with_index do |y,i|
|
178
|
-
align = @align_cols ? @align_cols[i] : :ljust
|
184
|
+
|
185
|
+
'| ' + row.map.with_index do |y,i|
|
186
|
+
align = @align_cols ? @align_cols[i] : :ljust
|
179
187
|
y.to_s.method(align).call(widths[i])
|
180
188
|
end.join(' | ') + " |\n"
|
181
189
|
end
|
@@ -199,9 +207,9 @@ class TableFormatter
|
|
199
207
|
tb = print_rows.call(a, widths)
|
200
208
|
table = th + th_line2 + tb
|
201
209
|
end
|
202
|
-
|
210
|
+
|
203
211
|
def fetch_column_widths(a)
|
204
|
-
|
212
|
+
|
205
213
|
puts 'a: ' + a.inspect if @debug
|
206
214
|
d = tabulate(a).map &:flatten
|
207
215
|
puts 'd: ' + d.inspect if @debug
|
@@ -209,16 +217,18 @@ class TableFormatter
|
|
209
217
|
d.map{|x| x.max_by(&:length).length}
|
210
218
|
|
211
219
|
end
|
212
|
-
|
220
|
+
|
213
221
|
def format_cols(row, col_widths, bar='')
|
214
222
|
|
215
|
-
outer_bar
|
216
|
-
|
223
|
+
outer_bar = ''
|
224
|
+
inner_spacer = border ? ' ' : ''
|
225
|
+
outer_bar = border ? '|' : ''
|
226
|
+
#(outer_bar = bar = '|'; inner_spacer = ' ') if border == true
|
217
227
|
col_spacer = @divider ? 0 : 2
|
218
|
-
|
228
|
+
|
219
229
|
buffer = outer_bar
|
220
230
|
|
221
|
-
|
231
|
+
|
222
232
|
row.each_with_index do |col, i|
|
223
233
|
|
224
234
|
align = @align_cols.any? ? @align_cols[i] : :ljust
|
@@ -230,45 +240,46 @@ class TableFormatter
|
|
230
240
|
end
|
231
241
|
|
232
242
|
buffer += inner_spacer + val + next_bar.to_s
|
233
|
-
|
234
|
-
|
243
|
+
puts 'buffer: ' + buffer.inspect if @debug
|
244
|
+
end
|
245
|
+
|
235
246
|
buffer
|
236
247
|
|
237
|
-
end
|
248
|
+
end
|
238
249
|
|
239
250
|
def format_rows(raw_a, col_widths)
|
240
251
|
|
241
252
|
@width = col_widths.inject(&:+)
|
242
253
|
|
243
254
|
col_widths[-1] -= col_widths.inject(&:+) - @maxwidth if @width > @maxwidth
|
244
|
-
|
255
|
+
|
245
256
|
a = if @wrap == true then
|
246
|
-
|
257
|
+
|
247
258
|
raw_a.map do |raw_row|
|
248
259
|
|
249
260
|
rowx = raw_row.map.with_index do |col, i|
|
250
261
|
col.chars.each_slice(col_widths[i]).map(&:join)
|
251
|
-
end
|
252
|
-
|
262
|
+
end
|
263
|
+
|
253
264
|
max_len = rowx.max_by {|x| x.length}.length
|
254
|
-
|
255
|
-
rowx.each do |x|
|
265
|
+
|
266
|
+
rowx.each do |x|
|
256
267
|
len = x.length
|
257
268
|
x.concat ([''] * (max_len - len)) if len < max_len
|
258
269
|
end
|
259
|
-
|
270
|
+
|
260
271
|
rowx.transpose
|
261
272
|
end.flatten(1)
|
262
273
|
else
|
263
274
|
raw_a
|
264
275
|
end
|
265
|
-
|
276
|
+
|
266
277
|
a.map {|row| format_cols(row, col_widths, divider) }
|
267
278
|
|
268
279
|
end
|
269
280
|
|
270
281
|
def just(x)
|
271
|
-
|
282
|
+
|
272
283
|
case x
|
273
284
|
when /^:.*:$/
|
274
285
|
[:center, x[1..-2]]
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table-formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -10,29 +10,53 @@ bindir: bin
|
|
10
10
|
cert_chain:
|
11
11
|
- |
|
12
12
|
-----BEGIN CERTIFICATE-----
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
13
|
+
MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
|
14
|
+
YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMjIwMjAxMjM1MTM1WhcN
|
15
|
+
MjMwMjAxMjM1MTM1WjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
|
16
|
+
cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQDRoaWP
|
17
|
+
0NrTIlcCA5Q2IJ79w1bSxVb05eCW8tTxnwXR7fuTEzmDsItLuc315nDK6KxzuoEb
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m/kbe0GcssatoUGXJt5Xc7PP
|
33
|
+
S35fv/YUDZBVcNxhMXRlxrwt3HQOWTJ4OgPbmUy3mECUiwS4zmd6gMBB4fnHu8Co
|
34
|
+
TtQHVdjMbM3o4LP7R8QX2j3EnNZujKWpnDV+rD584HGXY9KQJkyf3t/lhNFHrK+c
|
35
|
+
l3i5J/pMO13Qwr0Pdbd4P0pc/8KEBKZelhuzCHkpFbe0QjLtR5QEiQ50B6CZeLAx
|
36
|
+
0ZbFvuQZbFmrQk1+kPjecIT9
|
33
37
|
-----END CERTIFICATE-----
|
34
|
-
date:
|
38
|
+
date: 2022-02-01 00:00:00.000000000 Z
|
35
39
|
dependencies:
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: c32
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.2.0
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.2'
|
50
|
+
type: :runtime
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 0.2.0
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.2'
|
36
60
|
- !ruby/object:Gem::Dependency
|
37
61
|
name: rdiscount
|
38
62
|
requirement: !ruby/object:Gem::Requirement
|
@@ -54,7 +78,7 @@ dependencies:
|
|
54
78
|
- !ruby/object:Gem::Version
|
55
79
|
version: 2.2.0.1
|
56
80
|
description:
|
57
|
-
email:
|
81
|
+
email: digital.robertson@gmail.com
|
58
82
|
executables: []
|
59
83
|
extensions: []
|
60
84
|
extra_rdoc_files: []
|
@@ -80,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
104
|
version: '0'
|
81
105
|
requirements: []
|
82
106
|
rubyforge_project:
|
83
|
-
rubygems_version: 2.
|
107
|
+
rubygems_version: 2.7.10
|
84
108
|
signing_key:
|
85
109
|
specification_version: 4
|
86
110
|
summary: table-formatter prints a table in plain text format or Markdown format from
|
metadata.gz.sig
CHANGED
Binary file
|