colorful_tables 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,8 @@ module ColorfulTables
4
4
 
5
5
  DEFAULT_OPTIONS = {
6
6
  :data_color => :light_blue,
7
- :header_color => :light_blue
7
+ :header_color => :light_blue,
8
+ :digits_after_decimal_point => 4
8
9
  }
9
10
 
10
11
  attr_accessor :header, :data, :options
@@ -14,7 +15,7 @@ module ColorfulTables
14
15
  def initialize(header, data, options = {})
15
16
  @header = header
16
17
  @data = data
17
- @options = DEFAULT_OPTIONS.merge(options)
18
+ @options = options.reverse_merge!(DEFAULT_OPTIONS)
18
19
  @width ||= calculate_width
19
20
  end
20
21
 
@@ -23,7 +24,7 @@ module ColorfulTables
23
24
  def calculate_width
24
25
  # We have to add the header in the calculation
25
26
  data_and_header = self.data + [self.header]
26
- data_and_header.map { |cell| ColorfulTables.truncate_if_float(cell).to_s.length }.max
27
+ data_and_header.map { |cell| ColorfulTables.truncate_if_decimals(cell, @options[:digits_after_decimal_point]).to_s.length }.max
27
28
  end
28
29
 
29
30
  end
@@ -1,10 +1,8 @@
1
1
  module ColorfulTables
2
2
 
3
- DIGITS_AFTER_DECIMAL_POINT = 2
4
-
5
3
  class << self
6
- def truncate_if_float(element)
7
- element.is_a?(Float) ? ( "%.#{DIGITS_AFTER_DECIMAL_POINT}f" % element ) : element
4
+ def truncate_if_decimals(element, digits_after_decimal_point)
5
+ (element.is_a?(Float) || element.is_a?(BigDecimal)) ? ( "%.#{digits_after_decimal_point}f" % element ) : element
8
6
  end
9
7
  end
10
8
 
@@ -7,7 +7,8 @@ module ColorfulTables
7
7
  class Table
8
8
 
9
9
  DEFAULT_OPTIONS = {
10
- :border_color => :light_blue
10
+ :border_color => :light_blue,
11
+ :digits_after_decimal_point => 4
11
12
  }
12
13
 
13
14
  attr_accessor :data, :options
@@ -15,11 +16,11 @@ module ColorfulTables
15
16
  def initialize(data, options = {})
16
17
  #raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
17
18
  @data = data
18
- @options = DEFAULT_OPTIONS.merge(options)
19
+ @options = options.reverse_merge!(DEFAULT_OPTIONS)
19
20
  @border_chars ||= calculate_border_chars
20
21
  @columns_width ||= calculate_columns_width
21
22
  @columns_options ||= calculate_columns_options
22
- @terminal_heigth ||= ColorfulTables.detect_terminal_size[1]
23
+ @terminal_height ||= ColorfulTables.detect_terminal_size[1]
23
24
  @transposed_data ||= self.data.map(&:data).transpose
24
25
  end
25
26
 
@@ -56,7 +57,7 @@ module ColorfulTables
56
57
  end
57
58
 
58
59
  def separator
59
- # + 2 is because we have to add an space at the begining and another at the end inside a cell
60
+ # + 2 is because we have to add an space at the beginning and another at the end inside a cell
60
61
  self.data.inject("#{@border_chars[:cross]}") { |s, column| s += (("#{@border_chars[:horizontal]}" * (column.width + 2)) + "#{@border_chars[:cross]}") } + "\n"
61
62
  end
62
63
 
@@ -66,7 +67,7 @@ module ColorfulTables
66
67
 
67
68
  def format(row_elements, in_header = false)
68
69
  output = row_elements.inject_with_index(@border_chars[:vertical]) do |s, element, index|
69
- value = ColorfulTables.truncate_if_float(element)
70
+ value = ColorfulTables.truncate_if_decimals(element, @options[:digits_after_decimal_point])
70
71
  color = if in_header
71
72
  @columns_options[index][:header_color]
72
73
  else
@@ -91,7 +92,7 @@ module ColorfulTables
91
92
  end
92
93
 
93
94
  def rows_per_page
94
- @terminal_heigth - 5
95
+ @terminal_height - 5
95
96
  end
96
97
 
97
98
  def total_rows
@@ -1,3 +1,3 @@
1
1
  module ColorfulTables
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  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: 15
5
- prerelease: false
4
+ hash: 11
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Vidal
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-14 00:00:00 +02:00
19
- default_executable:
18
+ date: 2012-05-30 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: colorize
@@ -55,7 +54,6 @@ files:
55
54
  - lib/colorful_tables/table.rb
56
55
  - lib/colorful_tables/util.rb
57
56
  - lib/colorful_tables/version.rb
58
- has_rdoc: true
59
57
  homepage: ""
60
58
  licenses: []
61
59
 
@@ -85,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
83
  requirements: []
86
84
 
87
85
  rubyforge_project: colorful_tables
88
- rubygems_version: 1.3.7
86
+ rubygems_version: 1.7.2
89
87
  signing_key:
90
88
  specification_version: 3
91
89
  summary: Print colorful tables