class HammerCLI::Help::TextBuilder

Constants

INDENT_STEP
LIST_INDENT

Public Class Methods

new(richtext = false) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 7
def initialize(richtext = false)
  @out = StringIO.new
  @richtext = richtext
end

Public Instance Methods

indent(content, indentation = nil) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 48
def indent(content, indentation = nil)
  indentation ||= " " * INDENT_STEP
  content = content.split("\n") unless content.is_a? Array
  content.map do |line|
    (indentation + line).rstrip
  end.join("\n")
end
list(items) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 21
def list(items)
  return if items.empty?

  items = normalize_list(items)
  max_len = items.map { |i| i[0].to_s.length }.max
  indent_size = (max_len + INDENT_STEP > LIST_INDENT) ? (max_len + INDENT_STEP) : LIST_INDENT

  puts unless first_print?
  items.each do |col1, col2|
    # handle multiple lines in the second column
    col2 = indent(col2.to_s, ' ' * indent_size).lstrip

    line = "%-#{indent_size}s%s" % [col1, col2]
    line.strip!
    puts line
  end
end
section(label) { |sub_builder| ... } click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 39
def section(label, &block)
  puts unless first_print?
  heading(label)

  sub_builder = TextBuilder.new(@richtext)
  yield(sub_builder) if block_given?
  puts indent(sub_builder.string)
end
string() click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 12
def string
  @out.string
end
text(content) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 16
def text(content)
  puts unless first_print?
  puts content
end

Protected Instance Methods

first_print?() click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 68
def first_print?
  @out.size == 0
end
heading(label) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 58
def heading(label)
  label = "#{label}:"
  label = HighLine.color(label, :bold) if @richtext
  puts label
end
normalize_list(items) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 72
def normalize_list(items)
  items.map do |i|
    i.is_a?(Array) ? i : [i]
  end
end
puts(*args) click to toggle source
# File lib/hammer_cli/help/text_builder.rb, line 64
def puts(*args)
  @out.puts(*args)
end