columnize 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/ChangeLog +30 -11
  2. data/NEWS +8 -2
  3. data/VERSION +1 -1
  4. data/lib/columnize.rb +60 -39
  5. data/test/test-columnize.rb +25 -23
  6. metadata +34 -41
data/ChangeLog CHANGED
@@ -1,22 +1,38 @@
1
- 2008-09-22 19:48 rockyb
1
+ 2009-01-01 03:55 rockyb
2
2
 
3
- * VERSION, lib/columnize.rb: Get rid of hacky $0 test.
3
+ * ChangeLog: Get ready for release 0.2
4
4
 
5
- 2007-12-10 16:25 rockyb
5
+ 2009-01-01 03:52 rockyb
6
6
 
7
- * : release 0.1
7
+ * ChangeLog, NEWS, VERSION, lib/columnize.rb,
8
+ test/test-columnize.rb: Get ready for release 0.2
9
+
10
+ 2008-03-16 21:14 rockyb
11
+
12
+ * lib/columnize.rb: *** empty log message ***
13
+
14
+ 2008-03-16 21:13 rockyb
15
+
16
+ * lib/columnize.rb: Simplify __FILE__ == $0. (rdebug now changes $0
17
+ and rcov has an option)
18
+
19
+ 2008-02-11 19:25 rockyb
20
+
21
+ * lib/columnize.rb: Remove 1.9's shadow warning
8
22
 
9
23
  2007-12-09 17:33 rockyb
10
24
 
11
- * : Fix up doc a little. Make the default rake task "test".
25
+ * README, Rakefile: Fix up doc a little. Make the default rake task
26
+ "test".
12
27
 
13
28
  2007-12-09 16:49 rockyb
14
29
 
15
- * : Doc fix.
30
+ * lib/columnize.rb: Doc fix.
16
31
 
17
32
  2007-12-09 16:47 rockyb
18
33
 
19
- * : columnize.rb: Allow one to specify a separator string. Remove
34
+ * ChangeLog, lib/columnize.rb, test/test-columnize.rb:
35
+ columnize.rb: Allow one to specify a separator string. Remove
20
36
  restriction that
21
37
  all entries in the array have to be a string.
22
38
  test-columnize.rb: more tests - invalid list, empty list, with
@@ -25,15 +41,18 @@
25
41
 
26
42
  2007-12-09 14:51 rockyb
27
43
 
28
- * : Typo.
44
+ * test/test-columnize.rb: Typo.
29
45
 
30
46
  2007-12-09 14:48 rockyb
31
47
 
32
- * : Property changes for Id lines and to make test-columnize.rb
48
+ * ., NEWS, README, lib/columnize.rb, test/test-columnize.rb:
49
+ Property changes for Id lines and to make test-columnize.rb
33
50
  executable.
34
51
 
35
52
  2007-12-09 14:40 rockyb
36
53
 
37
- * : Initial release of Columnize, a module to print an Array in
38
- column-sorted order.
54
+ * ., AUTHORS, COPYING, ChangeLog, NEWS, README, Rakefile, VERSION,
55
+ lib, lib/columnize.rb, test, test/test-columnize.rb: Initial
56
+ release of Columnize, a module to print an Array in column-sorted
57
+ order.
39
58
 
data/NEWS CHANGED
@@ -1,6 +1,12 @@
1
- 0.1
1
+ 0.2.1
2
+ 12-31-08
3
+ Add ability to run columns horizontally
4
+
5
+ 0.2
6
+ - Minor - get rid of hacky $0 test
2
7
 
8
+ 0.1
3
9
  - Initial release of Columnize, a module to print an Array in
4
10
  column-sorted order
5
11
 
6
- $Id: NEWS 9 2007-12-09 14:48:00Z rockyb $
12
+ $Id: NEWS 155 2009-01-01 04:15:10Z rockyb $
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2
1
+ 0.2.1
data/lib/columnize.rb CHANGED
@@ -1,4 +1,4 @@
1
- # $Id: columnize.rb 145 2008-09-22 19:48:39Z rockyb $
1
+ # $Id: columnize.rb 150 2009-01-01 03:52:54Z rockyb $
2
2
  #
3
3
  # Copyright (C) 2007, 2008 Rocky Bernstein <rockyb@rubyforge.net>
4
4
  #
@@ -22,43 +22,62 @@
22
22
  #
23
23
  # Display a list of strings as a compact set of columns.
24
24
  #
25
- # Each column is only as wide as necessary.
26
- # By default, columns are separated by two spaces (one was not legible enough).
27
- # Adapted from the routine of the same name in cmd.py
25
+ # For example, for a line width of 4 characters (arranged vertically):
26
+ # ['1', '2,', '3', '4'] => '1 3\n2 4\n'
27
+ #
28
+ # or arranged horizontally:
29
+ # ['1', '2,', '3', '4'] => '1 2\n3 4\n'
30
+ #
31
+ # Each column is only as wide as necessary. By default, columns are
32
+ # separated by two spaces (one was not legible enough).
33
+
34
+
35
+ # Adapted from
36
+ # the routine of the same name in cmd.py
28
37
 
29
38
  module Columnize
30
39
 
31
- # Return a string with embedded newlines (\n) arranging +list+ in
40
+ # Return a string with embedded newlines (\n) arranging +list+ in
32
41
  # column-order so that each line is no larger than +displaywidth+.
33
42
  # If +list+ is not an array, the empty string, '', is returned.
34
- # +colsep+ contains the string to use to separate entries.
35
- def columnize(list, displaywidth=80, colsep = ' ')
43
+ # +colsep+ contains the string to use to separate entries. Normally,
44
+ # consecutive items go down from the top to bottom from the
45
+ # left-most column to the right-most. If +arrange_vertical+ is set
46
+ # false, consecutive items will go across, left to right, top to
47
+ # bottom.
48
+ def columnize(list, displaywidth=80, colsep = ' ', arrange_vertical=true)
36
49
  if not list.is_a?(Array)
37
50
  return ''
38
51
  end
39
52
  if list.size == 0
40
53
  return "<empty>\n"
41
54
  end
42
- l = list.map{|l| l.to_s}
55
+ l = list.map{|li| li.to_s}
43
56
  if 1 == l.size
44
57
  return "#{l[0]}\n"
45
58
  end
46
59
  # Consider arranging list in 1 rows total, then 2 rows...
47
60
  # Stop when at the smallest number of rows which
48
61
  # can be arranged less than the display width.
49
- nrows = ncols = 0
62
+ nrows = 0; ncols = l.size
63
+
64
+ if arrange_vertical
65
+ array_index = lambda {|nrows, row, col| nrows*col + row }
66
+ else
67
+ array_index = lambda {|nrows, row, col| nrows*row + col }
68
+ end
69
+
50
70
  colwidths = []
51
71
  1.upto(l.size) do
52
72
  colwidths = []
53
73
  nrows += 1
54
-
55
- ncols = (l.size + nrows-1) / nrows
56
74
  totwidth = -colsep.length
75
+ col = 0
57
76
  0.upto(ncols-1) do |col|
58
77
  # get max column width for this column
59
78
  colwidth = 0
60
79
  0.upto(nrows-1) do |row|
61
- i = row + nrows*col # [rows, cols]
80
+ i = array_index.call(nrows, row, col)
62
81
  if i >= l.size
63
82
  break
64
83
  end
@@ -67,6 +86,7 @@ module Columnize
67
86
  colwidths << colwidth
68
87
  totwidth += colwidth + colsep.length
69
88
  if totwidth > displaywidth
89
+ ncols = col
70
90
  break
71
91
  end
72
92
  end
@@ -82,27 +102,34 @@ module Columnize
82
102
  0.upto(nrows-1) do |row|
83
103
  texts = []
84
104
  0.upto(ncols-1) do |col|
85
- i = row + nrows*col
105
+ i = array_index.call(nrows, row, col)
86
106
  if i >= l.size
87
- x = ""
107
+ if arrange_vertical:
108
+ x = ""
109
+ else
110
+ break
111
+ end
88
112
  else
89
113
  x = l[i]
90
- end
91
- texts << x
114
+ end
115
+ texts << x
92
116
  end
93
117
  while texts and texts[-1] == ''
94
- texts = texts[0..-2]
118
+ texts = texts[0..-2]
95
119
  end
96
- 0.upto(texts.size-1) do |col|
97
- texts[col] = texts[col].ljust(colwidths[col])
120
+ if texts.size > 0
121
+ 0.upto(texts.size-1) do |col|
122
+ texts[col] = texts[col].ljust(colwidths[col]) if
123
+ colwidths[col]
124
+ end
125
+ s += "%s\n" % texts.join(colsep)
98
126
  end
99
- s += "%s\n" % texts.join(colsep)
100
127
  end
101
128
  return s
102
129
  end
103
130
  module_function :columnize
104
131
  end
105
- if __FILE__ == $0
132
+ if __FILE__ == $0
106
133
  #
107
134
  puts Columnize::columnize(5)
108
135
  include Columnize
@@ -110,22 +137,16 @@ if __FILE__ == $0
110
137
  puts columnize(["a", 2, "c"], 10, ', ')
111
138
  puts columnize(["oneitem"])
112
139
  puts columnize(["one", "two", "three"])
113
- puts columnize([
114
- "one", "two", "three",
115
- "4ne", "5wo", "6hree",
116
- "7ne", "8wo", "9hree",
117
- "10e", "11o", "12ree",
118
- "13e", "14o", "15ree",
119
- "16e", "17o", "18ree",
120
- "19e", "20o", "21ree",
121
- "22e", "23o", "24ree",
122
- "25e", "26o", "27ree",
123
- "28e", "29o", "30ree",
124
- "31e", "32o", "33ree",
125
- "34e", "35o", "36ree",
126
- "37e", "38o", "39ree",
127
- "40e", "41o", "42ree",
128
- "43e", "44o", "45ree",
129
- "46e", "47o", "48ree",
130
- "one", "two", "three"])
140
+ data = ["one", "two", "three",
141
+ "for", "five", "six",
142
+ "seven", "eight", "nine",
143
+ "ten", "eleven", "twelve",
144
+ "thirteen", "fourteen", "fifteen",
145
+ "sixteen", "seventeen", "eightteen",
146
+ "nineteen", "twenty", "twentyone",
147
+ "twentytwo", "twentythree", "twentyfour",
148
+ "twentyfive","twentysix", "twentyseven"]
149
+
150
+ puts columnize(data)
151
+ puts columnize(data, 80, ' ', false)
131
152
  end
@@ -14,32 +14,34 @@ class TestColumnize < Test::Unit::TestCase
14
14
  assert_equal("1, 2, 3\n",
15
15
  Columnize::columnize([1, 2, 3], 10, ', '))
16
16
  assert_equal("", columnize(5))
17
+ assert_equal("1 3\n2 4\n",
18
+ columnize(['1', '2', '3', '4'], 4))
19
+ assert_equal("1 2\n3 4\n",
20
+ columnize(['1', '2', '3', '4'], 4, ' ', false))
17
21
  assert_equal("<empty>\n", columnize([]))
18
22
  assert_equal("oneitem\n", columnize(["oneitem"]))
23
+ data = ["one", "two", "three",
24
+ "for", "five", "six",
25
+ "seven", "eight", "nine",
26
+ "ten", "eleven", "twelve",
27
+ "thirteen", "fourteen", "fifteen",
28
+ "sixteen", "seventeen", "eightteen",
29
+ "nineteen", "twenty", "twentyone",
30
+ "twentytwo", "twentythree", "twentyfour",
31
+ "twentyfive","twentysix", "twentyseven"]
32
+
33
+ assert_equal(
34
+ "one two three for five six \n" +
35
+ "seven eight nine ten eleven twelve \n" +
36
+ "thirteen fourteen fifteen sixteen seventeen eightteen \n" +
37
+ "nineteen twenty twentyone twentytwo twentythree twentyfour\n" +
38
+ "twentyfive twentysix twentyseven\n", columnize(data, 80, ' ', false))
39
+
19
40
  assert_equal(
20
- "one 6hree 11o 16e 21ree 26o 31e 36ree 41o 46e three\n" +
21
- "two 7ne 12ree 17o 22e 27ree 32o 37e 42ree 47o \n" +
22
- "three 8wo 13e 18ree 23o 28e 33ree 38o 43e 48ree\n" +
23
- "4ne 9hree 14o 19e 24ree 29o 34e 39ree 44o one \n" +
24
- "5wo 10e 15ree 20o 25e 30ree 35o 40e 45ree two \n",
25
- columnize([
26
- "one", "two", "three",
27
- "4ne", "5wo", "6hree",
28
- "7ne", "8wo", "9hree",
29
- "10e", "11o", "12ree",
30
- "13e", "14o", "15ree",
31
- "16e", "17o", "18ree",
32
- "19e", "20o", "21ree",
33
- "22e", "23o", "24ree",
34
- "25e", "26o", "27ree",
35
- "28e", "29o", "30ree",
36
- "31e", "32o", "33ree",
37
- "34e", "35o", "36ree",
38
- "37e", "38o", "39ree",
39
- "40e", "41o", "42ree",
40
- "43e", "44o", "45ree",
41
- "46e", "47o", "48ree",
42
- "one", "two", "three"]))
41
+ "one five nine thirteen seventeen twentyone twentyfive \n" +
42
+ "two six ten fourteen eightteen twentytwo twentysix \n" +
43
+ "three seven eleven fifteen nineteen twentythree twentyseven\n" +
44
+ "for eight twelve sixteen twenty twentyfour \n", columnize(data))
43
45
 
44
46
  end
45
47
  end
metadata CHANGED
@@ -1,27 +1,33 @@
1
1
  --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
2
4
  name: columnize
3
5
  version: !ruby/object:Gem::Version
4
- version: "0.2"
6
+ version: 0.2.1
7
+ date: 2008-12-31 00:00:00 -05:00
8
+ summary: Read file with caching
9
+ require_paths:
10
+ - lib
11
+ email: rockyb@rubyforge.net
12
+ homepage: http://rubyforge.org/projects/rocky-hacks/columnize
13
+ rubyforge_project: rocky-hacks
14
+ description: Columnize is a module for reading and caching lines. This may be useful for example in a debugger where the same lines are shown many times.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
5
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
6
29
  authors:
7
30
  - R. Bernstein
8
- autorequire:
9
- bindir: bin
10
- cert_chain: []
11
-
12
- date: 2008-09-22 00:00:00 -04:00
13
- default_executable:
14
- dependencies: []
15
-
16
- description: Columnize is a module for reading and caching lines. This may be useful for example in a debugger where the same lines are shown many times.
17
- email: rockyb@rubyforge.net
18
- executables: []
19
-
20
- extensions: []
21
-
22
- extra_rdoc_files:
23
- - README
24
- - lib/columnize.rb
25
31
  files:
26
32
  - AUTHORS
27
33
  - COPYING
@@ -32,31 +38,18 @@ files:
32
38
  - VERSION
33
39
  - lib/columnize.rb
34
40
  - test/test-columnize.rb
35
- has_rdoc: true
36
- homepage: http://rubyforge.org/projects/rocky-hacks/columnize
37
- post_install_message:
41
+ test_files: []
42
+
38
43
  rdoc_options: []
39
44
 
40
- require_paths:
41
- - lib
42
- required_ruby_version: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 1.8.2
47
- version:
48
- required_rubygems_version: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: "0"
53
- version:
45
+ extra_rdoc_files:
46
+ - README
47
+ - lib/columnize.rb
48
+ executables: []
49
+
50
+ extensions: []
51
+
54
52
  requirements: []
55
53
 
56
- rubyforge_project: rocky-hacks
57
- rubygems_version: 1.0.1
58
- signing_key:
59
- specification_version: 2
60
- summary: Read file with caching
61
- test_files: []
54
+ dependencies: []
62
55