columnize 0.3.6 → 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby-head
8
+ - rbx-18mode
9
+ - rbx-19mode
data/AUTHORS CHANGED
@@ -1 +1,2 @@
1
1
  R. Bernstein (rockyb@rubyforge.net)
2
+ M. Davis (waslogic@gmail.com)
data/COPYING CHANGED
@@ -1,7 +1,7 @@
1
1
  Columnize is copyrighted free software by Rocky Bernstein <rocky@rubyforge.org>.
2
2
 
3
3
  You can redistribute it and/or modify it under either the terms of the GPL
4
- version 2 (see the file GPL), or the conditions below:
4
+ version 2, or the conditions below:
5
5
 
6
6
  1. You may make and give away verbatim copies of the source form of the
7
7
  software without restriction, provided that you duplicate all of the
@@ -45,9 +45,9 @@ version 2 (see the file GPL), or the conditions below:
45
45
  For the list of those files and their copying conditions, see the
46
46
  file LEGAL.
47
47
 
48
- 5. The scripts and library files supplied as input to or produced as
48
+ 5. The scripts and library files supplied as input to or produced as
49
49
  output from the software do not automatically fall under the
50
- copyright of the software, but belong to whomever generated them,
50
+ copyright of the software, but belong to whomever generated them,
51
51
  and may be sold commercially, and may be aggregated with this
52
52
  software.
53
53
 
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ columnize (0.8.9)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ json (1.8.0)
10
+ rake (10.0.4)
11
+ rdoc (4.0.1)
12
+ json (~> 1.4)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ columnize!
19
+ rake
20
+ rdoc
data/Makefile CHANGED
@@ -1,5 +1,9 @@
1
1
  # I'll admit it -- I'm an absent-minded old-timer who has trouble
2
2
  # learning new tricks.
3
+
4
+ RUBY ?= ruby
5
+ RAKE ?= rake
6
+
3
7
  test: check
4
8
 
5
9
  #: Default target; same as "make check"
@@ -8,4 +12,4 @@ all: check
8
12
 
9
13
  #: Same as corresponding rake task
10
14
  %:
11
- rake $@
15
+ $(RAKE) $@
data/NEWS CHANGED
@@ -1,12 +1,19 @@
1
- 0.3.6 Dec 17, 2011
2
- - rename version.rb columnize/version.rb so as not to conflict with
1
+ 0.8.9 Apr 19, 2014
2
+ - Add columnize method to Array class and a place to set its default options
3
+ - Add option :colfmt to allow a format specifier to use (e.g. '%02d') in stringifying
4
+ list items
5
+ - Add option linesuffix (default is "\n")
6
+ - When using arrange_array each line now has trailing ","
7
+
8
+ 0.3.6 Dec 17, 2011
9
+ - rename version.rb columnize/version.rb so as not to conflict with
3
10
  another package called version
4
11
  - Administrivia - shorten gemcutter description
5
12
 
6
- 0.3.5 Nov 24, 2011
13
+ 0.3.5 Nov 24, 2011
7
14
  - Handle situation where an array element is larger than the display width.
8
15
 
9
- 0.3.4 July 4, 2011
16
+ 0.3.4 July 4, 2011
10
17
 
11
18
  - Change to Ruby License
12
19
  - Add option 'term_adjust' to ignore terminal sequences in text
@@ -19,15 +26,15 @@
19
26
  - More general but simpler inteface using an options
20
27
  hash. Compatibility is maintaind though.
21
28
 
22
- 0.3.2
29
+ 0.3.2
23
30
 
24
- - Mostly Administrivia.
25
- * Add .gemspec, correct description field and add a summary.
26
- * Add Columnize::VERSION
31
+ - Mostly Administrivia.
32
+ * Add .gemspec, correct description field and add a summary.
33
+ * Add Columnize::VERSION
27
34
  * Simplify Rakefile
28
35
  * Add stub Makefiles
29
36
 
30
- 0.3.1 (01-07-26)
37
+ 0.3.1 (01-07-26)
31
38
 
32
39
  - Correct for Ruby 1.9 (Mark Moseley)
33
40
 
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://travis-ci.org/rocky/columnize.png)](https://travis-ci.org/rocky/columnize)
2
+
1
3
  Columnize - Format an Array as a Column-aligned String
2
4
  ============================================================================
3
5
 
@@ -10,58 +12,82 @@ Setup
10
12
  -----
11
13
 
12
14
  $ irb
13
- >> a = (1..10).to_a
14
- => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
15
15
  >> require 'columnize'
16
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
17
 
22
18
  With numeric data
23
19
  -----------------
24
20
 
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
- ]
21
+ >> a = (1..10).to_a
22
+ => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
23
+
24
+ >> a.columnize
25
+ => "1 2 3 4 5 6 7 8 9 10"
26
+
27
+ >> puts a.columnize :arrange_array => true, :displaywidth => 10
28
+ [1, 2, 3,
29
+ 4, 5, 6,
30
+ 7, 8, 9,
31
+ 10]
32
+ => nil
33
+
34
+ >> puts a.columnize :arrange_array => true, :displaywidth => 20
35
+ [1, 2, 3, 4, 5, 6,
36
+ 7, 8, 9, 10]
34
37
  => 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
38
 
40
39
  With String data
41
40
  ----------------
42
41
 
43
- >> puts columnize g, :displaywidth => 15
42
+ >> g = %w(bibrons golden madascar leopard mourning suras tokay)
43
+ => ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"]
44
+
45
+ >> puts g.columnize :displaywidth => 15
44
46
  bibrons suras
45
47
  golden tokay
46
48
  madascar
47
- leopard
49
+ leopard
48
50
  mourning
49
51
  => nil
50
52
 
51
- >> puts columnize g, {:displaywidth => 19, :colsep => ' | '}
53
+ >> puts g.columnize :displaywidth => 19, :colsep => ' | '
52
54
  bibrons | suras
53
55
  golden | tokay
54
56
  madascar
55
- leopard
57
+ leopard
56
58
  mourning
57
59
  => nil
58
60
 
59
- >> puts columnize g, {:displaywidth => 18, :colsep => ' | ', :ljust=>false}
61
+ >> puts g.columnize :displaywidth => 18, :colsep => ' | ', :ljust => false
62
+ bibrons | mourning
63
+ golden | suras
64
+ madascar | tokay
65
+ leopard
66
+ => nil
67
+
68
+ Using Columnize.columnize
69
+ -------------------------
70
+
71
+ >> Columnize.columnize(a)
72
+ => "1 2 3 4 5 6 7 8 9 10"
60
73
 
74
+ >> puts Columnize.columnize(a, :displaywidth => 10)
75
+ 1 5 9
76
+ 2 6 10
77
+ 3 7
78
+ 4 8
79
+ => nil
80
+
81
+ >> Columnize.columnize(g)
82
+ => "bibrons golden madascar leopard mourning suras tokay"
83
+
84
+ >> puts Columnize.columnize(g, :displaywidth => 19, :colsep => ' | ')
61
85
  bibrons | mourning
62
- golden | suras
63
- madascar | tokay
64
- leopard
86
+ golden | suras
87
+ madascar | tokay
88
+ leopard
89
+ => nil
90
+
65
91
 
66
92
  Credits
67
93
  -------
@@ -71,14 +97,12 @@ This is adapted from a method of the same name from Python's cmd module.
71
97
  Other stuff
72
98
  -----------
73
99
 
74
- Author: Rocky Bernstein <rockyb@rubyforge.org>
100
+ Authors: Rocky Bernstein <rockyb@rubyforge.org> [![endorse](https://api.coderwall.com/rocky/endorsecount.png)](https://coderwall.com/rocky) and [Martin Davis](https://github.com/waslogic)
75
101
 
76
- License: Copyright (c) 2011 Rocky Bernstein
102
+ License: Copyright (c) 2011,2013 Rocky Bernstein
77
103
 
78
104
  Warranty
79
105
  --------
80
106
 
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.
107
+ You can redistribute it and/or modify it under either the terms of the GPL
108
+ version 2 or the conditions listed in COPYING
data/Rakefile CHANGED
@@ -1,47 +1,49 @@
1
1
  #!/usr/bin/env rake
2
2
  # -*- Ruby -*-
3
3
  require 'rubygems'
4
- require 'rake/gempackagetask'
5
- require 'rake/rdoctask'
6
- require 'rake/testtask'
7
4
  require 'fileutils'
8
5
 
9
6
  ROOT_DIR = File.dirname(__FILE__)
10
- require File.join(ROOT_DIR, '/lib/columnize/version')
7
+ Gemspec_filename = 'columnize.gemspec'
8
+ require File.join %W(#{ROOT_DIR} lib columnize version)
11
9
 
12
10
  def gemspec
13
- @gemspec ||= eval(File.read('columnize.gemspec'),
14
- binding, 'columnize.gemspec')
11
+ @gemspec ||= eval(File.read(Gemspec_filename), binding, Gemspec_filename)
15
12
  end
16
13
 
14
+ require 'rubygems/package_task'
17
15
  desc "Build the gem"
18
16
  task :package=>:gem
19
17
  task :gem=>:gemspec do
20
18
  Dir.chdir(ROOT_DIR) do
21
19
  sh "gem build columnize.gemspec"
22
20
  FileUtils.mkdir_p 'pkg'
23
- FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
21
+ FileUtils.mv gemspec.file_name, 'pkg'
24
22
  end
25
23
  end
26
24
 
27
25
  desc "Install the gem locally"
28
26
  task :install => :gem do
29
27
  Dir.chdir(ROOT_DIR) do
30
- sh %{gem install --local pkg/#{gemspec.name}-#{gemspec.version}}
28
+ sh %{gem install --local pkg/#{gemspec.file_name}}
31
29
  end
32
30
  end
33
31
 
32
+ require 'rake/testtask'
34
33
  desc "Test everything."
35
34
  Rake::TestTask.new(:test) do |t|
36
35
  t.libs << './lib'
37
- t.pattern = 'test/test-*.rb'
36
+ t.test_files = FileList['test/test-*.rb']
38
37
  t.verbose = true
39
38
  end
40
- task :test => :lib
39
+ task :test => :lib
41
40
 
42
41
  desc "same as test"
43
42
  task :check => :test
44
43
 
44
+ desc "same as test"
45
+ task :columnize => :test
46
+
45
47
  desc 'Create a GNU-style ChangeLog via git2cl'
46
48
  task :ChangeLog do
47
49
  system('git log --pretty --numstat --summary | git2cl > ChangeLog')
@@ -49,17 +51,11 @@ end
49
51
 
50
52
  task :default => [:test]
51
53
 
52
- task :clobber_rdoc do
53
- FileUtils.rm_rf File.join(ROOT_DIR, 'doc')
54
- end
55
-
56
- task :rm_patch_residue do
57
- FileUtils.rm_rf FileList['**/*.{rej,orig}'].to_a
54
+ desc 'Create a GNU-style ChangeLog via git2cl'
55
+ task :ChangeLog do
56
+ system('git log --pretty --numstat --summary | git2cl > ChangeLog')
58
57
  end
59
58
 
60
- desc "Remove built files"
61
- task :clean => [:clobber_package, :clobber_rdoc, :rm_patch_residue]
62
-
63
59
  desc "Generate the gemspec"
64
60
  task :generate do
65
61
  puts gemspec.to_ruby
@@ -71,6 +67,7 @@ task :gemspec do
71
67
  end
72
68
 
73
69
  # --------- RDoc Documentation ------
70
+ require 'rdoc/task'
74
71
  desc "Generate rdoc documentation"
75
72
  Rake::RDocTask.new("rdoc") do |rdoc|
76
73
  rdoc.rdoc_dir = 'doc'
@@ -81,6 +78,7 @@ Rake::RDocTask.new("rdoc") do |rdoc|
81
78
 
82
79
  rdoc.rdoc_files.include('lib/*.rb', 'README.md', 'COPYING')
83
80
  end
81
+
84
82
  desc "Same as rdoc"
85
83
  task :doc => :rdoc
86
84
 
@@ -91,3 +89,17 @@ end
91
89
  task :clobber_rdoc do
92
90
  FileUtils.rm_rf File.join(ROOT_DIR, 'doc')
93
91
  end
92
+
93
+ desc 'Remove residue from running patch'
94
+ task :rm_patch_residue do
95
+ FileUtils.rm_rf Dir.glob('**/*.{rej,orig}'), :verbose => true
96
+ end
97
+
98
+ desc 'Remove ~ backup files'
99
+ task :rm_tilde_backups do
100
+ FileUtils.rm_rf Dir.glob('**/*~'), :verbose => true
101
+ end
102
+
103
+ desc 'Remove built files'
104
+ task :clean => [:clobber_package, :clobber_rdoc, :rm_patch_residue,
105
+ :rm_tilde_backups]
data/THANKS ADDED
@@ -0,0 +1,8 @@
1
+ I am indebted to:
2
+
3
+ Mark Moseley:
4
+ the initial port from 1.8 to 1.9
5
+ Martin Davis:
6
+ how to extend into Array classes and set options from there
7
+ David Rodríguez de Dios:
8
+ Miscellaneous portability fixes and doggedness to get out a new release
@@ -1,18 +1,18 @@
1
1
  # -*- Ruby -*-
2
2
  # -*- encoding: utf-8 -*-
3
3
  require 'rake'
4
- require 'rubygems' unless
4
+ require 'rubygems' unless
5
5
  Object.const_defined?(:Gem)
6
- require File.dirname(__FILE__) + "/lib/columnize/version" unless
6
+ require File.dirname(__FILE__) + "/lib/columnize/version" unless
7
7
  Object.const_defined?(:'Columnize')
8
8
 
9
9
  Gem::Specification.new do |spec|
10
- spec.authors = ['R. Bernstein']
10
+ spec.authors = ['Rocky Bernstein']
11
11
  spec.date = Time.now
12
12
  spec.description = '
13
13
  In showing a long lists, sometimes one would prefer to see the value
14
14
  arranged aligned in columns. Some examples include listing methods
15
- of an object or debugger commands.
15
+ of an object or debugger commands.
16
16
  See Examples in the rdoc documentation for examples.
17
17
  '
18
18
  spec.email = 'rockyb@rubyforge.net'
@@ -27,10 +27,12 @@ See Examples in the rdoc documentation for examples.
27
27
  spec.summary = 'Module to format an Array as an Array of String aligned in columns'
28
28
  spec.version = Columnize::VERSION
29
29
  spec.has_rdoc = true
30
- spec.extra_rdoc_files = %w(README.md lib/columnize.rb COPYING)
30
+ spec.extra_rdoc_files = %w(README.md lib/columnize.rb COPYING THANKS)
31
31
 
32
32
  # Make the readme file the start page for the generated html
33
33
  spec.rdoc_options += %w(--main README)
34
34
  spec.rdoc_options += ['--title', "Columnize #{Columnize::VERSION} Documentation"]
35
35
 
36
+ spec.add_development_dependency 'rdoc'
37
+ spec.add_development_dependency 'rake'
36
38
  end
@@ -1,292 +1,127 @@
1
- # Module to format an Array as an Array of String aligned in columns.
1
+ # Module to format an Array into a single string with embedded
2
+ # newlines, On printing the string, the columns are aligned.
2
3
  #
3
4
  # == Summary
4
- # Display a list of strings as a compact set of columns.
5
5
  #
6
- # For example, for a line width of 4 characters (arranged vertically):
7
- # ['1', '2,', '3', '4'] => '1 3\n2 4\n'
8
- #
9
- # or arranged horizontally:
10
- # ['1', '2,', '3', '4'] => '1 2\n3 4\n'
11
- #
12
- # Each column is only as wide as necessary. By default, columns are
13
- # separated by two spaces. Options are avalable for setting
14
- # * the display width
15
- # * the column separator
16
- # * the line prefix
17
- # * whether to ignore terminal codes in text size calculation
18
- # * whether to left justify text instead of right justify
6
+ # Return a string from an array with embedded newlines formatted so
7
+ # that when printed the columns are aligned.
8
+ # See below for examples and options to the main method +columnize+.
19
9
  #
20
- # == License
21
10
  #
22
- # Columnize is copyright (C) 2007, 2008, 2009, 2010, 2011 Rocky Bernstein
11
+ # == License
12
+ #
13
+ # Columnize is copyright (C) 2007-2011, 2013 Rocky Bernstein
23
14
  # <rockyb@rubyforge.net>
24
15
  #
25
16
  # All rights reserved. You can redistribute and/or modify it under
26
17
  # the same terms as Ruby.
27
18
  #
28
- # Adapted from the routine of the same name in Python +cmd.py+.
19
+ # Also available in Python (columnize), and Perl (Array::Columnize)
29
20
 
30
21
  module Columnize
22
+ # Pull in the rest of my pieces
23
+ ROOT_DIR = File.dirname(__FILE__)
24
+ %w(opts columnize version).each do |submod|
25
+ require File.join %W(#{ROOT_DIR} columnize #{submod})
26
+ end
31
27
 
32
- # When an option is not specified for the below keys, these
33
- # are the defaults.
34
- DEFAULT_OPTS = {
35
- :arrange_array => false,
36
- :arrange_vertical => true,
37
- :array_prefix => '',
38
- :array_suffix => '',
39
- :colsep => ' ',
40
- :displaywidth => 80,
41
- :lineprefix => '',
42
- :ljust => :auto,
43
- :term_adjust => false
44
- }
45
-
46
- module_function
28
+ # Add +columnize_opts+ instance variable to classes that mix in this module. The type should be a kind of hash in file +columnize/opts+.
29
+ attr_accessor :columnize_opts
47
30
 
48
- # Options parsing routine for Columnize::columnize. In the preferred
49
- # newer style, +args+ is either a hash where each key is one of the option
50
- # names:
31
+ # Columnize.columize([args]) => String
32
+ #
33
+ # Return a string from an array with embedded newlines formatted so
34
+ # that when printed the columns are aligned.
35
+ #
36
+ # For example, for a line width of 4 characters (arranged vertically):
37
+ # a = (1..4).to_a
38
+ # Columnize.columnize(a) => '1 3\n2 4\n'
39
+ #
40
+ # Alternatively:
41
+ # a.columnize => '1 3\n2 4\n'
42
+ #
43
+ # Arranged horizontally:
44
+ # a.columnize(:arrange_vertical => false) =>
45
+ # ['1', '2,', '3', '4'] => '1 2\n3 4\n'
46
+ #
47
+ # Formatted as an array using format specifier '%02d':
48
+ # puts (1..10).to_a.columnize(:arrange_array => true, :colfmt => '%02d',
49
+ # :displaywidth => 10) =>
50
+ # [01, 02,
51
+ # 03, 04,
52
+ # 05, 06,
53
+ # 07, 08,
54
+ # 09, 10,
55
+ # ]
51
56
  #
52
- # [arrange_vertical] Arrange list vertically rather than horizontally. This is the default
53
- # [colsep] String used to separate columns
54
- # [displaywidth] Maximum width of each line
55
- # [ljust] Boolean or +:auto+: Left-justify fields in a column? The default is +true+. If
56
- # the :auto, then right-justify if every element of the data is a kind of Numeric.
57
- # [lineprefix] String: string to prepend to each line. The default is ''.
58
- #
59
- # In the older style positional arguments are used and the positions
60
- # are in the order: +displaywidth+, +colsep+, +arrange_vertical+,
61
- # +ljust+, and +lineprefix+.
62
- def parse_columnize_options(args)
57
+ # Each column is only as wide as necessary. By default, columns are
58
+ # separated by two spaces. Options are available for setting
59
+ # * the line display width
60
+ # * a column separator
61
+ # * a line prefix
62
+ # * a line suffix
63
+ # * A format specify for formatting each item each array item to a string
64
+ # * whether to ignore terminal codes in text size calculation
65
+ # * whether to left justify text instead of right justify
66
+ # * whether to format as an array - with surrounding [] and
67
+ # separating ', '
68
+ def self.columnize(*args)
63
69
  list = args.shift
64
- if 1 == args.size && args[0].kind_of?(Hash)
65
- opts = DEFAULT_OPTS.merge(args[0])
66
- if opts[:arrange_array]
67
- opts[:array_prefix] = '['
68
- opts[:lineprefix] = ' '
69
- opts[:array_suffix] = "]\n"
70
- opts[:colsep] = ', '
71
- opts[:arrange_vertical] = false
72
- end
73
- opts[:ljust] = !(list.all?{|datum| datum.kind_of?(Numeric)}) if
74
- opts[:ljust] == :auto
75
- return list, opts
76
- else
77
- opts = DEFAULT_OPTS.dup
78
- %w(displaywidth colsep arrange_vertical ljust lineprefix
79
- ).each do |field|
80
- break if args.empty?
81
- opts[field.to_sym] = args.shift
82
- end
83
- return list, opts
84
- end
70
+ opts = parse_columnize_options(args)
71
+ Columnizer.new(list, opts).columnize
85
72
  end
86
73
 
87
- # Return the length of String +cell+. If Boolean +term_adjust+ is true,
88
- # ignore terminal sequences in +cell+.
89
- def cell_size(cell, term_adjust)
90
- if term_adjust
91
- cell.gsub(/\e\[.*?m/, '')
92
- else
93
- cell
94
- end.size
74
+ # Adds columnize_opts to the singleton level of included class
75
+ def self.included(base)
76
+ # screw class variables, we'll use an instance variable on the class singleton
77
+ class << base
78
+ attr_accessor :columnize_opts
79
+ end
80
+ base.columnize_opts = DEFAULT_OPTS.dup
95
81
  end
96
82
 
97
- # Return a list of strings with embedded newlines (\n) as a compact
98
- # set of columns arranged horizontally or vertically.
99
- #
100
- # For example, for a line width of 4 characters (arranged vertically):
101
- # ['1', '2,', '3', '4'] => '1 3\n2 4\n'
102
-
103
- # or arranged horizontally:
104
- # ['1', '2,', '3', '4'] => '1 2\n3 4\n'
105
- #
106
- # Each column is only as wide possible, no larger than
107
- # +displaywidth'. If +list+ is not an array, the empty string, '',
108
- # is returned. By default, columns are separated by two spaces - one
109
- # was not legible enough. Set +colsep+ to adjust the string separate
110
- # columns. If +arrange_vertical+ is set false, consecutive items
111
- # will go across, left to right, top to bottom.
112
-
113
83
  def columnize(*args)
84
+ return Columnize.columnize(*args) if args.length > 1
85
+ opts = args.empty? ? {} : args.pop
86
+ @columnize_opts ||= self.class.columnize_opts.dup
87
+ @columnizer ||= Columnizer.new(self, @columnize_opts)
88
+ # make sure that any changes to list or opts get passed to columnizer
89
+ @columnizer.list = self unless @columnizer.list == self
90
+ @columnizer.opts = @columnize_opts.merge(opts) unless @columnizer.opts == @columnize_opts and opts.empty?
91
+ @columnizer.columnize
92
+ end
93
+ end
114
94
 
115
- list, opts = parse_columnize_options(args)
95
+ # Mix Columnize into Array
96
+ Array.send :include, Columnize
116
97
 
117
- # Some degenerate cases
118
- return '' if not list.is_a?(Array)
119
- return "<empty>\n" if list.empty?
120
- l = list.map{|li| li.to_s}
121
- return "%s%s%s\n" % [opts[:array_prefix], l[0],
122
- opts[:array_suffix]] if 1 == l.size
98
+ # Demo this sucker
99
+ if __FILE__ == $0
100
+ # include Columnize
123
101
 
124
- nrows = ncols = 0 # Make nrows, ncols have more global scope
125
- colwidths = [] # Same for colwidths
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
131
- if opts[:arrange_vertical]
132
- array_index = lambda {|num_rows, row, col| num_rows*col + row }
133
- # Try every row count from 1 upwards
134
- 1.upto(l.size-1) do |_nrows|
135
- nrows = _nrows
136
- ncols = (l.size + nrows-1) / nrows
137
- colwidths = []
138
- totwidth = -opts[:colsep].length
102
+ a = (1..80).to_a
103
+ puts a.columnize :arrange_array => true
104
+ puts '=' * 50
139
105
 
140
- 0.upto(ncols-1) do |col|
141
- # get max column width for this column
142
- colwidth = 0
143
- 0.upto(nrows-1) do |_row|
144
- row = _row
145
- i = array_index.call(nrows, row, col)
146
- break if i >= l.size
147
- colwidth = [colwidth, cell_size(l[i], opts[:term_adjust])].max
148
- end
149
- colwidths.push(colwidth)
150
- totwidth += colwidth + opts[:colsep].length
151
- if totwidth > opts[:displaywidth]
152
- ncols = col
153
- break
154
- end
155
- end
156
- break if totwidth <= opts[:displaywidth]
157
- end
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.
163
- s = ''
164
- 0.upto(nrows-1) do |_row|
165
- row = _row
166
- texts = []
167
- 0.upto(ncols-1) do |col|
168
- i = array_index.call(nrows, row, col)
169
- if i >= l.size
170
- x = ''
171
- else
172
- x = l[i]
173
- end
174
- texts.push(x)
175
- end
176
- texts.pop while !texts.empty? and texts[-1] == ''
177
- if texts.size > 0
178
- 0.upto(texts.size-1) do |col|
179
- unless ncols == 1 && opts[:ljust]
180
- if opts[:ljust]
181
- texts[col] = texts[col].ljust(colwidths[col])
182
- else
183
- texts[col] = texts[col].rjust(colwidths[col])
184
- end
185
- end
186
- end
187
- s += "%s%s\n" % [opts[:lineprefix], texts.join(opts[:colsep])]
188
- end
189
- end
190
- return s
191
- else
192
- array_index = lambda {|num_rows, row, col| ncols*(row-1) + col }
193
- # Assign to make enlarge scope of loop variables.
194
- totwidth = i = rounded_size = 0
195
- # Try every column count from size downwards.
196
- l.size.downto(1) do |_ncols|
197
- ncols = _ncols
198
- # Try every row count from 1 upwards
199
- min_rows = (l.size+ncols-1) / ncols
200
- min_rows.upto(l.size) do |_nrows|
201
- nrows = _nrows
202
- rounded_size = nrows * ncols
203
- colwidths = []
204
- totwidth = -opts[:colsep].length
205
- colwidth = row = 0
206
- 0.upto(ncols-1) do |col|
207
- # get max column width for this column
208
- 1.upto(nrows) do |_row|
209
- row = _row
210
- i = array_index.call(nrows, row, col)
211
- break if i >= l.size
212
- colwidth = [colwidth, cell_size(l[i], opts[:term_adjust])].max
213
- end
214
- colwidths.push(colwidth)
215
- totwidth += colwidth + opts[:colsep].length
216
- break if totwidth > opts[:displaywidth];
217
- end
218
- if totwidth <= opts[:displaywidth]
219
- # Found the right nrows and ncols
220
- nrows = row
221
- break
222
- elsif totwidth >= opts[:displaywidth]
223
- # Need to reduce ncols
224
- break
225
- end
226
- end
227
- break if totwidth <= opts[:displaywidth] and i >= rounded_size-1
228
- end
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.
234
- s = ''
235
- prefix = if opts[:array_prefix].empty?
236
- opts[:lineprefix]
237
- else
238
- opts[:array_prefix]
239
- end
240
- 1.upto(nrows) do |row|
241
- texts = []
242
- 0.upto(ncols-1) do |col|
243
- i = array_index.call(nrows, row, col)
244
- if i >= l.size
245
- break
246
- else
247
- x = l[i]
248
- end
249
- texts.push(x)
250
- end
251
- 0.upto(texts.size-1) do |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
258
- end
259
- end
260
- s += "%s%s\n" % [prefix, texts.join(opts[:colsep])]
261
- prefix = opts[:lineprefix]
262
- end
263
- s += opts[:array_suffix]
264
- return s
265
- end
266
- end
267
- end
268
- if __FILE__ == $0
269
- #
270
- include Columnize
271
-
272
- line = 'require "irb"';
273
- puts cell_size(line, true);
274
- puts cell_size(line, false);
106
+ b = (1..10).to_a
107
+ puts b.columnize(:displaywidth => 10)
108
+
109
+ puts '-' * 50
110
+ puts b.columnize(:arrange_array => true, :colfmt => '%02d', :displaywidth => 10)
275
111
 
276
112
  [[4, 4], [4, 7], [100, 80]].each do |width, num|
277
- data = (1..num).map{|i| i}
113
+ data = (1..num).map{|i| i }
278
114
  [[false, 'horizontal'], [true, 'vertical']].each do |bool, dir|
279
115
  puts "Width: #{width}, direction: #{dir}"
280
- print columnize(data, :displaywidth => width, :colsep => ' ',
281
- :arrange_vertical => bool, :ljust => :auto)
116
+ print Columnize.columnize(data, :displaywidth => width, :colsep => ' ', :arrange_vertical => bool, :ljust => :auto)
282
117
  end
283
118
  end
284
119
 
285
- puts Columnize::columnize(5)
286
- puts columnize([])
287
- puts columnize(["a", 2, "c"], :displaywidth =>10, :colsep => ', ')
288
- puts columnize(["oneitem"])
289
- puts columnize(["one", "two", "three"])
120
+ puts Columnize.columnize(5)
121
+ puts Columnize.columnize([])
122
+ puts Columnize.columnize(["a", 2, "c"], :displaywidth =>10, :colsep => ', ')
123
+ puts Columnize.columnize(["oneitem"])
124
+ puts Columnize.columnize(["one", "two", "three"])
290
125
  data = ["one", "two", "three",
291
126
  "for", "five", "six",
292
127
  "seven", "eight", "nine",
@@ -296,7 +131,7 @@ if __FILE__ == $0
296
131
  "nineteen", "twenty", "twentyone",
297
132
  "twentytwo", "twentythree", "twentyfour",
298
133
  "twentyfive","twentysix", "twentyseven"]
299
-
300
- puts columnize(data)
301
- puts columnize(data, 80, ' ', false)
134
+
135
+ puts Columnize.columnize(data)
136
+ puts Columnize.columnize(data, 80, ' ', false)
302
137
  end