table-formatter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/table-formatter.rb +23 -2
  2. metadata +2 -2
@@ -5,17 +5,22 @@ class TableFormatter
5
5
  attr_accessor :source, :labels
6
6
 
7
7
  def initialize()
8
+ super()
8
9
  end
9
10
 
10
- def display
11
+ def display(width=nil)
11
12
  a = @source
12
13
  labels = @labels
13
14
  column_widths = fetch_column_widths(a)
15
+ column_widths[-1] -= column_widths.inject(&:+) - width if width
16
+
14
17
  records = format_rows(a.clone, column_widths)
18
+
15
19
  div = '-' * records[0].length + "\n"
16
20
  label_buffer = ''
17
21
  label_buffer = format_cols(labels, column_widths) + "\n" + div if labels
18
22
  div + label_buffer + records.join("\n") + "\n" + div
23
+
19
24
  end
20
25
 
21
26
  private
@@ -34,12 +39,28 @@ class TableFormatter
34
39
  def format_cols(row, col_widths)
35
40
  buffer = '|'
36
41
  row.each_with_index do |col, i|
42
+ puts col.inspect
37
43
  buffer += ' ' + col.ljust(col_widths[i] + 2) + '|'
38
44
  end
39
45
  buffer
40
46
  end
41
47
 
42
48
  def format_rows(a, col_widths)
49
+ col_widths[-1] -= col_widths.inject(&:+) - 80
50
+ a.each_with_index do |x,i|
51
+ col_rows = wrap(x[-1], col_widths[-1]).split(/\n/)
52
+ if col_rows.length > 1 then
53
+ x[-1] = col_rows[0]
54
+ col_rows[1..-1].map.with_index {|y,j| a.insert(i+1+j, Array.new(x.length - 1,'') + [y])}
55
+ end
56
+ end
57
+
43
58
  a.map {|row| format_cols(row, col_widths)}
44
59
  end
45
- end
60
+
61
+ def wrap(s, col=80)
62
+ s.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
63
+ "\\1\\3\n")
64
+ end
65
+
66
+ 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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-05-02 00:00:00 +01:00
12
+ date: 2010-07-04 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15