Module: Rodish::Plugins::Wrap_

Included in:
Rodish::Plugins::WrappedOptionsSeparator::OptionParserMethods
Defined in:
lib/rodish/plugins/_wrap.rb

Instance Method Summary collapse

Instance Method Details

#wrap(prefix, values, separator: " ", limit: 80) ⇒ Object

Return an array of strings, each no longer than limit, showing the prefix and all values.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rodish/plugins/_wrap.rb', line 13

public def wrap(prefix, values, separator: " ", limit: 80)
  line = [prefix]
  lines = [line]
  prefix_length = length = prefix.length
  sep_length = separator.length
  indent = " " * prefix_length

  values.each do |value|
    value = value.to_s
    value_length = value.length
    new_length = sep_length + length + value_length
    if new_length > limit
      line = [indent, separator, value]
      lines << line
      length = prefix_length
    else
      line << separator << value
    end
    length += sep_length + value_length
  end

  lines.map{|l| l.join}
end