Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

started refactoring vertical.rb; it's going to be harder than horizon…

…tal, possibly
  • Loading branch information...
commit 2aad533aa5e4c74651441c41ae9bda2e28cadbb2 1 parent 382ff1f
@waslogic waslogic authored
Showing with 84 additions and 42 deletions.
  1. +1 −0  AUTHORS
  2. +52 −39 lib/columnize/vertical.rb
  3. +31 −3 test/test-rows_and_cols.rb
View
1  AUTHORS
@@ -1 +1,2 @@
R. Bernstein (rockyb@rubyforge.net)
+M. Davis (waslogic@gmail.com)
View
91 lib/columnize/vertical.rb
@@ -4,25 +4,61 @@
# Part of Columnize to format in the vertical direction
module Columnize
module_function
- def columnize_vertical(l, opts)
+ def columnize_vertical(list, opts)
+ nrows, ncols, colwidths = compute_stuff(list, opts)
+ # nrows, ncols, colwidths = compute_rows_cols_and_width(list, opts)
+ array_index = lambda {|num_rows, row, col| num_rows*col + row }
+ # The smallest number of rows computed and the max widths for each column has been obtained.
+ # Now we just have to format each of the rows.
+ (0...nrows).inject('') do |s, _row|
+ row = _row
+ texts = []
+ 0.upto(ncols-1) do |col|
+ i = array_index.call(nrows, row, col)
+ if i >= list.size
+ x = ''
+ else
+ x = list[i]
+ end
+ texts.push(x)
+ end
+ texts.pop while !texts.empty? and texts[-1] == ''
+ if texts.size > 0
+ 0.upto(texts.size-1) do |col|
+ unless ncols == 1 && opts[:ljust]
+ if opts[:ljust]
+ texts[col] = texts[col].ljust(colwidths[col])
+ else
+ texts[col] = texts[col].rjust(colwidths[col])
+ end
+ end
+ end
+ s += "%s%s%s" % [opts[:lineprefix], texts.join(opts[:colsep]), opts[:linesuffix]]
+ end
+ s
+ end
+ end
+
+ # vertical rows and columns differ from horizontal rows and columns!
+ def compute_stuff(list, opts)
nrows = ncols = 0 # Make nrows, ncols have more global scope
colwidths = [] # Same for colwidths
array_index = lambda {|num_rows, row, col| num_rows*col + row }
# Try every row count from 1 upwards
- 1.upto(l.size-1) do |_nrows|
+ 1.upto(list.size-1) do |_nrows|
nrows = _nrows
- ncols = (l.size + nrows-1) / nrows
+ ncols = (list.size + nrows-1) / nrows
colwidths = []
totwidth = -opts[:colsep].length
-
+
0.upto(ncols-1) do |col|
# get max column width for this column
colwidth = 0
0.upto(nrows-1) do |_row|
row = _row
i = array_index.call(nrows, row, col)
- break if i >= l.size
- colwidth = [colwidth, cell_size(l[i], opts[:term_adjust])].max
+ break if i >= list.size
+ colwidth = [colwidth, cell_size(list[i], opts[:term_adjust])].max
end
colwidths.push(colwidth)
totwidth += colwidth + opts[:colsep].length
@@ -34,38 +70,15 @@ def columnize_vertical(l, opts)
break if totwidth <= opts[:displaywidth]
end
ncols = 1 if ncols < 1
- nrows = l.size if ncols == 1
- # The smallest number of rows computed and the max widths for
- # each column has been obtained. Now we just have to format
- # each of the rows.
- s = ''
- 0.upto(nrows-1) do |_row|
- row = _row
- texts = []
- 0.upto(ncols-1) do |col|
- i = array_index.call(nrows, row, col)
- if i >= l.size
- x = ''
- else
- x = l[i]
- end
- texts.push(x)
- end
- texts.pop while !texts.empty? and texts[-1] == ''
- if texts.size > 0
- 0.upto(texts.size-1) do |col|
- unless ncols == 1 && opts[:ljust]
- if opts[:ljust]
- texts[col] = texts[col].ljust(colwidths[col])
- else
- texts[col] = texts[col].rjust(colwidths[col])
- end
- end
- end
- s += "%s%s%s" % [opts[:lineprefix], texts.join(opts[:colsep]),
- opts[:linesuffix]]
- end
- end
- return s
+ nrows = list.size if ncols == 1
+ [nrows, ncols, colwidths]
+ end
+
+ def v_rows_and_cols(list, nrows, ncols)
+ puts [list, nrows, ncols].inspect
+ cols = (0...ncols).map {|c| list[c*nrows, nrows] }.compact
+ puts "cols: #{cols.inspect}"
+ rows = cols[0].zip(*cols[1..-1]).map(&:compact)
+ [rows, cols]
end
end
View
34 test/test-rows_and_cols.rb
@@ -9,15 +9,16 @@ class TestRowsAndCols < Test::Unit::TestCase
require File.join(TOP_SRC_DIR, 'columnize.rb')
include Columnize
- OPTS = Columnize::DEFAULT_OPTS.dup
+ OPTS = Columnize::DEFAULT_OPTS.merge(:arrange_vertical => false)
def test_base
assert_equal([1, 3, [8,8,8]], compute_rows_cols_and_width([1, 2, 3], OPTS))
+ assert_equal([1, 3, [8,8,8]], compute_stuff([1, 2, 3], OPTS))
end
def test_colwidths
- data = ["one", "two", "three",
- "for", "five", "six",
+ data = ["one", "two", "three",
+ "four", "five", "six",
"seven", "eight", "nine",
"ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen",
@@ -27,10 +28,37 @@ def test_colwidths
"twentyfive","twentysix", "twentyseven"]
assert_equal([5, 6, [10, 9, 11, 9, 11, 10]], compute_rows_cols_and_width(data, OPTS))
+ # assert_equal([5, 6, [10, 9, 11, 9, 11, 10]], compute_stuff(data, OPTS))
+ end
+
+ def test_horizontal_vs_vertical
+ data = (0..54).map{|i| i.to_s}
+ opts = OPTS.merge(:displaywidth => 39)
+ assert_equal([6, 10, [2,2,2,2,2,2,2,2,2,2]], compute_rows_cols_and_width(data, opts))
+ assert_equal([6, 10, [1,2,2,2,2,2,2,2,2,2]], compute_stuff(data, opts))
+
+ # assert_equal(
+ # "0, 6, 12, 18, 24, 30, 36, 42, 48, 54\n" +
+ # "1, 7, 13, 19, 25, 31, 37, 43, 49\n" +
+ # "2, 8, 14, 20, 26, 32, 38, 44, 50\n" +
+ # "3, 9, 15, 21, 27, 33, 39, 45, 51\n" +
+ # "4, 10, 16, 22, 28, 34, 40, 46, 52\n" +
+ # "5, 11, 17, 23, 29, 35, 41, 47, 53\n",
+ # columnize(data, 39, ', ', true, false))
+
+ # assert_equal(
+ # " 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n" +
+ # "10, 11, 12, 13, 14, 15, 16, 17, 18, 19\n" +
+ # "20, 21, 22, 23, 24, 25, 26, 27, 28, 29\n" +
+ # "30, 31, 32, 33, 34, 35, 36, 37, 38, 39\n" +
+ # "40, 41, 42, 43, 44, 45, 46, 47, 48, 49\n" +
+ # "50, 51, 52, 53, 54\n",
+ # columnize(data, 39, ', ', false, false))
end
def test_displaywidth_smaller_than_largest_atom
data = ['a' * 100, 'b', 'c', 'd', 'e']
assert_equal([5, 1, [100]], compute_rows_cols_and_width(data, OPTS))
+ assert_equal([5, 1, [100]], compute_stuff(data, OPTS))
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.