Permalink
Browse files

Merge pull request #5 from waslogic/master

patches applied, ready to roll
  • Loading branch information...
2 parents ffbe4bd + b834186 commit 52681e3b1971d3719569f6500c8e66b876ebcbc8 @rocky committed Mar 25, 2013
View
@@ -1 +1,2 @@
R. Bernstein (rockyb@rubyforge.net)
+M. Davis (waslogic@gmail.com)
View
@@ -10,58 +10,82 @@ Setup
-----
$ irb
- >> a = (1..10).to_a
- => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> require 'columnize'
=> true
- >> include Columnize
- => Object
- >> g = %w(bibrons golden madascar leopard mourning suras tokay)
- => ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"]
With numeric data
-----------------
- columnize(a)
- => "1 2 3 4 5 6 7 8 9 10\n"
- >> puts Columnize::columnize(a, :arrange_array => true, :displaywidth => 10)
- [1, 2
- 3, 4
- 5, 6
- 7, 8
- 9, 10
- ]
+ >> a = (1..10).to_a
+ => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+
+ >> a.columnize
+ => "1 2 3 4 5 6 7 8 9 10"
+
+ >> puts a.columnize :arrange_array => true, :displaywidth => 10
+ [1, 2, 3,
+ 4, 5, 6,
+ 7, 8, 9,
+ 10]
+ => nil
+
+ >> puts a.columnize :arrange_array => true, :displaywidth => 20
+ [1, 2, 3, 4, 5, 6,
+ 7, 8, 9, 10]
=> nil
- >> puts Columnize::columnize(a, :arrange_array => true, :displaywidth => 20)
- [1, 2, 3, 4, 5, 6
- 7, 8, 9, 10
- ]
With String data
----------------
- >> puts columnize g, :displaywidth => 15
+ >> g = %w(bibrons golden madascar leopard mourning suras tokay)
+ => ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"]
+
+ >> puts g.columnize :displaywidth => 15
bibrons suras
golden tokay
madascar
- leopard
+ leopard
mourning
=> nil
- >> puts columnize g, {:displaywidth => 19, :colsep => ' | '}
+ >> puts g.columnize :displaywidth => 19, :colsep => ' | '
bibrons | suras
golden | tokay
madascar
- leopard
+ leopard
mourning
=> nil
- >> puts columnize g, {:displaywidth => 18, :colsep => ' | ', :ljust=>false}
+ >> puts g.columnize :displaywidth => 18, :colsep => ' | ', :ljust => false
+ bibrons | mourning
+ golden | suras
+ madascar | tokay
+ leopard
+ => nil
+
+Using Columnize.columnize
+-------------------------
+
+ >> Columnize.columnize(a)
+ => "1 2 3 4 5 6 7 8 9 10"
+ >> puts Columnize.columnize(a, :displaywidth => 10)
+ 1 5 9
+ 2 6 10
+ 3 7
+ 4 8
+ => nil
+
+ >> Columnize.columnize(g)
+ => "bibrons golden madascar leopard mourning suras tokay"
+
+ >> puts Columnize.columnize(g, :displaywidth => 19, :colsep => ' | ')
bibrons | mourning
- golden | suras
- madascar | tokay
- leopard
+ golden | suras
+ madascar | tokay
+ leopard
+ => nil
+
Credits
-------
@@ -71,9 +95,9 @@ This is adapted from a method of the same name from Python's cmd module.
Other stuff
-----------
-Author: Rocky Bernstein <rockyb@rubyforge.org>
+Authors: Rocky Bernstein <rockyb@rubyforge.org> and [Martin Davis](https://github.com/waslogic)
-License: Copyright (c) 2011 Rocky Bernstein
+License: Copyright (c) 2011 Rocky Bernstein
Warranty
--------
View
@@ -8,7 +8,7 @@
# See below for examples and options to the main method +columnize+.
#
#
-# == License
+# == License
#
# Columnize is copyright (C) 2007-2011, 2013 Rocky Bernstein
# <rockyb@rubyforge.net>
@@ -19,41 +19,31 @@
# Also available in Python (columnize), and Perl (Array::Columnize)
module Columnize
-
# Pull in the rest of my pieces
ROOT_DIR = File.dirname(__FILE__)
- %w(opts horizontal vertical version).each do |submod|
+ %w(opts columnize version).each do |submod|
require File.join %W(#{ROOT_DIR} columnize #{submod})
end
- module_function
-
- # Return the length of String +cell+. If Boolean +term_adjust+ is true,
- # ignore terminal sequences in +cell+.
- def cell_size(cell, term_adjust)
- if term_adjust
- cell.gsub(/\e\[.*?m/, '')
- else
- cell
- end.size
- end
+ # Add +columnize_opts+ instance variable to classes that mix in this module. The type should be a kind of hash in file +columnize/opts+.
+ attr_accessor :columnize_opts
- # columize([args]) => String
- #
+ # Columnize.columize([args]) => String
+ #
# Return a string from an array with embedded newlines formatted so
# that when printed the columns are aligned.
- #
+ #
# For example, for a line width of 4 characters (arranged vertically):
# a = (1..4).to_a
# Columnize.columnize(a) => '1 3\n2 4\n'
#
- # Alternatively:
+ # Alternatively:
# a.columnize => '1 3\n2 4\n'
- #
+ #
# Arranged horizontally:
- # a.columnize(:arrange_vertical => false) =>
+ # a.columnize(:arrange_vertical => false) =>
# ['1', '2,', '3', '4'] => '1 2\n3 4\n'
- #
+ #
# Formatted as an array using format specifier '%02d':
# puts (1..10).to_a.columnize(:arrange_array => true, :colfmt => '%02d',
# :displaywidth => 10) =>
@@ -63,7 +53,7 @@ def cell_size(cell, term_adjust)
# 07, 08,
# 09, 10,
# ]
- #
+ #
# Each column is only as wide as necessary. By default, columns are
# separated by two spaces. Options are available for setting
# * the line display width
@@ -73,91 +63,63 @@ def cell_size(cell, term_adjust)
# * A format specify for formatting each item each array item to a string
# * whether to ignore terminal codes in text size calculation
# * whether to left justify text instead of right justify
- # * whether to format as an array - with surrounding [] and
+ # * whether to format as an array - with surrounding [] and
# separating ', '
+ def self.columnize(*args)
+ list = args.shift
+ opts = parse_columnize_options(args)
+ Columnizer.new(list, opts).columnize
+ end
- def columnize(*args)
-
- list, opts = parse_columnize_options(args)
-
- # Some degenerate cases
- return '' if not list.is_a?(Array)
- return "<empty>\n" if list.empty?
-
- # Stringify array elements
- l =
- if opts[:colfmt]
- list.map{|li| opts[:colfmt] % li}
- else
- list.map{|li| li.to_s}
- end
-
- return "%s%s%s\n" % [opts[:array_prefix], l[0],
- opts[:array_suffix]] if 1 == l.size
-
- if opts[:displaywidth] - opts[:lineprefix].length < 4
- opts[:displaywidth] = opts[:lineprefix].length + 4
- else
- opts[:displaywidth] -= opts[:lineprefix].length
- end
- if opts[:arrange_vertical]
- return columnize_vertical(l, opts)
- else
- return columnize_horizontal(l, opts)
+ # Adds columnize_opts to the singleton level of included class
+ def self.included(base)
+ # screw class variables, we'll use an instance variable on the class singleton
+ class << base
+ attr_accessor :columnize_opts
end
+ base.columnize_opts = DEFAULT_OPTS.dup
end
-end
-# Mix in "Columnize" in the Array class and make the columnize method
-# public.
-# Array.send :include, Columnize
-# Array.send :public, :columnize
-
-class Array
- attr_accessor :columnize_opts
- def columnize(*args)
- if args.empty? and self.columnize_opts
- Columnize.columnize(self, self.columnize_opts)
- else
- Columnize.columnize(self, *args)
- end
+ def columnize(opts={})
+ @columnize_opts ||= self.class.columnize_opts.dup
+ @columnizer ||= Columnizer.new(self, @columnize_opts)
+ # make sure that any changes to list or opts get passed to columnizer
+ @columnizer.list = self unless @columnizer.list == self
+ @columnizer.opts = @columnize_opts.merge(opts) unless @columnizer.opts == @columnize_opts and opts.empty?
+ @columnizer.columnize
end
end
+# Mix Columnize into Array
+Array.send :include, Columnize
+
# Demo this sucker
if __FILE__ == $0
- include Columnize
-
+ # include Columnize
+
a = (1..80).to_a
- a.columnize_opts = {:arrange_array => true}
- puts a.columnize
+ puts a.columnize :arrange_array => true
puts '=' * 50
b = (1..10).to_a
puts b.columnize(:displaywidth => 10)
puts '-' * 50
- puts b.columnize(:arrange_array => true, :colfmt => '%02d',
- :displaywidth => 10)
-
- line = 'require "irb"';
- puts cell_size(line, true);
- puts cell_size(line, false);
+ puts b.columnize(:arrange_array => true, :colfmt => '%02d', :displaywidth => 10)
[[4, 4], [4, 7], [100, 80]].each do |width, num|
- data = (1..num).map{|i| i}
+ data = (1..num).map{|i| i }
[[false, 'horizontal'], [true, 'vertical']].each do |bool, dir|
puts "Width: #{width}, direction: #{dir}"
- print columnize(data, :displaywidth => width, :colsep => ' ',
- :arrange_vertical => bool, :ljust => :auto)
+ print Columnize.columnize(data, :displaywidth => width, :colsep => ' ', :arrange_vertical => bool, :ljust => :auto)
end
end
- puts Columnize::columnize(5)
- puts columnize([])
- puts columnize(["a", 2, "c"], :displaywidth =>10, :colsep => ', ')
- puts columnize(["oneitem"])
- puts columnize(["one", "two", "three"])
+ puts Columnize.columnize(5)
+ puts Columnize.columnize([])
+ puts Columnize.columnize(["a", 2, "c"], :displaywidth =>10, :colsep => ', ')
+ puts Columnize.columnize(["oneitem"])
+ puts Columnize.columnize(["one", "two", "three"])
data = ["one", "two", "three",
"for", "five", "six",
"seven", "eight", "nine",
@@ -167,8 +129,7 @@ def columnize(*args)
"nineteen", "twenty", "twentyone",
"twentytwo", "twentythree", "twentyfour",
"twentyfive","twentysix", "twentyseven"]
-
- puts columnize(data)
- puts columnize(data, 80, ' ', false)
+ puts Columnize.columnize(data)
+ puts Columnize.columnize(data, 80, ' ', false)
end
Oops, something went wrong.

0 comments on commit 52681e3

Please sign in to comment.