colorful_tables 0.1.0 → 0.2.0
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/colorful_tables/version.rb +1 -1
- data/lib/column.rb +5 -1
- data/lib/table.rb +28 -8
- metadata +3 -3
data/lib/column.rb
CHANGED
@@ -2,6 +2,10 @@ module ColorfulTables
|
|
2
2
|
|
3
3
|
class Column
|
4
4
|
|
5
|
+
DEFAULT_OPTIONS = {
|
6
|
+
:data_color => :light_blue
|
7
|
+
}
|
8
|
+
|
5
9
|
attr_accessor :header, :data, :options
|
6
10
|
|
7
11
|
attr_reader :width
|
@@ -9,7 +13,7 @@ module ColorfulTables
|
|
9
13
|
def initialize(header, data, options = {})
|
10
14
|
@header = header
|
11
15
|
@data = data
|
12
|
-
@options = options
|
16
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
13
17
|
@width ||= calculate_width
|
14
18
|
end
|
15
19
|
|
data/lib/table.rb
CHANGED
@@ -1,20 +1,28 @@
|
|
1
|
+
# Colors from colorize gem:
|
2
|
+
# [:black, :red, :green, :light_blue, :blue, :magenta, :cyan, :light_blue, :default, :light_black, :light_red, :light_blue, :light_light_blue, :light_blue, :light_magenta, :light_blue, :light_light_blue]
|
3
|
+
# String.colors.each { |s| puts s.to_s.colorize(s) }
|
4
|
+
|
1
5
|
module ColorfulTables
|
2
6
|
|
3
7
|
class Table
|
4
8
|
|
5
|
-
|
9
|
+
DEFAULT_OPTIONS = {
|
10
|
+
:border_color => :light_blue
|
11
|
+
}
|
6
12
|
|
7
|
-
|
13
|
+
attr_accessor :data, :options
|
8
14
|
|
9
15
|
def initialize(data, options = {})
|
10
16
|
raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
|
11
17
|
@data = data
|
12
|
-
@options = options
|
13
|
-
@
|
18
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
19
|
+
@border_chars ||= calculate_border_chars
|
20
|
+
@columns_width ||= calculate_columns_width
|
21
|
+
@columns_options ||= calculate_columns_options
|
14
22
|
end
|
15
23
|
|
16
24
|
def to_s
|
17
|
-
t = header
|
25
|
+
t = "\r" + header
|
18
26
|
self.data.map(&:data).transpose.each do |row|
|
19
27
|
t += format(row)
|
20
28
|
end
|
@@ -22,7 +30,7 @@ module ColorfulTables
|
|
22
30
|
t
|
23
31
|
end
|
24
32
|
|
25
|
-
|
33
|
+
private
|
26
34
|
|
27
35
|
def is_an_array_of_columns?(data)
|
28
36
|
array_of_booleans = data.try(:map) { |i| i.is_a? Column }
|
@@ -33,9 +41,13 @@ module ColorfulTables
|
|
33
41
|
@data.map(&:width)
|
34
42
|
end
|
35
43
|
|
44
|
+
def calculate_columns_options
|
45
|
+
@data.map(&:options)
|
46
|
+
end
|
47
|
+
|
36
48
|
def separator
|
37
49
|
# + 2 is because we have to add an space at the begining and another at the end inside a cell
|
38
|
-
self.data.inject(
|
50
|
+
self.data.inject("#{@border_chars[:cross]}") { |s, column| s += (("#{@border_chars[:horizontal]}" * (column.width + 2)) + "#{@border_chars[:cross]}") } + "\n"
|
39
51
|
end
|
40
52
|
|
41
53
|
def header
|
@@ -43,7 +55,15 @@ module ColorfulTables
|
|
43
55
|
end
|
44
56
|
|
45
57
|
def format(row_elements)
|
46
|
-
row_elements.inject_with_index(
|
58
|
+
row_elements.inject_with_index(@border_chars[:vertical]) { |s, element, index| s += " #{element.to_s.rjust(@columns_width[index]).colorize(@columns_options[index][:data_color])} #{@border_chars[:vertical]}" } + "\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
def calculate_border_chars
|
62
|
+
color = @options[:border_color]
|
63
|
+
{:vertical => '|'.colorize(color),
|
64
|
+
:horizontal => '-'.colorize(color),
|
65
|
+
:cross => '+'.colorize(color)
|
66
|
+
}
|
47
67
|
end
|
48
68
|
|
49
69
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colorful_tables
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Javier Vidal
|