columnize 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/Makefile CHANGED
@@ -1,8 +1,11 @@
1
1
  # I'll admit it -- I'm an absent-minded old-timer who has trouble
2
2
  # learning new tricks.
3
3
  test: check
4
+
5
+ #: Same as "make check"
4
6
  all: check
5
7
  true
6
8
 
9
+ #: Same as corresponding rake task
7
10
  %:
8
11
  rake $@
data/NEWS CHANGED
@@ -1,3 +1,6 @@
1
+ 0.3.5 Nov 24, 2011
2
+ - Handle situation where an array element is larger than the display width.
3
+
1
4
  0.3.4 July 4, 2011
2
5
 
3
6
  - Change to Ruby License
@@ -0,0 +1,84 @@
1
+ Columnize - Format an Array as a Column-aligned String
2
+ ============================================================================
3
+
4
+ In showing a long lists, sometimes one would prefer to see the value
5
+ arranged aligned in columns. Some examples include listing methods of
6
+ an object, listing debugger commands, or showing a numeric array with data
7
+ aligned.
8
+
9
+ Setup
10
+ -----
11
+
12
+ $ irb
13
+ >> a = (1..10).to_a
14
+ => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
15
+ >> require 'columnize'
16
+ => true
17
+ >> include Columnize
18
+ => Object
19
+ >> g = %w(bibrons golden madascar leopard mourning suras tokay)
20
+ => ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"]
21
+
22
+ With numeric data
23
+ -----------------
24
+
25
+ columnize(a)
26
+ => "1 2 3 4 5 6 7 8 9 10\n"
27
+ >> puts Columnize::columnize(a, :arrange_array => true, :displaywidth => 10)
28
+ [1, 2
29
+ 3, 4
30
+ 5, 6
31
+ 7, 8
32
+ 9, 10
33
+ ]
34
+ => nil
35
+ >> puts Columnize::columnize(a, :arrange_array => true, :displaywidth => 20)
36
+ [1, 2, 3, 4, 5, 6
37
+ 7, 8, 9, 10
38
+ ]
39
+
40
+ With String data
41
+ ----------------
42
+
43
+ >> puts columnize g, :displaywidth => 15
44
+ bibrons suras
45
+ golden tokay
46
+ madascar
47
+ leopard
48
+ mourning
49
+ => nil
50
+
51
+ >> puts columnize g, {:displaywidth => 19, :colsep => ' | '}
52
+ bibrons | suras
53
+ golden | tokay
54
+ madascar
55
+ leopard
56
+ mourning
57
+ => nil
58
+
59
+ >> puts columnize g, {:displaywidth => 18, :colsep => ' | ', :ljust=>false}
60
+
61
+ bibrons | mourning
62
+ golden | suras
63
+ madascar | tokay
64
+ leopard
65
+
66
+ Credits
67
+ -------
68
+
69
+ This is adapted from a method of the same name from Python's cmd module.
70
+
71
+ Other stuff
72
+ -----------
73
+
74
+ Author: Rocky Bernstein <rockyb@rubyforge.org>
75
+
76
+ License: Copyright (c) 2011 Rocky Bernstein
77
+
78
+ Warranty
79
+ --------
80
+
81
+ This program is distributed in the hope that it will be useful,
82
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
83
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84
+ GNU General Public License for more details.
@@ -42,10 +42,10 @@ require "columnize"
42
42
  spec.summary = 'Module to format an Array as an Array of String aligned in columns'
43
43
  spec.version = Columnize::VERSION
44
44
  spec.has_rdoc = true
45
- spec.extra_rdoc_files = %w(README lib/columnize.rb COPYING)
45
+ spec.extra_rdoc_files = %w(README.md lib/columnize.rb COPYING)
46
46
 
47
47
  # Make the readme file the start page for the generated html
48
- spec.rdoc_options += %w(--verbose --main README)
48
+ spec.rdoc_options += %w(--main README)
49
49
  spec.rdoc_options += ['--title', "Columnize #{Columnize::VERSION} Documentation"]
50
50
 
51
51
  end
@@ -1,7 +1,5 @@
1
1
  # Module to format an Array as an Array of String aligned in columns.
2
2
  #
3
- # :main:README.md
4
- #
5
3
  # == Summary
6
4
  # Display a list of strings as a compact set of columns.
7
5
  #
@@ -36,7 +34,7 @@ module Columnize
36
34
  DEFAULT_OPTS = {
37
35
  :arrange_array => false,
38
36
  :arrange_vertical => true,
39
- :array_prefix => false,
37
+ :array_prefix => '',
40
38
  :array_suffix => '',
41
39
  :colsep => ' ',
42
40
  :displaywidth => 80,
@@ -115,22 +113,21 @@ module Columnize
115
113
  def columnize(*args)
116
114
 
117
115
  list, opts = parse_columnize_options(args)
116
+
118
117
  # Some degenerate cases
119
- if not list.is_a?(Array)
120
- return ''
121
- end
122
- if list.size == 0
123
- return "<empty>\n"
124
- end
118
+ return '' if not list.is_a?(Array)
119
+ return "<empty>\n" if list.empty?
125
120
  l = list.map{|li| li.to_s}
126
- if 1 == l.size
127
- return "#{l[0]}\n"
128
- end
121
+ return "%s%s%s\n" % [opts[:array_prefix], l[0],
122
+ opts[:array_suffix]] if 1 == l.size
129
123
 
130
124
  nrows = ncols = 0 # Make nrows, ncols have more global scope
131
125
  colwidths = [] # Same for colwidths
132
- opts[:displaywidth] = [4,
133
- opts[:displaywidth] - opts[:lineprefix].length].max
126
+ if opts[:displaywidth] - opts[:lineprefix].length < 4
127
+ opts[:displaywidth] = opts[:lineprefix].length + 4
128
+ else
129
+ opts[:displaywidth] -= opts[:lineprefix].length
130
+ end
134
131
  if opts[:arrange_vertical]
135
132
  array_index = lambda {|num_rows, row, col| num_rows*col + row }
136
133
  # Try every row count from 1 upwards
@@ -140,57 +137,51 @@ module Columnize
140
137
  colwidths = []
141
138
  totwidth = -opts[:colsep].length
142
139
 
143
- 0.upto(ncols-1) do |_col|
144
- col = _col
140
+ 0.upto(ncols-1) do |col|
145
141
  # get max column width for this column
146
142
  colwidth = 0
147
143
  0.upto(nrows-1) do |_row|
148
144
  row = _row
149
145
  i = array_index.call(nrows, row, col)
150
- if i >= l.size
151
- break
152
- end
146
+ break if i >= l.size
153
147
  colwidth = [colwidth, cell_size(l[i], opts[:term_adjust])].max
154
148
  end
155
- colwidths << colwidth
149
+ colwidths.push(colwidth)
156
150
  totwidth += colwidth + opts[:colsep].length
157
151
  if totwidth > opts[:displaywidth]
158
152
  ncols = col
159
153
  break
160
154
  end
161
155
  end
162
- if totwidth <= opts[:displaywidth]
163
- break
164
- end
156
+ break if totwidth <= opts[:displaywidth]
165
157
  end
166
- # The smallest number of rows computed and the
167
- # max widths for each column has been obtained.
168
- # Now we just have to format each of the
169
- # rows.
158
+ ncols = 1 if ncols < 1
159
+ nrows = l.size if ncols == 1
160
+ # The smallest number of rows computed and the max widths for
161
+ # each column has been obtained. Now we just have to format
162
+ # each of the rows.
170
163
  s = ''
171
164
  0.upto(nrows-1) do |_row|
172
165
  row = _row
173
166
  texts = []
174
- 0.upto(ncols-1) do |_col|
175
- col = _col
167
+ 0.upto(ncols-1) do |col|
176
168
  i = array_index.call(nrows, row, col)
177
169
  if i >= l.size
178
170
  x = ''
179
171
  else
180
172
  x = l[i]
181
173
  end
182
- texts << x
183
- end
184
- while texts and texts[-1] == ''
185
- texts = texts[0..-2]
174
+ texts.push(x)
186
175
  end
176
+ texts.pop while !texts.empty? and texts[-1] == ''
187
177
  if texts.size > 0
188
- 0.upto(texts.size-1) do |_col|
189
- col = _col
190
- if opts[:ljust]
178
+ 0.upto(texts.size-1) do |col|
179
+ unless ncols == 1 && opts[:ljust]
180
+ if opts[:ljust]
191
181
  texts[col] = texts[col].ljust(colwidths[col])
192
- else
182
+ else
193
183
  texts[col] = texts[col].rjust(colwidths[col])
184
+ end
194
185
  end
195
186
  end
196
187
  s += "%s%s\n" % [opts[:lineprefix], texts.join(opts[:colsep])]
@@ -199,10 +190,10 @@ module Columnize
199
190
  return s
200
191
  else
201
192
  array_index = lambda {|num_rows, row, col| ncols*(row-1) + col }
202
- # Try every column count from size downwards
203
- # Assign to make enlarge scope of loop variables
193
+ # Assign to make enlarge scope of loop variables.
204
194
  totwidth = i = rounded_size = 0
205
- l.size.downto(0) do |_ncols|
195
+ # Try every column count from size downwards.
196
+ l.size.downto(1) do |_ncols|
206
197
  ncols = _ncols
207
198
  # Try every row count from 1 upwards
208
199
  min_rows = (l.size+ncols-1) / ncols
@@ -212,23 +203,17 @@ module Columnize
212
203
  colwidths = []
213
204
  totwidth = -opts[:colsep].length
214
205
  colwidth = row = 0
215
- 0.upto(ncols-1) do |_col|
216
- col = _col
206
+ 0.upto(ncols-1) do |col|
217
207
  # get max column width for this column
218
208
  1.upto(nrows) do |_row|
219
209
  row = _row
220
210
  i = array_index.call(nrows, row, col)
221
- if i >= rounded_size
222
- break
223
- elsif i < l.size
224
- colwidth = [colwidth, cell_size(l[i], opts[:term_adjust])].max
225
- end
211
+ break if i >= l.size
212
+ colwidth = [colwidth, cell_size(l[i], opts[:term_adjust])].max
226
213
  end
227
- colwidths << colwidth
214
+ colwidths.push(colwidth)
228
215
  totwidth += colwidth + opts[:colsep].length
229
- if totwidth > opts[:displaywidth]
230
- break
231
- end
216
+ break if totwidth > opts[:displaywidth];
232
217
  end
233
218
  if totwidth <= opts[:displaywidth]
234
219
  # Found the right nrows and ncols
@@ -239,16 +224,19 @@ module Columnize
239
224
  break
240
225
  end
241
226
  end
242
- if totwidth <= opts[:displaywidth] and i >= rounded_size-1
243
- break
244
- end
227
+ break if totwidth <= opts[:displaywidth] and i >= rounded_size-1
245
228
  end
246
- # The smallest number of rows computed and the
247
- # max widths for each column has been obtained.
248
- # Now we just have to format each of the
249
- # rows.
229
+ ncols = 1 if ncols < 1
230
+ nrows = l.size if ncols == 1
231
+ # The smallest number of rows computed and the max widths for
232
+ # each column has been obtained. Now we just have to format
233
+ # each of the rows.
250
234
  s = ''
251
- prefix = opts[:array_prefix] || opts[:lineprefix]
235
+ prefix = if opts[:array_prefix].empty?
236
+ opts[:lineprefix]
237
+ else
238
+ opts[:array_prefix]
239
+ end
252
240
  1.upto(nrows) do |row|
253
241
  texts = []
254
242
  0.upto(ncols-1) do |col|
@@ -258,13 +246,15 @@ module Columnize
258
246
  else
259
247
  x = l[i]
260
248
  end
261
- texts << x
249
+ texts.push(x)
262
250
  end
263
251
  0.upto(texts.size-1) do |col|
264
- if opts[:ljust]
265
- texts[col] = texts[col].ljust(colwidths[col])
266
- else
267
- texts[col] = texts[col].rjust(colwidths[col])
252
+ unless ncols == 1 && opts[:ljust]
253
+ if opts[:ljust]
254
+ texts[col] = texts[col].ljust(colwidths[col]) if ncols != 1
255
+ else
256
+ texts[col] = texts[col].rjust(colwidths[col])
257
+ end
268
258
  end
269
259
  end
270
260
  s += "%s%s\n" % [prefix, texts.join(opts[:colsep])]
@@ -279,17 +269,22 @@ if __FILE__ == $0
279
269
  #
280
270
  include Columnize
281
271
 
272
+ line = 'require "irb"';
273
+ puts cell_size(line, true);
274
+ puts cell_size(line, false);
275
+
282
276
  [[4, 4], [4, 7], [100, 80]].each do |width, num|
283
- data = (1..num).map{|i| i.to_s}
277
+ data = (1..num).map{|i| i}
284
278
  [[false, 'horizontal'], [true, 'vertical']].each do |bool, dir|
285
279
  puts "Width: #{width}, direction: #{dir}"
286
- print columnize(data, width, ' ', arrange_vertical=bool)
280
+ print columnize(data, :displaywidth => width, :colsep => ' ',
281
+ :arrange_vertical => bool, :ljust => :auto)
287
282
  end
288
283
  end
289
284
 
290
285
  puts Columnize::columnize(5)
291
286
  puts columnize([])
292
- puts columnize(["a", 2, "c"], 10, ', ')
287
+ puts columnize(["a", 2, "c"], :displaywidth =>10, :colsep => ', ')
293
288
  puts columnize(["oneitem"])
294
289
  puts columnize(["one", "two", "three"])
295
290
  data = ["one", "two", "three",
@@ -3,5 +3,5 @@
3
3
  # require'ing 'columnize'.
4
4
  module Columnize
5
5
  # The current version of this package
6
- VERSION = '0.3.4'
6
+ VERSION = '0.3.5'
7
7
  end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ require 'test/unit'
3
+
4
+ # Test of Columnize module
5
+ class TestIssue3 < Test::Unit::TestCase
6
+ @@TOP_SRC_DIR = File.join(File.expand_path(File.dirname(__FILE__)),
7
+ '..', 'lib')
8
+ require File.join(@@TOP_SRC_DIR, 'columnize.rb')
9
+ include Columnize
10
+ # test columnize
11
+ def test_long_column
12
+ data = ["what's", "upppppppppppppppppp"]
13
+ # Try at least one test where we give the module name explicitely.
14
+ assert_equal("what's\nupppppppppppppppppp\n",
15
+ Columnize::columnize(data, :arrange_vertical => false,
16
+ :displaywidth => 7))
17
+ assert_equal("what's\nupppppppppppppppppp\n",
18
+ Columnize::columnize(data, :arrange_vertical => true,
19
+ :displaywidth => 7))
20
+ data = ["whaaaaaat's", "up"]
21
+ assert_equal("whaaaaaat's\n up\n",
22
+ Columnize::columnize(data,
23
+ :arrange_vertical => false,
24
+ :ljust => false,
25
+ :displaywidth => 7))
26
+ assert_equal("whaaaaaat's\nup\n",
27
+ Columnize::columnize(data,
28
+ :arrange_vertical => false,
29
+ :ljust => true,
30
+ :displaywidth => 7))
31
+ assert_equal("whaaaaaat's\nup\n",
32
+ Columnize::columnize(data,
33
+ :arrange_vertical => true,
34
+ :ljust => true,
35
+ :displaywidth => 7))
36
+ end
37
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 4
9
- version: 0.3.4
8
+ - 5
9
+ version: 0.3.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - R. Bernstein
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-05 00:00:00 -04:00
17
+ date: 2011-11-24 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -32,7 +32,7 @@ executables: []
32
32
  extensions: []
33
33
 
34
34
  extra_rdoc_files:
35
- - README
35
+ - README.md
36
36
  - lib/columnize.rb
37
37
  - COPYING
38
38
  files:
@@ -42,7 +42,7 @@ files:
42
42
  - ChangeLog
43
43
  - Makefile
44
44
  - NEWS
45
- - README
45
+ - README.md
46
46
  - Rakefile
47
47
  - columnize.gemspec
48
48
  - lib/Makefile
@@ -50,6 +50,7 @@ files:
50
50
  - lib/version.rb
51
51
  - test/test-columnize.rb
52
52
  - test/test-hashparm.rb
53
+ - test/test-issue3.rb
53
54
  has_rdoc: true
54
55
  homepage: https://github.com/rocky/columnize
55
56
  licenses:
@@ -57,11 +58,10 @@ licenses:
57
58
  - GPL2
58
59
  post_install_message:
59
60
  rdoc_options:
60
- - --verbose
61
61
  - --main
62
62
  - README
63
63
  - --title
64
- - Columnize 0.3.4 Documentation
64
+ - Columnize 0.3.5 Documentation
65
65
  require_paths:
66
66
  - lib
67
67
  required_ruby_version: !ruby/object:Gem::Requirement
data/README DELETED
@@ -1,78 +0,0 @@
1
- = Columnize - Module to show an Array formatted as a String aligned in columns.
2
-
3
- == Summary
4
-
5
- In showing a long lists, sometimes one would prefer to see the value
6
- arranged aligned in columns. Some examples include listing methods of
7
- an object, listing debugger commands, or showing a numeric array with data
8
- aligned.
9
-
10
- === Setup
11
-
12
- $ irb
13
- >> a = (1..10).to_a
14
- => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
15
- >> require 'columnize'
16
- => true
17
- >> include Columnize
18
- => Object
19
- >> g = %w(bibrons golden madascar leopard mourning suras tokay)
20
- => ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"]
21
-
22
- === With numeric data
23
-
24
- columnize(a)
25
- => "1 2 3 4 5 6 7 8 9 10\n"
26
- >> puts Columnize::columnize(a, :arrange_array => true, :displaywidth => 10)
27
- [1, 2
28
- 3, 4
29
- 5, 6
30
- 7, 8
31
- 9, 10
32
- ]
33
- => nil
34
- >> puts Columnize::columnize(a, :arrange_array => true, :displaywidth => 20)
35
- [1, 2, 3, 4, 5, 6
36
- 7, 8, 9, 10
37
- ]
38
-
39
- === With String data
40
-
41
- >> puts columnize g, :displaywidth => 15
42
- bibrons suras
43
- golden tokay
44
- madascar
45
- leopard
46
- mourning
47
- => nil
48
-
49
- >> puts columnize g, {:displaywidth => 18, :colsep => ' | '}
50
- bibrons | suras
51
- golden | tokay
52
- madascar
53
- leopard
54
- mourning
55
- => nil
56
-
57
- >> puts columnize g, {:displaywidth => 18, :colsep => ' | ', :ljust=>false}
58
- bibrons | suras
59
- golden | tokay
60
- madascar
61
- leopard
62
-
63
- == Credits
64
-
65
- This is adapted from a method of the same name from Python's cmd module.
66
-
67
- == Other stuff
68
-
69
- Author:: Rocky Bernstein <rockyb@rubyforge.net>
70
- License:: Copyright (c) 2007, 2011 Rocky Bernstein
71
- Released under the GNU GPL 2 license
72
-
73
- == Warranty
74
-
75
- This program is distributed in the hope that it will be useful,
76
- but WITHOUT ANY WARRANTY; without even the implied warranty of
77
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78
- GNU General Public License for more details.