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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e3b96eba902e550087ee77b0701fc50b2b3ff1c
4
- data.tar.gz: 3eff237f64d0ee0472315d71e7c27272df8802cc
3
+ metadata.gz: 0805b84d0db329574a5c68407627d3dd7111341a
4
+ data.tar.gz: e266cc44ee6b7dd22d397e99c81b3ea3b95bdea3
5
5
  SHA512:
6
- metadata.gz: 898014ae59081c32c8ea468e222a397eb56e99f47f9ebf25ca824e76c602d0ff288cebb6e26b817a41282eb7fdbf2ef6e239bd906afd81237dda593b2c25a7af
7
- data.tar.gz: ee444b16ede63b45b3d02a9c37f8e35769d405682972d35a69eca587efc79210544a094b5790e70b769919477ca747c0056c5e27bd7f12f042a3824f9a486b7e
6
+ metadata.gz: 24e961e95697e189e1ae00498a7222d4fea64a4525e0a2a51de8a1d41a5ca554597546eaf0a2c837d34d55095aa884466fc1e0df0c428ca506fc8379485299ff
7
+ data.tar.gz: ce50714ba2e0576bfe31af6e51868c0f452825015d659d0b9f58d5fd9c25148377897fca7751170aadbf408c0e47829a3f60246dd2c9697fabcf8d4621121e6b
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -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
- @labels = o[:labels]
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
- private
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.ljust(col_widths[i] + 2) + bar
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
@@ -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.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file