class HammerCLI::Help::List

Constants

LIST_INDENT

Public Class Methods

new(items, options = {}) click to toggle source
Calls superclass method HammerCLI::Help::AbstractItem.new
# File lib/hammer_cli/help/definition/list.rb, line 6
def initialize(items, options = {})
  super(options)
  @indent_size = options[:indent_size] || indent_size(items)
  build_definition(items || [])
end

Public Instance Methods

build_string() click to toggle source
# File lib/hammer_cli/help/definition/list.rb, line 12
def build_string
  out = StringIO.new
  @definition.each do |item|
    out.puts item.build_string
  end
  out.string
end

Protected Instance Methods

build_definition(items) click to toggle source
# File lib/hammer_cli/help/definition/list.rb, line 22
def build_definition(items)
  @definition = Definition.new
  items.each do |item|
    @definition << Text.new(format_item(item))
  end
  @definition
end

Private Instance Methods

format_item(item) click to toggle source
# File lib/hammer_cli/help/definition/list.rb, line 38
def format_item(item)
  col1, col2 = item
  col2 = indent(col2.to_s, ' ' * @indent_size).lstrip
  line = "%-#{@indent_size}s%s" % [col1, col2]
  line.strip!
  line
end
indent_size(items) click to toggle source
# File lib/hammer_cli/help/definition/list.rb, line 32
def indent_size(items)
  items = normalize(items)
  max_len = items.map { |i| i[0].to_s.length }.max
  (max_len + INDENT_STEP > LIST_INDENT) ? (max_len + INDENT_STEP) : LIST_INDENT
end
normalize(items) click to toggle source
# File lib/hammer_cli/help/definition/list.rb, line 46
def normalize(items)
  items.map do |i|
    i.is_a?(Array) ? i : [i]
  end
end