class Fuelcell::Help::CmdsFormatter

Attributes

desc_space[R]
indent[R]

Public Class Methods

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

Public Instance Methods

call(data) click to toggle source
# File lib/fuelcell/help/cmds_formatter.rb, line 11
def call(data)
  return '' if empty?(data)
  str    = "Commands:\n"
  widest = widest_cmd(data[:commands])
  data[:commands].each do |cmd|
    str << line(cmd[:name], widest, cmd[:desc])
  end
  str
end
create_padding(widest, cmd) click to toggle source
# File lib/fuelcell/help/cmds_formatter.rb, line 30
def create_padding(widest, cmd)
  ' ' * (widest - cmd.size)
end
empty?(data) click to toggle source
# File lib/fuelcell/help/cmds_formatter.rb, line 34
def empty?(data)
  !data.key?(:commands) || data[:commands].empty?
end
line(cmd, widest_cmd, desc) click to toggle source
# File lib/fuelcell/help/cmds_formatter.rb, line 21
def line(cmd, widest_cmd, desc)
  max          = indent + desc_space + widest_cmd
  desc         = wrap(desc || '', max)
  pad          = create_padding(widest_cmd, cmd)
  indent_str   = ' ' * indent
  column_space = ' ' * desc_space
  "#{indent_str}#{cmd}#{pad}#{column_space}# #{desc}\n"
end
widest_cmd(commands) click to toggle source
# File lib/fuelcell/help/cmds_formatter.rb, line 47
def widest_cmd(commands)
  max = 0
  commands.each do |data|
    size = data[:name].size
    max  = size if size > max
  end
  max
end
wrap(text, widest_text) click to toggle source
# File lib/fuelcell/help/cmds_formatter.rb, line 38
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 = text.gsub(pattern, "\\1\n#{pad}# ")
  text.gsub(/# $/, '').strip
end