Class: HammerCLI::Help::TextBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/help/text_builder.rb

Constant Summary collapse

INDENT_STEP =
2
LIST_INDENT =
20

Instance Method Summary collapse

Constructor Details

#initialize(richtext = false) ⇒ TextBuilder

Returns a new instance of TextBuilder.



7
8
9
10
# File 'lib/hammer_cli/help/text_builder.rb', line 7

def initialize(richtext = false)
  @out = StringIO.new
  @richtext = richtext
end

Instance Method Details

#indent(content, indentation = nil) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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| ... } ⇒ Object

Yields:

  • (sub_builder)


39
40
41
42
43
44
45
46
# 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

#stringObject



12
13
14
# File 'lib/hammer_cli/help/text_builder.rb', line 12

def string
  @out.string
end

#text(content) ⇒ Object



16
17
18
19
# File 'lib/hammer_cli/help/text_builder.rb', line 16

def text(content)
  puts unless first_print?
  puts content
end