colorful_tables 0.0.1 → 0.1.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/colorful_tables.gemspec +2 -0
- data/lib/colorful_tables.rb +7 -2
- data/lib/colorful_tables/version.rb +1 -1
- data/lib/column.rb +26 -0
- data/lib/enumerable.rb +6 -0
- data/lib/errors.rb +3 -0
- data/lib/object.rb +14 -0
- data/lib/table.rb +51 -0
- metadata +24 -6
data/colorful_tables.gemspec
CHANGED
data/lib/colorful_tables.rb
CHANGED
data/lib/column.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module ColorfulTables
|
2
|
+
|
3
|
+
class Column
|
4
|
+
|
5
|
+
attr_accessor :header, :data, :options
|
6
|
+
|
7
|
+
attr_reader :width
|
8
|
+
|
9
|
+
def initialize(header, data, options = {})
|
10
|
+
@header = header
|
11
|
+
@data = data
|
12
|
+
@options = options
|
13
|
+
@width ||= calculate_width
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def calculate_width
|
19
|
+
# We have to add the header in the calculation
|
20
|
+
data_and_header = self.data + [self.header]
|
21
|
+
data_and_header.map { |c| c.to_s.length }.max
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/enumerable.rb
ADDED
data/lib/errors.rb
ADDED
data/lib/object.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
# From https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
|
4
|
+
def try(*a, &b)
|
5
|
+
if a.empty? && block_given?
|
6
|
+
yield self
|
7
|
+
elsif !a.empty? && !respond_to?(a.first)
|
8
|
+
nil
|
9
|
+
else
|
10
|
+
__send__(*a, &b)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/lib/table.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module ColorfulTables
|
2
|
+
|
3
|
+
class Table
|
4
|
+
|
5
|
+
attr_accessor :data, :options
|
6
|
+
|
7
|
+
attr_reader :columns_width
|
8
|
+
|
9
|
+
def initialize(data, options = {})
|
10
|
+
raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
|
11
|
+
@data = data
|
12
|
+
@options = options
|
13
|
+
@columns_width ||= calculate_columns_width
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
t = header
|
18
|
+
self.data.map(&:data).transpose.each do |row|
|
19
|
+
t += format(row)
|
20
|
+
end
|
21
|
+
t += separator
|
22
|
+
t
|
23
|
+
end
|
24
|
+
|
25
|
+
#private
|
26
|
+
|
27
|
+
def is_an_array_of_columns?(data)
|
28
|
+
array_of_booleans = data.try(:map) { |i| i.is_a? Column }
|
29
|
+
array_of_booleans.nil? ? false : !(array_of_booleans.include? false)
|
30
|
+
end
|
31
|
+
|
32
|
+
def calculate_columns_width
|
33
|
+
@data.map(&:width)
|
34
|
+
end
|
35
|
+
|
36
|
+
def separator
|
37
|
+
# + 2 is because we have to add an space at the begining and another at the end inside a cell
|
38
|
+
self.data.inject('+') { |s, column| s += (('-' * (column.width + 2)) + '+') } + "\n"
|
39
|
+
end
|
40
|
+
|
41
|
+
def header
|
42
|
+
separator + format(@data.map(&:header)) + separator
|
43
|
+
end
|
44
|
+
|
45
|
+
def format(row_elements)
|
46
|
+
row_elements.inject_with_index('|') { |s, element, index| s += " #{element.to_s.rjust(@columns_width[index])} |" } + "\n"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
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: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Javier Vidal
|
@@ -15,10 +15,23 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-10-
|
18
|
+
date: 2011-10-09 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: colorize
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
22
35
|
description: Print colorful tables
|
23
36
|
email:
|
24
37
|
- zanaguara@gmail.com
|
@@ -35,6 +48,11 @@ files:
|
|
35
48
|
- colorful_tables.gemspec
|
36
49
|
- lib/colorful_tables.rb
|
37
50
|
- lib/colorful_tables/version.rb
|
51
|
+
- lib/column.rb
|
52
|
+
- lib/enumerable.rb
|
53
|
+
- lib/errors.rb
|
54
|
+
- lib/object.rb
|
55
|
+
- lib/table.rb
|
38
56
|
has_rdoc: true
|
39
57
|
homepage: ""
|
40
58
|
licenses: []
|