table-formatter 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/table-formatter.rb +12 -6
- metadata +1 -1
data/lib/table-formatter.rb
CHANGED
@@ -2,10 +2,14 @@
|
|
2
2
|
|
3
3
|
class TableFormatter
|
4
4
|
|
5
|
-
attr_accessor :source, :labels
|
5
|
+
attr_accessor :source, :labels, :border
|
6
6
|
|
7
|
-
def initialize()
|
7
|
+
def initialize(opt={})
|
8
|
+
o = {source: nil, labels: nil, border: true}.merge(opt)
|
8
9
|
super()
|
10
|
+
@source = o[:source]
|
11
|
+
@labels = o[:labels]
|
12
|
+
@border = o[:border]
|
9
13
|
end
|
10
14
|
|
11
15
|
def display(width=nil)
|
@@ -16,13 +20,15 @@ class TableFormatter
|
|
16
20
|
|
17
21
|
records = format_rows(a.clone, column_widths)
|
18
22
|
|
19
|
-
div = '-' * records[0].length + "\n"
|
23
|
+
div = (border == true ? '-' : ' ') * records[0].length + "\n"
|
20
24
|
label_buffer = ''
|
21
25
|
label_buffer = format_cols(labels, column_widths) + "\n" + div if labels
|
22
26
|
div + label_buffer + records.join("\n") + "\n" + div
|
23
27
|
|
24
28
|
end
|
25
29
|
|
30
|
+
alias to_s display
|
31
|
+
|
26
32
|
private
|
27
33
|
|
28
34
|
def tabulate(a)
|
@@ -37,10 +43,10 @@ class TableFormatter
|
|
37
43
|
end
|
38
44
|
|
39
45
|
def format_cols(row, col_widths)
|
40
|
-
|
46
|
+
bar = border == true ? '|' : ' '
|
47
|
+
buffer = bar
|
41
48
|
row.each_with_index do |col, i|
|
42
|
-
|
43
|
-
buffer += ' ' + col.ljust(col_widths[i] + 2) + '|'
|
49
|
+
buffer += ' ' + col.ljust(col_widths[i] + 2) + bar
|
44
50
|
end
|
45
51
|
buffer
|
46
52
|
end
|