table-formatter 0.1.13 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/table-formatter.rb +35 -25
- metadata +19 -19
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35630fef5882d3f10725cf89cf97629e288cf83a
|
4
|
+
data.tar.gz: c242dd193ffbf9a2d9a1a77c825d4a9f8a58f8b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba7e6dba293803906167489a270b6d2124a810311bd136a3d245199f4eeecb56f3d94eaed9659a3698af152e6a24eaf239b99ffa949fe34958e924f2567d95d6
|
7
|
+
data.tar.gz: bb4a5a6adef0397a7093ad5cfaf0b053ff26f77762a71ef40d36b09b222aed471a3e7b787ccc72630052c6163f3b8440294a6d6a47c278dfe8b2766e6570a0b1
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/table-formatter.rb
CHANGED
@@ -6,18 +6,18 @@ class TableFormatter
|
|
6
6
|
|
7
7
|
attr_accessor :source, :labels, :border, :divider
|
8
8
|
|
9
|
-
def initialize(source: nil, labels: nil, border: true,
|
9
|
+
def initialize(source: nil, labels: nil, border: true, wrap: true, divider: nil)
|
10
10
|
|
11
11
|
super()
|
12
12
|
@source = source
|
13
13
|
@raw_labels = labels
|
14
14
|
@border = border
|
15
|
-
@
|
15
|
+
@wrap = wrap
|
16
16
|
@divider = divider
|
17
17
|
@maxwidth = 60
|
18
18
|
end
|
19
19
|
|
20
|
-
def display(width=nil)
|
20
|
+
def display(width=nil, widths: nil)
|
21
21
|
|
22
22
|
if @raw_labels then
|
23
23
|
|
@@ -38,7 +38,7 @@ class TableFormatter
|
|
38
38
|
a = @source.map {|x| x.map.to_a}.to_a
|
39
39
|
labels = @labels
|
40
40
|
|
41
|
-
column_widths = fetch_column_widths(a)
|
41
|
+
column_widths = widths ? widths : fetch_column_widths(a)
|
42
42
|
|
43
43
|
column_widths[-1] -= column_widths.inject(&:+) - width if width
|
44
44
|
|
@@ -46,7 +46,7 @@ class TableFormatter
|
|
46
46
|
|
47
47
|
div = (border == true ? '-' : '') * records[0].length + "\n"
|
48
48
|
label_buffer = ''
|
49
|
-
|
49
|
+
|
50
50
|
label_buffer = format_cols(labels, column_widths) + "\n" + div if labels
|
51
51
|
|
52
52
|
div + label_buffer + records.join("\n") + "\n" + div
|
@@ -68,15 +68,16 @@ class TableFormatter
|
|
68
68
|
# find the maximum lengths
|
69
69
|
d.map{|x| x.max_by(&:length).length}
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
def format_cols(row, col_widths, bar='')
|
73
|
-
|
73
|
+
|
74
74
|
outer_bar, inner_spacer = '', ''
|
75
75
|
(outer_bar = bar = '|'; inner_spacer = ' ') if border == true
|
76
76
|
col_spacer = @divider ? 0 : 2
|
77
77
|
|
78
78
|
buffer = outer_bar
|
79
79
|
|
80
|
+
|
80
81
|
row.each_with_index do |col, i|
|
81
82
|
|
82
83
|
align = @align_cols ? @align_cols[i] : :ljust
|
@@ -86,27 +87,41 @@ class TableFormatter
|
|
86
87
|
else
|
87
88
|
[col.method(align).call(col_widths[i] + col_spacer).rstrip, '']
|
88
89
|
end
|
89
|
-
|
90
|
-
buffer += inner_spacer + val + next_bar
|
91
|
-
end
|
92
90
|
|
91
|
+
buffer += inner_spacer + val + next_bar.to_s
|
92
|
+
end
|
93
|
+
|
93
94
|
buffer
|
94
|
-
end
|
95
95
|
|
96
|
-
|
96
|
+
end
|
97
|
+
|
98
|
+
def format_rows(raw_a, col_widths)
|
97
99
|
|
98
100
|
@width = col_widths.inject(&:+)
|
99
101
|
|
100
102
|
col_widths[-1] -= col_widths.inject(&:+) - @maxwidth if @width > @maxwidth
|
101
|
-
|
102
|
-
a
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
103
|
+
|
104
|
+
a = if @wrap == true then
|
105
|
+
|
106
|
+
raw_a.map do |raw_row|
|
107
|
+
|
108
|
+
rowx = raw_row.map.with_index do |col, i|
|
109
|
+
col.chars.each_slice(col_widths[i]).map(&:join)
|
110
|
+
end
|
111
|
+
|
112
|
+
max_len = rowx.max_by {|x| x.length}.length
|
113
|
+
|
114
|
+
rowx.each do |x|
|
115
|
+
len = x.length
|
116
|
+
x.concat ([''] * (max_len - len)) if len < max_len
|
117
|
+
end
|
118
|
+
|
119
|
+
rowx.transpose
|
120
|
+
end.flatten(1)
|
121
|
+
else
|
122
|
+
raw_a
|
108
123
|
end
|
109
|
-
|
124
|
+
|
110
125
|
a.map {|row| format_cols(row, col_widths, divider) }
|
111
126
|
|
112
127
|
end
|
@@ -129,9 +144,4 @@ class TableFormatter
|
|
129
144
|
a[0].zip(a.length > 2 ? tabulate(a[1..-1]) : a[-1])
|
130
145
|
end
|
131
146
|
|
132
|
-
def wrap(s, col=@maxwidth)
|
133
|
-
s.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
|
134
|
-
"\\1\\3\n")
|
135
|
-
end
|
136
|
-
|
137
147
|
end
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -12,26 +12,26 @@ cert_chain:
|
|
12
12
|
-----BEGIN CERTIFICATE-----
|
13
13
|
MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
|
14
14
|
YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgm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cTkFTfg5
|
28
|
+
dqsYFj4vESvrFXVDJqD48Q/alpHp/I+Wuh9X1spDlnGmeHfN4AnZf9f/tFAdAv4Q
|
29
|
+
BVqRZcC11Jxce32fMHHntaM+4DUIc4QApXMSq+kwXXqDmWM4jQYHFbljhqzBceSp
|
30
|
+
50TsDKjDj5qq05S4tS/Sowmk9Z+AmRGeyeJDu5d/PGBHoHyP7YTVfGfsEVZs9Yy2
|
31
|
+
vnFODZUU2Pg5P/ClR8sD4YDz54LpnVtYeMR++l8KXUMW+QG+q13SRxmlz/4irtLj
|
32
|
+
UArbhzcwzsMfVQ==
|
33
33
|
-----END CERTIFICATE-----
|
34
|
-
date:
|
34
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
35
35
|
dependencies: []
|
36
36
|
description:
|
37
37
|
email: james@r0bertson.co.uk
|
@@ -52,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
52
|
requirements:
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version:
|
55
|
+
version: 2.1.2
|
56
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - ">="
|
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
version: '0'
|
61
61
|
requirements: []
|
62
62
|
rubyforge_project:
|
63
|
-
rubygems_version: 2.
|
63
|
+
rubygems_version: 2.4.6
|
64
64
|
signing_key:
|
65
65
|
specification_version: 4
|
66
66
|
summary: table-formatter prints a table in plain text format from an array
|
metadata.gz.sig
CHANGED
Binary file
|