table-formatter 0.1.6 → 0.1.7
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 +39 -7
- metadata +1 -1
- 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: 0805b84d0db329574a5c68407627d3dd7111341a
|
4
|
+
data.tar.gz: e266cc44ee6b7dd22d397e99c81b3ea3b95bdea3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24e961e95697e189e1ae00498a7222d4fea64a4525e0a2a51de8a1d41a5ca554597546eaf0a2c837d34d55095aa884466fc1e0df0c428ca506fc8379485299ff
|
7
|
+
data.tar.gz: ce50714ba2e0576bfe31af6e51868c0f452825015d659d0b9f58d5fd9c25148377897fca7751170aadbf408c0e47829a3f60246dd2c9697fabcf8d4621121e6b
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/table-formatter.rb
CHANGED
@@ -7,16 +7,30 @@ class TableFormatter
|
|
7
7
|
attr_accessor :source, :labels, :border
|
8
8
|
|
9
9
|
def initialize(opt={})
|
10
|
+
|
10
11
|
o = {source: nil, labels: nil, border: true}.merge(opt)
|
11
12
|
super()
|
12
13
|
@source = o[:source]
|
13
|
-
@
|
14
|
+
@raw_labels = o[:labels]
|
14
15
|
@border = o[:border]
|
15
16
|
@maxwidth = 60
|
16
17
|
end
|
17
18
|
|
18
19
|
def display(width=nil)
|
19
|
-
|
20
|
+
|
21
|
+
if @raw_labels then
|
22
|
+
|
23
|
+
@align_cols, @labels = [], []
|
24
|
+
|
25
|
+
@raw_labels.each do |raw_label|
|
26
|
+
|
27
|
+
col_just, label = just(raw_label)
|
28
|
+
@align_cols << col_just
|
29
|
+
@labels << label
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
20
34
|
#width ||= @maxwidth
|
21
35
|
@width = width
|
22
36
|
@maxwidth = width if width
|
@@ -38,12 +52,12 @@ class TableFormatter
|
|
38
52
|
|
39
53
|
alias to_s display
|
40
54
|
|
41
|
-
|
42
|
-
|
43
|
-
def tabulate(a)
|
44
|
-
a[0].zip(a.length > 2 ? tabulate(a[1..-1]) : a[-1])
|
55
|
+
def labels=(a)
|
56
|
+
@raw_labels = a
|
45
57
|
end
|
46
58
|
|
59
|
+
private
|
60
|
+
|
47
61
|
def fetch_column_widths(a)
|
48
62
|
|
49
63
|
d = tabulate(a).map &:flatten
|
@@ -56,7 +70,7 @@ class TableFormatter
|
|
56
70
|
bar = border == true ? '|' : ' '
|
57
71
|
buffer = bar
|
58
72
|
row.each_with_index do |col, i|
|
59
|
-
buffer += ' ' + col.
|
73
|
+
buffer += ' ' + col.method(@align_cols[i]).call(col_widths[i] + 2) + bar
|
60
74
|
end
|
61
75
|
buffer
|
62
76
|
end
|
@@ -78,6 +92,24 @@ class TableFormatter
|
|
78
92
|
a.map {|row| format_cols(row, col_widths)}
|
79
93
|
end
|
80
94
|
|
95
|
+
def just(x)
|
96
|
+
|
97
|
+
case x
|
98
|
+
when /^:.*:$/
|
99
|
+
[:center, x[1..-2]]
|
100
|
+
when /:$/
|
101
|
+
[:rjust, x[0..-2]]
|
102
|
+
when /^:/
|
103
|
+
[:ljust, x[1..-1]]
|
104
|
+
else
|
105
|
+
[:ljust, x]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def tabulate(a)
|
110
|
+
a[0].zip(a.length > 2 ? tabulate(a[1..-1]) : a[-1])
|
111
|
+
end
|
112
|
+
|
81
113
|
def wrap(s, col=@maxwidth)
|
82
114
|
s.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
|
83
115
|
"\\1\\3\n")
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|