Method: Loquacious::Configuration::Help#initialize

Defined in:
lib/loquacious/configuration/help.rb

#initialize(config, opts = {}) ⇒ Help

Create a new Help instance for the given configuration where config can be either a Configuration instance or a configuration name or symbol. Several options can be provided to determine how the configuration information will be printed to the IO stream.

:name_leader      String appearing before the attribute name
:name_length      Maximum length for an attribute name
:name_value_sep   String separating the attribute name from the value
:desc_leader      String appearing before the description
:io               The IO object where help will be written
:nesting_nodes    Flag to enable or disable output of nesting nodes
                  (this does not affect display of attributes
                  contained by the nesting nodes)
:colorize         Flag to colorize the output or not
:colors           Hash of colors for the name, value, description
    :name           Name color
    :value          Value color
    :description    Description color
    :leader         Leader and spacer color

The description is printed before each attribute name and value on its own line.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/loquacious/configuration/help.rb', line 58

def initialize( config, opts = {} )
  opts = @@defaults.merge opts
  @config = config.kind_of?(::Loquacious::Configuration) ? config :
            ::Loquacious::Configuration.for(config)

  @io = opts[:io]
  @name_length = Integer(opts[:name_length])
  @desc_leader = opts[:desc_leader]
  @nesting_nodes = opts[:nesting_nodes]
  @colorize = opts[:colorize]
  @colors = opts[:colors]

  unless @name_length > 0
    Iterator.new(@config).each do |node|
      length = node.name.length
      @name_length = length if length > @name_length
    end
  end

  name_leader = opts[:name_leader]
  name_value_sep = opts[:name_value_sep]
  extra_length = name_leader.length + name_value_sep.length
  name_value_sep = name_value_sep.gsub('%', '%%')

  @value_length = 78 - @name_length - extra_length
  @value_leader = "\n" + ' '*(@name_length + extra_length)
  @format = "#{name_leader}%-#{@name_length}s#{name_value_sep}%s"
  @name_format = "#{name_leader}%s"

  if colorize?
    @desc_leader = self.__send__(@colors[:leader], @desc_leader)
    name_leader = self.__send__(@colors[:leader], name_leader)
    name_value_sep = self.__send__(@colors[:leader], name_value_sep)

    @format = name_leader.dup
    @format << self.__send__(@colors[:name], "%-#{@name_length}s")
    @format << name_value_sep.dup
    @format << self.__send__(@colors[:value], "%s")

    @name_format = name_leader.dup
    @name_format << self.__send__(@colors[:name], "%s")
  end

  @desc_leader.freeze
  @value_leader.freeze
  @format.freeze
  @name_format.freeze
end