File: opts.rb

package info (click to toggle)
ruby-columnize 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 236 kB
  • sloc: ruby: 477; makefile: 22
file content (35 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Columnize
  computed_displaywidth = (ENV['COLUMNS'] || '80').to_i
  computed_displaywidth = 80 unless computed_displaywidth >= 10

  # 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      => '',
    :colfmt            => nil,
    :colsep            => '  ',
    :displaywidth      => computed_displaywidth,
    :line_prefix        => '',
    :line_suffix        => '',
    :ljust             => :auto,
    :term_adjust       => false
  }

  # Options parsing routine for Columnize::columnize. In the preferred
  # newer style, +args+ is a hash where each key is one of the option names.
  #
  # In the older style positional arguments are used and the positions
  # are in the order: +displaywidth+, +colsep+, +arrange_vertical+,
  # +ljust+, and +line_prefix+.
  def self.parse_columnize_options(args)
    if 1 == args.size && args[0].kind_of?(Hash) # explicitly passed as a hash
      args[0]
    elsif !args.empty? # passed as ugly positional parameters.
      Hash[args.zip([:displaywidth, :colsep, :arrange_vertical, :ljust, :line_prefix]).map(&:reverse)]
    else
      {}
    end
  end
end