terminal-table 1.5.2 → 1.6.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.
@@ -1,13 +1,12 @@
1
-
2
1
  module Terminal
3
2
  class Table
4
-
3
+
5
4
  attr_reader :title
6
5
  attr_reader :headings
7
-
6
+
8
7
  ##
9
8
  # Generates a ASCII table with the given _options_.
10
-
9
+
11
10
  def initialize options = {}, &block
12
11
  @column_widths = []
13
12
  self.style = options.fetch :style, {}
@@ -16,10 +15,10 @@ module Terminal
16
15
  self.title = options.fetch :title, nil
17
16
  yield_or_eval(&block) if block
18
17
  end
19
-
18
+
20
19
  ##
21
20
  # Align column _n_ to the given _alignment_ of :center, :left, or :right.
22
-
21
+
23
22
  def align_column n, alignment
24
23
  r = rows
25
24
  column(n).each_with_index do |col, i|
@@ -27,10 +26,10 @@ module Terminal
27
26
  cell.alignment = alignment unless cell.alignment?
28
27
  end
29
28
  end
30
-
29
+
31
30
  ##
32
- # Add a row.
33
-
31
+ # Add a row.
32
+
34
33
  def add_row array
35
34
  row = array == :separator ? Separator.new(self) : Row.new(self, array)
36
35
  @rows << row
@@ -44,58 +43,58 @@ module Terminal
44
43
  def add_separator
45
44
  self << :separator
46
45
  end
47
-
46
+
48
47
  def cell_spacing
49
48
  cell_padding + style.border_y.length
50
49
  end
51
-
50
+
52
51
  def cell_padding
53
52
  style.padding_left + style.padding_right
54
53
  end
55
-
54
+
56
55
  ##
57
56
  # Return column _n_.
58
-
57
+
59
58
  def column n, method = :value, array = rows
60
- array.map { |row|
59
+ array.map { |row|
61
60
  cell = row[n]
62
61
  cell && method ? cell.__send__(method) : cell
63
- }.compact
62
+ }.compact
64
63
  end
65
-
64
+
66
65
  ##
67
66
  # Return _n_ column including headings.
68
-
67
+
69
68
  def column_with_headings n, method = :value
70
69
  column n, method, headings_with_rows
71
70
  end
72
-
71
+
73
72
  ##
74
73
  # Return columns.
75
-
74
+
76
75
  def columns
77
- (0...number_of_columns).map { |n| column n }
76
+ (0...number_of_columns).map { |n| column n }
78
77
  end
79
-
78
+
80
79
  ##
81
80
  # Return length of column _n_.
82
-
81
+
83
82
  def column_width n
84
83
  width = @column_widths[n] || 0
85
84
  width + additional_column_widths[n].to_i
86
85
  end
87
86
  alias length_of_column column_width # for legacy support
88
-
87
+
89
88
  ##
90
89
  # Return total number of columns available.
91
-
90
+
92
91
  def number_of_columns
93
92
  headings_with_rows.map { |r| r.cells.size }.max
94
93
  end
95
94
 
96
95
  ##
97
96
  # Set the headings
98
-
97
+
99
98
  def headings= arrays
100
99
  arrays = [arrays] unless arrays.first.is_a?(Array)
101
100
  @headings = arrays.map do |array|
@@ -107,7 +106,7 @@ module Terminal
107
106
 
108
107
  ##
109
108
  # Render the table.
110
-
109
+
111
110
  def render
112
111
  separator = Separator.new(self)
113
112
  buffer = [separator]
@@ -121,19 +120,23 @@ module Terminal
121
120
  buffer << separator
122
121
  end
123
122
  end
124
- buffer += @rows
125
- buffer << separator
126
- buffer.map { |r| r.render }.join("\n")
123
+ if style.all_separators
124
+ buffer += @rows.product([separator]).flatten
125
+ else
126
+ buffer += @rows
127
+ buffer << separator
128
+ end
129
+ buffer.map { |r| style.margin_left + r.render }.join("\n")
127
130
  end
128
131
  alias :to_s :render
129
-
132
+
130
133
  ##
131
134
  # Return rows without separator rows.
132
135
 
133
136
  def rows
134
137
  @rows.reject { |row| row.is_a? Separator }
135
138
  end
136
-
139
+
137
140
  def rows= array
138
141
  @rows = []
139
142
  array.each { |arr| self << arr }
@@ -142,16 +145,16 @@ module Terminal
142
145
  def style=(options)
143
146
  style.apply options
144
147
  end
145
-
148
+
146
149
  def style
147
150
  @style ||= Style.new
148
151
  end
149
-
152
+
150
153
  def title=(title)
151
154
  @title = title
152
155
  recalc_column_widths Row.new(self, [title_cell_options])
153
156
  end
154
-
157
+
155
158
  ##
156
159
  # Check if _other_ is equal to self. _other_ is considered equal
157
160
  # if it contains the same headings and rows.
@@ -163,11 +166,11 @@ module Terminal
163
166
  end
164
167
 
165
168
  private
166
-
169
+
167
170
  def columns_width
168
171
  @column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
169
172
  end
170
-
173
+
171
174
  def additional_column_widths
172
175
  return [] if style.width.nil?
173
176
  spacing = style.width - columns_width
@@ -181,7 +184,7 @@ module Terminal
181
184
  arr
182
185
  end
183
186
  end
184
-
187
+
185
188
  def recalc_column_widths row
186
189
  return if row.is_a? Separator
187
190
  i = 0
@@ -202,14 +205,14 @@ module Terminal
202
205
  end
203
206
  end
204
207
  end
205
-
208
+
206
209
  ##
207
210
  # Return headings combined with rows.
208
-
211
+
209
212
  def headings_with_rows
210
213
  @headings + rows
211
214
  end
212
-
215
+
213
216
  def yield_or_eval &block
214
217
  return unless block
215
218
  if block.arity > 0
@@ -1,9 +1,9 @@
1
- module Terminal
2
- class Table
3
- module TableHelper
4
- def table headings = [], *rows, &block
5
- Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
6
- end
7
- end
8
- end
9
- end
1
+ module Terminal
2
+ class Table
3
+ module TableHelper
4
+ def table headings = [], *rows, &block
5
+ Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Terminal
2
2
  class Table
3
- VERSION = '1.5.2'
3
+ VERSION = '1.6.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-29 00:00:00.000000000 Z
12
+ date: 2016-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -89,7 +89,6 @@ extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
- - ".rvmrc"
93
92
  - Gemfile
94
93
  - History.rdoc
95
94
  - Manifest
@@ -127,8 +126,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
126
  version: '0'
128
127
  requirements: []
129
128
  rubyforge_project:
130
- rubygems_version: 2.4.8
129
+ rubygems_version: 2.5.1
131
130
  signing_key:
132
131
  specification_version: 4
133
132
  summary: Simple, feature rich ascii table generation library
134
133
  test_files: []
134
+ has_rdoc:
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm 1.8.7@terminal-table --create