pax_global_header00006660000000000000000000000064120442603230014506gustar00rootroot0000000000000052 comment=9424d0c64799d1b2f6576f9942ef59869f792bcd ruby-columnize-0.3.6/000077500000000000000000000000001204426032300145005ustar00rootroot00000000000000ruby-columnize-0.3.6/.gitignore000066400000000000000000000000151204426032300164640ustar00rootroot00000000000000*~ /doc /pkg ruby-columnize-0.3.6/AUTHORS000066400000000000000000000000441204426032300155460ustar00rootroot00000000000000R. Bernstein (rockyb@rubyforge.net) ruby-columnize-0.3.6/COPYING000066400000000000000000000047171204426032300155440ustar00rootroot00000000000000Columnize is copyrighted free software by Rocky Bernstein . You can redistribute it and/or modify it under either the terms of the GPL version 2 (see the file GPL), or the conditions below: 1. You may make and give away verbatim copies of the source form of the software without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may modify your copy of the software in any way, provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or by allowing the author to include your modifications in the software. b) use the modified software only within your corporation or organization. c) give non-standard binaries non-standard names, with instructions on where to get the original software distribution. d) make other distribution arrangements with the author. 3. You may distribute the software in object code or binary form, provided that you do at least ONE of the following: a) distribute the binaries and library files of the software, together with instructions (in the manual page or equivalent) on where to get the original distribution. b) accompany the distribution with the machine-readable source of the software. c) give non-standard binaries non-standard names, with instructions on where to get the original software distribution. d) make other distribution arrangements with the author. 4. You may modify and include the part of the software into any other software (possibly commercial). But some files in the distribution are not written by the author, so that they are not under these terms. For the list of those files and their copying conditions, see the file LEGAL. 5. The scripts and library files supplied as input to or produced as output from the software do not automatically fall under the copyright of the software, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this software. 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ruby-columnize-0.3.6/ChangeLog000066400000000000000000000210471204426032300162560ustar00rootroot000000000000002011-06-09 rocky * ChangeLog, NEWS, Rakefile: Get ready for new release. 2011-04-27 r * README.md: Fix code example. 2011-04-27 r * .gemspec, README, README.md, Rakefile: README->README.md 2011-04-27 r * .gemspec, AUTHORS, COPYING, ChangeLog, Makefile, NEWS, README, Rakefile, lib/Makefile, lib/columnize.rb, lib/version.rb, tags/release-0.2.1/AUTHORS, tags/release-0.2.1/COPYING, tags/release-0.2.1/ChangeLog, tags/release-0.2.1/NEWS, tags/release-0.2.1/README, tags/release-0.2.1/Rakefile, tags/release-0.2.1/VERSION, tags/release-0.2.1/lib/columnize.rb, tags/release-0.2.1/test/test-columnize.rb, tags/release-0.3.0/AUTHORS, tags/release-0.3.0/COPYING, tags/release-0.3.0/ChangeLog, tags/release-0.3.0/NEWS, tags/release-0.3.0/README, tags/release-0.3.0/Rakefile, tags/release-0.3.0/VERSION, tags/release-0.3.0/lib/columnize.rb, tags/release-0.3.0/test/test-columnize.rb, tags/release-0.3.1/AUTHORS, tags/release-0.3.1/COPYING, tags/release-0.3.1/ChangeLog, tags/release-0.3.1/NEWS, tags/release-0.3.1/README, tags/release-0.3.1/Rakefile, tags/release-0.3.1/VERSION, tags/release-0.3.1/lib/columnize.rb, tags/release-0.3.1/test/test-columnize.rb, test/test-columnize.rb, test/test-hashparm.rb, trunk/.gemspec, trunk/AUTHORS, trunk/COPYING, trunk/ChangeLog, trunk/Makefile, trunk/NEWS, trunk/README, trunk/Rakefile, trunk/lib/Makefile, trunk/lib/columnize.rb, trunk/lib/version.rb, trunk/test/test-columnize.rb, trunk/test/test-hashparm.rb: Remove trunk and tags. 2011-04-27 rockyb * trunk/ChangeLog, trunk/lib/columnize.rb, trunk/lib/version.rb, trunk/test/test-columnize.rb, trunk/test/test-hashparm.rb: Redo columnize options processing to allow for a single hash of options instead of the numerous optional arguments. This make future expansion easier. 2010-09-22 rockyb * trunk/Makefile, trunk/Rakefile: Rakefile: Remove bad test task Makefile: simplify. 2010-09-21 rockyb * trunk/Makefile, trunk/Rakefile: Makefile was a little off 2010-09-21 rockyb * trunk/.gemspec, trunk/Rakefile, trunk/lib/columnize.rb: Small changes to Rakefile and rdoc and so on. 2010-09-20 rockyb * trunk/.gemspec, trunk/Rakefile: Forgot to add .gemspec 2010-09-20 rockyb * trunk/ChangeLog, trunk/Makefile, trunk/NEWS, trunk/README, trunk/Rakefile, trunk/VERSION, trunk/lib/Makefile, trunk/lib/columnize.rb, trunk/lib/version.rb: Add .gemspec, correct description field and add a summary. Add Columnize::VERSION Simplify Rakefile Add stub Makefiles 2010-03-01 rockyb * trunk/README: Better README description 2010-03-01 rockyb * trunk/README: Update README 2010-02-22 rockyb * trunk/ChangeLog, trunk/VERSION, trunk/lib/columnize.rb: Remove shadow variable warnings. Bump version number 2009-07-26 rockyb * tags/release-0.3.1/AUTHORS, tags/release-0.3.1/COPYING, tags/release-0.3.1/ChangeLog, tags/release-0.3.1/NEWS, tags/release-0.3.1/README, tags/release-0.3.1/Rakefile, tags/release-0.3.1/VERSION, tags/release-0.3.1/lib/columnize.rb, tags/release-0.3.1/test/test-columnize.rb: Release 0.3.1 2009-07-26 rockyb * trunk/lib/columnize.rb: Small comment spelling typo. 2009-07-26 rockyb * trunk/ChangeLog: Get ready for 0.3.1 release 2009-07-26 rockyb * trunk/NEWS, trunk/lib/columnize.rb, trunk/test/test-columnize.rb: Add lineprefix. Get ready for release. 2009-07-26 mark-moseley * trunk/ChangeLog, trunk/lib/columnize.rb: Ruby 1.9 updates 2009-03-30 rockyb * trunk/Rakefile: Revise for 1.9 version of rake 2009-01-16 rockyb * trunk/ChangeLog, trunk/Rakefile, trunk/VERSION: package readme was wonky. Looks like no one reads this stuff. 2009-01-10 rockyb * tags/release-0.3.0/AUTHORS, tags/release-0.3.0/COPYING, tags/release-0.3.0/ChangeLog, tags/release-0.3.0/NEWS, tags/release-0.3.0/README, tags/release-0.3.0/Rakefile, tags/release-0.3.0/VERSION, tags/release-0.3.0/lib/columnize.rb, tags/release-0.3.0/test/test-columnize.rb: Release 0.3.0 2009-01-10 rockyb * trunk/ChangeLog, trunk/NEWS: Get ready for release 2009-01-08 rockyb * trunk/ChangeLog, trunk/NEWS, trunk/VERSION, trunk/lib/columnize.rb, trunk/test/test-columnize.rb: Fix bad bug in arranging horizontally 2009-01-01 rockyb * columnize/trunk/AUTHORS, columnize/trunk/COPYING, columnize/trunk/ChangeLog, columnize/trunk/NEWS, columnize/trunk/README, columnize/trunk/Rakefile, columnize/trunk/VERSION, columnize/trunk/lib/columnize.rb, columnize/trunk/test/test-columnize.rb: Remove hacky columnize/columnize 2009-01-01 rockyb * tags/columnize-0.2/AUTHORS, tags/columnize-0.2/COPYING, tags/columnize-0.2/ChangeLog, tags/columnize-0.2/NEWS, tags/columnize-0.2/README, tags/columnize-0.2/Rakefile, tags/columnize-0.2/VERSION, tags/columnize-0.2/lib/columnize.rb, tags/columnize-0.2/test/test-columnize.rb: Regularize tags names better 2009-01-01 rockyb * tags/release-0.2.1/AUTHORS, tags/release-0.2.1/COPYING, tags/release-0.2.1/ChangeLog, tags/release-0.2.1/NEWS, tags/release-0.2.1/README, tags/release-0.2.1/Rakefile, tags/release-0.2.1/VERSION, tags/release-0.2.1/lib/columnize.rb, tags/release-0.2.1/test/test-columnize.rb: Release 0.2.1 2009-01-01 rockyb * trunk/NEWS: Merge NEWS 2009-01-01 rockyb * columnize/tags/columnize-0.2/AUTHORS, columnize/tags/columnize-0.2/COPYING, columnize/tags/columnize-0.2/ChangeLog, columnize/tags/columnize-0.2/NEWS, columnize/tags/columnize-0.2/README, columnize/tags/columnize-0.2/Rakefile, columnize/tags/columnize-0.2/VERSION, columnize/tags/columnize-0.2/lib/columnize.rb, columnize/tags/columnize-0.2/test/test-columnize.rb, tags/columnize-0.2/AUTHORS, tags/columnize-0.2/COPYING, tags/columnize-0.2/ChangeLog, tags/columnize-0.2/NEWS, tags/columnize-0.2/README, tags/columnize-0.2/Rakefile, tags/columnize-0.2/VERSION, tags/columnize-0.2/lib/columnize.rb, tags/columnize-0.2/test/test-columnize.rb: Cleanup of columnize/columnize crap 2009-01-01 rockyb * : Add directory for releases 2009-01-01 rockyb * trunk/NEWS, trunk/VERSION: Ooops 0.2 release done. Go for 0.2.1 2009-01-01 rockyb * trunk/ChangeLog: Get ready for release 0.2 2009-01-01 rockyb * trunk/ChangeLog, trunk/NEWS, trunk/VERSION, trunk/lib/columnize.rb, trunk/test/test-columnize.rb: Get ready for release 0.2 2008-09-22 rockyb * columnize/tags/columnize-0.2/AUTHORS, columnize/tags/columnize-0.2/COPYING, columnize/tags/columnize-0.2/ChangeLog, columnize/tags/columnize-0.2/NEWS, columnize/tags/columnize-0.2/README, columnize/tags/columnize-0.2/Rakefile, columnize/tags/columnize-0.2/VERSION, columnize/tags/columnize-0.2/lib/columnize.rb, columnize/tags/columnize-0.2/test/test-columnize.rb: Tag 0.2 release. 2008-09-22 rockyb * : Add tags directory for release 2008-09-22 rockyb * columnize/trunk/ChangeLog, columnize/trunk/NEWS: Add directory for tagging releases. Other Administrivia. 2008-09-22 rockyb * columnize/trunk/VERSION, columnize/trunk/lib/columnize.rb: Get rid of hacky $0 test. 2008-03-16 rockyb * trunk/lib/columnize.rb: [no log message] 2008-03-16 rockyb * trunk/lib/columnize.rb: Simplify __FILE__ == $0. (rdebug now changes $0 and rcov has an option) 2008-02-11 rockyb * trunk/lib/columnize.rb: Remove 1.9's shadow warning 2007-12-10 rockyb * columnize/trunk/AUTHORS, columnize/trunk/COPYING, columnize/trunk/ChangeLog, columnize/trunk/NEWS, columnize/trunk/README, columnize/trunk/Rakefile, columnize/trunk/VERSION, columnize/trunk/lib/columnize.rb, columnize/trunk/test/test-columnize.rb: release 0.1 2007-12-09 rockyb * trunk/README, trunk/Rakefile: Fix up doc a little. Make the default rake task "test". 2007-12-09 rockyb * trunk/lib/columnize.rb: Doc fix. 2007-12-09 rockyb * trunk/ChangeLog, trunk/lib/columnize.rb, trunk/test/test-columnize.rb: columnize.rb: Allow one to specify a separator string. Remove restriction that all entries in the array have to be a string. test-columnize.rb: more tests - invalid list, empty list, with separator string parameter. 2007-12-09 rockyb * trunk/test/test-columnize.rb: Typo. 2007-12-09 rockyb * : Property changes for Id lines and to make test-columnize.rb executable. 2007-12-09 rockyb * Initial release of Columnize, a module to print an Array in column-sorted order. ruby-columnize-0.3.6/Makefile000066400000000000000000000003201204426032300161330ustar00rootroot00000000000000# I'll admit it -- I'm an absent-minded old-timer who has trouble # learning new tricks. test: check #: Default target; same as "make check" all: check true #: Same as corresponding rake task %: rake $@ ruby-columnize-0.3.6/NEWS000066400000000000000000000022611204426032300152000ustar00rootroot000000000000000.3.6 Dec 17, 2011 - rename version.rb columnize/version.rb so as not to conflict with another package called version - Administrivia - shorten gemcutter description 0.3.5 Nov 24, 2011 - Handle situation where an array element is larger than the display width. 0.3.4 July 4, 2011 - Change to Ruby License - Add option 'term_adjust' to ignore terminal sequences in text - Add :ljust => :auto to decide whether or not to automatically left or right justify. When passing a hash parameter, the default is :auto. 0.3.3 June 12, 2011 Fleetwood release - More general but simpler inteface using an options hash. Compatibility is maintaind though. 0.3.2 - Mostly Administrivia. * Add .gemspec, correct description field and add a summary. * Add Columnize::VERSION * Simplify Rakefile * Add stub Makefiles 0.3.1 (01-07-26) - Correct for Ruby 1.9 (Mark Moseley) - add optional lineprefix parameter 0.3.0 (01-10-09) - Sam Woodward Release - Fix bad bug in arranging horizontally 0.2.1 (12-31-08) - Add ability to run columns horizontally 0.2 - Minor - get rid of hacky $0 test 0.1 - Initial release of Columnize, a module to print an Array in column-sorted order ruby-columnize-0.3.6/README.md000066400000000000000000000037341204426032300157660ustar00rootroot00000000000000Columnize - Format an Array as a Column-aligned String ============================================================================ In showing a long lists, sometimes one would prefer to see the value arranged aligned in columns. Some examples include listing methods of an object, listing debugger commands, or showing a numeric array with data aligned. 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 ] => 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 bibrons suras golden tokay madascar leopard mourning => nil >> puts columnize g, {:displaywidth => 19, :colsep => ' | '} bibrons | suras golden | tokay madascar leopard mourning => nil >> puts columnize g, {:displaywidth => 18, :colsep => ' | ', :ljust=>false} bibrons | mourning golden | suras madascar | tokay leopard Credits ------- This is adapted from a method of the same name from Python's cmd module. Other stuff ----------- Author: Rocky Bernstein License: Copyright (c) 2011 Rocky Bernstein Warranty -------- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ruby-columnize-0.3.6/Rakefile000066400000000000000000000040171204426032300161470ustar00rootroot00000000000000#!/usr/bin/env rake # -*- Ruby -*- require 'rubygems' require 'rake/gempackagetask' require 'rake/rdoctask' require 'rake/testtask' require 'fileutils' ROOT_DIR = File.dirname(__FILE__) require File.join(ROOT_DIR, '/lib/columnize/version') def gemspec @gemspec ||= eval(File.read('columnize.gemspec'), binding, 'columnize.gemspec') end desc "Build the gem" task :package=>:gem task :gem=>:gemspec do Dir.chdir(ROOT_DIR) do sh "gem build columnize.gemspec" FileUtils.mkdir_p 'pkg' FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg' end end desc "Install the gem locally" task :install => :gem do Dir.chdir(ROOT_DIR) do sh %{gem install --local pkg/#{gemspec.name}-#{gemspec.version}} end end desc "Test everything." Rake::TestTask.new(:test) do |t| t.libs << './lib' t.pattern = 'test/test-*.rb' t.verbose = true end task :test => :lib desc "same as test" task :check => :test desc 'Create a GNU-style ChangeLog via git2cl' task :ChangeLog do system('git log --pretty --numstat --summary | git2cl > ChangeLog') end task :default => [:test] task :clobber_rdoc do FileUtils.rm_rf File.join(ROOT_DIR, 'doc') end task :rm_patch_residue do FileUtils.rm_rf FileList['**/*.{rej,orig}'].to_a end desc "Remove built files" task :clean => [:clobber_package, :clobber_rdoc, :rm_patch_residue] desc "Generate the gemspec" task :generate do puts gemspec.to_ruby end desc "Validate the gemspec" task :gemspec do gemspec.validate end # --------- RDoc Documentation ------ desc "Generate rdoc documentation" Rake::RDocTask.new("rdoc") do |rdoc| rdoc.rdoc_dir = 'doc' rdoc.title = "Columnize #{Columnize::VERSION} Documentation" # Make the README file the start page for the generated html rdoc.options += %w(--main README.md) rdoc.rdoc_files.include('lib/*.rb', 'README.md', 'COPYING') end desc "Same as rdoc" task :doc => :rdoc task :clobber_package do FileUtils.rm_rf File.join(ROOT_DIR, 'pkg') end task :clobber_rdoc do FileUtils.rm_rf File.join(ROOT_DIR, 'doc') end ruby-columnize-0.3.6/columnize.gemspec000066400000000000000000000025271204426032300200600ustar00rootroot00000000000000# -*- Ruby -*- # -*- encoding: utf-8 -*- require 'rake' require 'rubygems' unless Object.const_defined?(:Gem) require File.dirname(__FILE__) + "/lib/columnize/version" unless Object.const_defined?(:'Columnize') Gem::Specification.new do |spec| spec.authors = ['R. Bernstein'] spec.date = Time.now spec.description = ' In showing a long lists, sometimes one would prefer to see the value arranged aligned in columns. Some examples include listing methods of an object or debugger commands. See Examples in the rdoc documentation for examples. ' spec.email = 'rockyb@rubyforge.net' spec.files = `git ls-files`.split("\n") spec.homepage = 'https://github.com/rocky/columnize' spec.name = 'columnize' spec.licenses = ['Ruby', 'GPL2'] spec.platform = Gem::Platform::RUBY spec.require_path = 'lib' spec.required_ruby_version = '>= 1.8.2' spec.rubyforge_project = 'columnize' spec.summary = 'Module to format an Array as an Array of String aligned in columns' spec.version = Columnize::VERSION spec.has_rdoc = true spec.extra_rdoc_files = %w(README.md lib/columnize.rb COPYING) # Make the readme file the start page for the generated html spec.rdoc_options += %w(--main README) spec.rdoc_options += ['--title', "Columnize #{Columnize::VERSION} Documentation"] end ruby-columnize-0.3.6/lib/000077500000000000000000000000001204426032300152465ustar00rootroot00000000000000ruby-columnize-0.3.6/lib/Makefile000066400000000000000000000004461204426032300167120ustar00rootroot00000000000000# Whatever it is you want to do, it should be handled by the main # (parent) Makefile. So reissue make from there. PHONY=check all #: Default target - the parent's testing or "check" target all: check #: Whatever it is you want to do, it should be handled by the parent %: $(MAKE) -C .. $@ ruby-columnize-0.3.6/lib/columnize.rb000066400000000000000000000237011204426032300176030ustar00rootroot00000000000000# Module to format an Array as an Array of String aligned in columns. # # == Summary # Display a list of strings as a compact set of columns. # # For example, for a line width of 4 characters (arranged vertically): # ['1', '2,', '3', '4'] => '1 3\n2 4\n' # # or arranged horizontally: # ['1', '2,', '3', '4'] => '1 2\n3 4\n' # # Each column is only as wide as necessary. By default, columns are # separated by two spaces. Options are avalable for setting # * the display width # * the column separator # * the line prefix # * whether to ignore terminal codes in text size calculation # * whether to left justify text instead of right justify # # == License # # Columnize is copyright (C) 2007, 2008, 2009, 2010, 2011 Rocky Bernstein # # # All rights reserved. You can redistribute and/or modify it under # the same terms as Ruby. # # Adapted from the routine of the same name in Python +cmd.py+. module Columnize # When an option is not specified for the below keys, these # are the defaults. DEFAULT_OPTS = { :arrange_array => false, :arrange_vertical => true, :array_prefix => '', :array_suffix => '', :colsep => ' ', :displaywidth => 80, :lineprefix => '', :ljust => :auto, :term_adjust => false } module_function # Options parsing routine for Columnize::columnize. In the preferred # newer style, +args+ is either a hash where each key is one of the option # names: # # [arrange_vertical] Arrange list vertically rather than horizontally. This is the default # [colsep] String used to separate columns # [displaywidth] Maximum width of each line # [ljust] Boolean or +:auto+: Left-justify fields in a column? The default is +true+. If # the :auto, then right-justify if every element of the data is a kind of Numeric. # [lineprefix] String: string to prepend to each line. The default is ''. # # In the older style positional arguments are used and the positions # are in the order: +displaywidth+, +colsep+, +arrange_vertical+, # +ljust+, and +lineprefix+. def parse_columnize_options(args) list = args.shift if 1 == args.size && args[0].kind_of?(Hash) opts = DEFAULT_OPTS.merge(args[0]) if opts[:arrange_array] opts[:array_prefix] = '[' opts[:lineprefix] = ' ' opts[:array_suffix] = "]\n" opts[:colsep] = ', ' opts[:arrange_vertical] = false end opts[:ljust] = !(list.all?{|datum| datum.kind_of?(Numeric)}) if opts[:ljust] == :auto return list, opts else opts = DEFAULT_OPTS.dup %w(displaywidth colsep arrange_vertical ljust lineprefix ).each do |field| break if args.empty? opts[field.to_sym] = args.shift end return list, opts end end # 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 # Return a list of strings with embedded newlines (\n) as a compact # set of columns arranged horizontally or vertically. # # For example, for a line width of 4 characters (arranged vertically): # ['1', '2,', '3', '4'] => '1 3\n2 4\n' # or arranged horizontally: # ['1', '2,', '3', '4'] => '1 2\n3 4\n' # # Each column is only as wide possible, no larger than # +displaywidth'. If +list+ is not an array, the empty string, '', # is returned. By default, columns are separated by two spaces - one # was not legible enough. Set +colsep+ to adjust the string separate # columns. If +arrange_vertical+ is set false, consecutive items # will go across, left to right, top to bottom. def columnize(*args) list, opts = parse_columnize_options(args) # Some degenerate cases return '' if not list.is_a?(Array) return "\n" if list.empty? l = list.map{|li| li.to_s} return "%s%s%s\n" % [opts[:array_prefix], l[0], opts[:array_suffix]] if 1 == l.size nrows = ncols = 0 # Make nrows, ncols have more global scope colwidths = [] # Same for colwidths if opts[:displaywidth] - opts[:lineprefix].length < 4 opts[:displaywidth] = opts[:lineprefix].length + 4 else opts[:displaywidth] -= opts[:lineprefix].length end if opts[:arrange_vertical] 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| nrows = _nrows ncols = (l.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 end colwidths.push(colwidth) totwidth += colwidth + opts[:colsep].length if totwidth > opts[:displaywidth] ncols = col break end end 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\n" % [opts[:lineprefix], texts.join(opts[:colsep])] end end return s else array_index = lambda {|num_rows, row, col| ncols*(row-1) + col } # Assign to make enlarge scope of loop variables. totwidth = i = rounded_size = 0 # Try every column count from size downwards. l.size.downto(1) do |_ncols| ncols = _ncols # Try every row count from 1 upwards min_rows = (l.size+ncols-1) / ncols min_rows.upto(l.size) do |_nrows| nrows = _nrows rounded_size = nrows * ncols colwidths = [] totwidth = -opts[:colsep].length colwidth = row = 0 0.upto(ncols-1) do |col| # get max column width for this column 1.upto(nrows) 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 end colwidths.push(colwidth) totwidth += colwidth + opts[:colsep].length break if totwidth > opts[:displaywidth]; end if totwidth <= opts[:displaywidth] # Found the right nrows and ncols nrows = row break elsif totwidth >= opts[:displaywidth] # Need to reduce ncols break end end break if totwidth <= opts[:displaywidth] and i >= rounded_size-1 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 = '' prefix = if opts[:array_prefix].empty? opts[:lineprefix] else opts[:array_prefix] end 1.upto(nrows) do |row| texts = [] 0.upto(ncols-1) do |col| i = array_index.call(nrows, row, col) if i >= l.size break else x = l[i] end texts.push(x) end 0.upto(texts.size-1) do |col| unless ncols == 1 && opts[:ljust] if opts[:ljust] texts[col] = texts[col].ljust(colwidths[col]) if ncols != 1 else texts[col] = texts[col].rjust(colwidths[col]) end end end s += "%s%s\n" % [prefix, texts.join(opts[:colsep])] prefix = opts[:lineprefix] end s += opts[:array_suffix] return s end end end if __FILE__ == $0 # include Columnize line = 'require "irb"'; puts cell_size(line, true); puts cell_size(line, false); [[4, 4], [4, 7], [100, 80]].each do |width, num| 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) end end puts Columnize::columnize(5) puts columnize([]) puts columnize(["a", 2, "c"], :displaywidth =>10, :colsep => ', ') puts columnize(["oneitem"]) puts columnize(["one", "two", "three"]) data = ["one", "two", "three", "for", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive","twentysix", "twentyseven"] puts columnize(data) puts columnize(data, 80, ' ', false) end ruby-columnize-0.3.6/lib/columnize/000077500000000000000000000000001204426032300172535ustar00rootroot00000000000000ruby-columnize-0.3.6/lib/columnize/Makefile000066400000000000000000000004571204426032300207210ustar00rootroot00000000000000# Whatever it is you want to do, it should be handled by the main # (parent) Makefile. So reissue make from there. PHONY=check all #: Default target - the parent's testing or "check" target all: check true #: Whatever it is you want to do, it should be handled by the parent %: $(MAKE) -C ../.. $@ ruby-columnize-0.3.6/lib/columnize/version.rb000066400000000000000000000003611204426032300212650ustar00rootroot00000000000000# Sets constant Columnize::VERSION, the version number of # this package. It is used in Gem creation but can also be consulted after # require'ing 'columnize'. module Columnize # The current version of this package VERSION = '0.3.6' end ruby-columnize-0.3.6/metadata.yml000066400000000000000000000033251204426032300170060ustar00rootroot00000000000000--- !ruby/object:Gem::Specification name: columnize version: !ruby/object:Gem::Version prerelease: false segments: - 0 - 3 - 6 version: 0.3.6 platform: ruby authors: - R. Bernstein autorequire: bindir: bin cert_chain: [] date: 2011-12-17 00:00:00 -05:00 default_executable: dependencies: [] description: "\n\ In showing a long lists, sometimes one would prefer to see the value\n\ arranged aligned in columns. Some examples include listing methods\n\ of an object or debugger commands. \n\ See Examples in the rdoc documentation for examples.\n" email: rockyb@rubyforge.net executables: [] extensions: [] extra_rdoc_files: - README.md - lib/columnize.rb - COPYING files: - .gitignore - AUTHORS - COPYING - ChangeLog - Makefile - NEWS - README.md - Rakefile - columnize.gemspec - lib/Makefile - lib/columnize.rb - lib/columnize/Makefile - lib/columnize/version.rb - test/test-columnize.rb - test/test-hashparm.rb - test/test-issue3.rb has_rdoc: true homepage: https://github.com/rocky/columnize licenses: - Ruby - GPL2 post_install_message: rdoc_options: - --main - README - --title - Columnize 0.3.6 Documentation require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version segments: - 1 - 8 - 2 version: 1.8.2 required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version segments: - 0 version: "0" requirements: [] rubyforge_project: columnize rubygems_version: 1.3.7 signing_key: specification_version: 3 summary: Module to format an Array as an Array of String aligned in columns test_files: [] ruby-columnize-0.3.6/test/000077500000000000000000000000001204426032300154575ustar00rootroot00000000000000ruby-columnize-0.3.6/test/test-columnize.rb000077500000000000000000000067111204426032300207760ustar00rootroot00000000000000#!/usr/bin/env ruby require 'test/unit' # Test of Columnize module class TestColumnize < Test::Unit::TestCase @@TOP_SRC_DIR = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib') require File.join(@@TOP_SRC_DIR, 'columnize.rb') include Columnize def test_cell_size assert_equal(3, cell_size('abc', false)) assert_equal(3, cell_size('abc', true)) assert_equal(6, cell_size("\e[0;31mObject\e[0;4m", true)) assert_equal(19, cell_size("\e[0;31mObject\e[0;4m", false)) end # test columnize def test_basic # Try at least one test where we give the module name explicitely. assert_equal("1, 2, 3\n", Columnize::columnize([1, 2, 3], 10, ', ')) assert_equal("", columnize(5)) assert_equal("1 3\n2 4\n", columnize(['1', '2', '3', '4'], 4)) assert_equal("1 2\n3 4\n", columnize(['1', '2', '3', '4'], 4, ' ', false)) assert_equal("\n", columnize([])) assert_equal("oneitem\n", columnize(["oneitem"])) data = (0..54).map{|i| i.to_s} 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)) assert_equal( " 0, 1, 2, 3, 4, 5, 6, 7, 8\n" + " 9, 10, 11, 12, 13, 14, 15, 16, 17\n" + " 18, 19, 20, 21, 22, 23, 24, 25, 26\n" + " 27, 28, 29, 30, 31, 32, 33, 34, 35\n" + " 36, 37, 38, 39, 40, 41, 42, 43, 44\n" + " 45, 46, 47, 48, 49, 50, 51, 52, 53\n" + " 54\n", columnize(data, 39, ', ', false, false, ' ')) data = ["one", "two", "three", "for", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive","twentysix", "twentyseven"] assert_equal( "one two three for five six \n" + "seven eight nine ten eleven twelve \n" + "thirteen fourteen fifteen sixteen seventeen eightteen \n" + "nineteen twenty twentyone twentytwo twentythree twentyfour \n" + "twentyfive twentysix twentyseven\n", columnize(data, 80, ' ', false)) assert_equal( "one five nine thirteen seventeen twentyone twentyfive \n" + "two six ten fourteen eightteen twentytwo twentysix \n" + "three seven eleven fifteen nineteen twentythree twentyseven\n" + "for eight twelve sixteen twenty twentyfour \n", columnize(data)) end end ruby-columnize-0.3.6/test/test-hashparm.rb000066400000000000000000000057021204426032300205700ustar00rootroot00000000000000#!/usr/bin/env ruby require 'test/unit' # Test of Columnize module class TestHashFormat < Test::Unit::TestCase TOP_SRC_DIR = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib') require File.join(TOP_SRC_DIR, 'columnize.rb') include Columnize def test_parse_columnize_options list, default_opts = parse_columnize_options([[], {}]) assert list.kind_of?(Array) assert default_opts.kind_of?(Hash) list, opts = parse_columnize_options([[], 90]) assert_equal 90, opts[:displaywidth] list, opts = parse_columnize_options([[], 70, '|']) assert_equal 70, opts[:displaywidth] assert_equal '|', opts[:colsep] end def test_parse_columnize_ljust list, opts = parse_columnize_options([[1.5, 2, 3], {:ljust => :auto}]) assert_equal false, opts[:ljust] list, opts = parse_columnize_options([[1.5, 2, 3], {:ljust => false}]) assert_equal false, opts[:ljust] list, opts = parse_columnize_options([[1.5, 2, 3], {:ljust => true}]) assert_equal true, opts[:ljust] list, opts = parse_columnize_options([[1, 2, 'b'], {:ljust => :auto}]) assert_equal true, opts[:ljust] end def test_new_hash list, opts = parse_columnize_options([[], {:displaywidth => 40, :colsep => ', ', :term_adjust => true, }]) [[:displaywidth, 40], [:colsep, ', '], [:term_adjust, true]].each do |field, value| assert_equal(value , opts[field]) end list, opts = parse_columnize_options([[], {:displaywidth => 40, :colsep => ', ', }]) assert_equal(false , opts[:term_adjust]) opts = {:colsep => ', '} assert_equal("1, 2, 3\n", Columnize::columnize([1, 2, 3], opts)) end def test_array data = (0..54).to_a 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" + "]\n", columnize(data, :arrange_array => true, :ljust => false, :displaywidth => 39)) end def test_justify data = (0..54).to_a 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" + "]\n", columnize(data, :arrange_array => true, :displaywidth => 39)) end end ruby-columnize-0.3.6/test/test-issue3.rb000066400000000000000000000031621204426032300201760ustar00rootroot00000000000000#!/usr/bin/env ruby require 'test/unit' # Test of Columnize module class TestIssue3 < Test::Unit::TestCase @@TOP_SRC_DIR = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib') require File.join(@@TOP_SRC_DIR, 'columnize.rb') include Columnize # test columnize def test_long_column data = ["what's", "upppppppppppppppppp"] # Try at least one test where we give the module name explicitely. assert_equal("what's\nupppppppppppppppppp\n", Columnize::columnize(data, :arrange_vertical => false, :displaywidth => 7)) assert_equal("what's\nupppppppppppppppppp\n", Columnize::columnize(data, :arrange_vertical => true, :displaywidth => 7)) data = ["whaaaaaat's", "up"] assert_equal("whaaaaaat's\n up\n", Columnize::columnize(data, :arrange_vertical => false, :ljust => false, :displaywidth => 7)) assert_equal("whaaaaaat's\nup\n", Columnize::columnize(data, :arrange_vertical => false, :ljust => true, :displaywidth => 7)) assert_equal("whaaaaaat's\nup\n", Columnize::columnize(data, :arrange_vertical => true, :ljust => true, :displaywidth => 7)) end end