class Fuelcell::Help::OptsFormatter

Attributes

banner_space[R]
indent[R]

Public Class Methods

new(config = {}) click to toggle source
Calls superclass method Fuelcell::Help::BaseFormatter::new
# File lib/fuelcell/help/opts_formatter.rb, line 5
def initialize(config = {})
  super
  @indent       = (config[:indent] || 2).to_i
  @banner_space = (config[:banner_space] || 2).to_i
end

Public Instance Methods

call(data) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 11
def call(data)
  return '' if empty?(data)

  str    = "Options:\n"
  widest = widest_opt(data[:options])
  data[:options].each do |opt|
    opt, banner = opt_data(opt)
    str << line(opt, widest, banner)
  end
  str
end
create_padding(widest, opt) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 32
def create_padding(widest, opt)
  ' ' * (widest - opt.size)
end
empty?(data) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 36
def empty?(data)
  !data.key?(:options) || data[:options].empty?
end
line(opt, widest_opt, banner_text) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 23
def line(opt, widest_opt, banner_text)
  max          = indent + banner_space + widest_opt
  banner       = wrap(banner_text, max)
  pad          = create_padding(widest_opt, opt)
  indent_str   = ' ' * indent
  column_space = ' ' * banner_space
  "#{indent_str}#{opt}#{pad}#{column_space}#{banner}\n"
end
opt_data(data) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 57
def opt_data(data)
  ["#{short_opt(data, '  ')} #{long_opt(data)}", data[:banner] || '']
end
widest_opt(options) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 48
def widest_opt(options)
  max = 0
  options.each do |data|
    size = "#{short_opt(data, '  ')} #{long_opt(data)}".size
    max  = size if size > max
  end
  max
end
wrap(text, widest_text) click to toggle source
# File lib/fuelcell/help/opts_formatter.rb, line 40
def wrap(text, widest_text)
  return text if (widest_text + text.size) < max_width

  pad = ' ' * widest_text
  pattern = /(.{1,#{max_width - widest_text}})(\s+|$)/
  text.gsub(pattern, "\\1\n#{pad}").strip
end