# File lib/hammer_cli/help/text_builder.rb, line 7 def initialize(richtext = false) @out = StringIO.new @richtext = richtext end
# 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
# 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
# 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
# File lib/hammer_cli/help/text_builder.rb, line 12 def string @out.string end
# File lib/hammer_cli/help/text_builder.rb, line 16 def text(content) puts unless first_print? puts content end
# File lib/hammer_cli/help/text_builder.rb, line 68 def first_print? @out.size == 0 end
# 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
# 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
# File lib/hammer_cli/help/text_builder.rb, line 64 def puts(*args) @out.puts(*args) end