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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11aeadab532d39ee1c8223f4cbc20181d7ab701b
4
- data.tar.gz: 5d2bcbaa2b5a69ba0112aeca54f501748105195a
3
+ metadata.gz: 35630fef5882d3f10725cf89cf97629e288cf83a
4
+ data.tar.gz: c242dd193ffbf9a2d9a1a77c825d4a9f8a58f8b0
5
5
  SHA512:
6
- metadata.gz: b4d8ab9210ad4ee09403665cb4cab7e5edc17703bd49c028a5c7416c4adc9df5134c19677fa3b68d8896ee9212184da7983e797f85bca425fa140eab142bd594
7
- data.tar.gz: 8a49ec0725fb6e238c543a92a7e98391c15e61521f3aff03a07f1f7dac15008ae52d797f5cf89b3dfca56ca181034cfae7fa2ef1ca59b63b4013e217d327f247
6
+ metadata.gz: ba7e6dba293803906167489a270b6d2124a810311bd136a3d245199f4eeecb56f3d94eaed9659a3698af152e6a24eaf239b99ffa949fe34958e924f2567d95d6
7
+ data.tar.gz: bb4a5a6adef0397a7093ad5cfaf0b053ff26f77762a71ef40d36b09b222aed471a3e7b787ccc72630052c6163f3b8440294a6d6a47c278dfe8b2766e6570a0b1
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -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, nowrap: false, divider: nil)
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
- @nowrap = nowrap
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
- def format_rows(a, col_widths)
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.each_with_index do |x,i|
103
- col_rows = @nowrap == false ? wrap(x[-1], col_widths[-1]).split(/\n/) : [x[-1]]
104
- if col_rows.length > 1 then
105
- x[-1] = col_rows[0]
106
- col_rows[1..-1].map.with_index {|y,j| a.insert(i+1+j, Array.new(x.length - 1,'') + [y])}
107
- end
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.1.13
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/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
- 8ixkARkWAmV1MB4XDTE0MDMyNTIxMDUxNVoXDTE1MDMyNTIxMDUxNVowSDESMBAG
15
+ 8ixkARkWAmV1MB4XDTE1MDcwMzE5MjA0MloXDTE2MDcwMjE5MjA0MlowSDESMBAG
16
16
  A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
17
17
  EjAQBgoJkiaJk/IsZAEZFgJldTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQ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kqhkiG9w0BAQUFAAOCAQEAswyKuUaQ
27
- zaalcas2asWXnUh5T/zbKVmymHIEG9kvXH0rMA51P4sRVEXpvqlYPBZioEzCn5f0
28
- re+bscYJS5XmScGZHjTH9Gr5eahV6AWQTpBFdI5d416db4RuOtHZ8Ek+qr2us0VD
29
- Kxqfb7yOCHMxQVNUD42DSBIsK+n2uX0++ltsnW8o8ZQvL4nrUdxMBUF28m3Z+V3o
30
- ZWtCoj2zbSQLZzmPNIy9RAzn4Z64XB651zDe432QymDfLSEVi6n5YKixjsDOfa91
31
- xMYZkElOttvQm8M6uUy483IxDueFvAWV/BnpYc9uzT6Rb9VtyMgUXX9pi/WwGjoF
32
- 0vXd6ZyRHRSlpQ==
26
+ c3RlckBqYW1lc3JvYmVydHNvbi5ldTANBgkqhkiG9w0BAQUFAAOCAQEATbhZQVe7
27
+ k3QW2Bdql+CNYT3pSJIFCZXAVrvHY3Joh54B5E/065KXNDJT35XZIYZxcTkFTfg5
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: 2014-03-26 00:00:00.000000000 Z
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: '0'
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.1.11
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