colorful_tables 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ColorfulTables
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,8 +1,11 @@
1
1
  module ColorfulTables
2
+
2
3
  require 'colorize'
3
4
  require 'object'
4
5
  require 'enumerable'
5
6
  require 'errors'
6
7
  require 'table'
7
8
  require 'column'
9
+ require 'misc'
10
+
8
11
  end
data/lib/column.rb CHANGED
@@ -22,7 +22,7 @@ module ColorfulTables
22
22
  def calculate_width
23
23
  # We have to add the header in the calculation
24
24
  data_and_header = self.data + [self.header]
25
- data_and_header.map { |c| c.to_s.length }.max
25
+ data_and_header.map { |cell| ColorfulTables.truncate_if_float(cell).to_s.length }.max
26
26
  end
27
27
 
28
28
  end
data/lib/misc.rb ADDED
@@ -0,0 +1,11 @@
1
+ module ColorfulTables
2
+
3
+ DIGITS_AFTER_DECIMAL_POINT = 2
4
+
5
+ class << self
6
+ def truncate_if_float(element)
7
+ element.is_a?(Float) ? ( "%.#{DIGITS_AFTER_DECIMAL_POINT}f" % element ) : element
8
+ end
9
+ end
10
+
11
+ end
data/lib/table.rb CHANGED
@@ -55,15 +55,19 @@ module ColorfulTables
55
55
  end
56
56
 
57
57
  def format(row_elements)
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"
58
+ output = row_elements.inject_with_index(@border_chars[:vertical]) do |s, element, index|
59
+ value = ColorfulTables.truncate_if_float(element)
60
+ color = @columns_options[index][:data_color].is_a?(Proc) ? @columns_options[index][:data_color].call(value) : @columns_options[index][:data_color]
61
+ s += " #{value.to_s.rjust(@columns_width[index]).colorize(color)} #{@border_chars[:vertical]}"
62
+ end
63
+ output + "\n"
59
64
  end
60
65
 
61
66
  def calculate_border_chars
62
67
  color = @options[:border_color]
63
- {:vertical => '|'.colorize(color),
64
- :horizontal => '-'.colorize(color),
65
- :cross => '+'.colorize(color)
66
- }
68
+ { :vertical => '|'.colorize(color),
69
+ :horizontal => '-'.colorize(color),
70
+ :cross => '+'.colorize(color) }
67
71
  end
68
72
 
69
73
  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: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Vidal
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-09 00:00:00 +02:00
18
+ date: 2011-10-11 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -51,6 +51,7 @@ files:
51
51
  - lib/column.rb
52
52
  - lib/enumerable.rb
53
53
  - lib/errors.rb
54
+ - lib/misc.rb
54
55
  - lib/object.rb
55
56
  - lib/table.rb
56
57
  has_rdoc: true