Class: Clamp::Help::Builder

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

Overview

A builder for auto-generated help.

Constant Summary collapse

DETAIL_FORMAT =
"    %-29s %s".freeze

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



58
59
60
# File 'lib/clamp/help.rb', line 58

def initialize
  @lines = []
end

Instance Method Details

#add_description(description) ⇒ Object



93
94
95
96
97
# File 'lib/clamp/help.rb', line 93

def add_description(description)
  return unless description
  line
  line description.gsub(/^/, "  ")
end

#add_list(heading, items) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/clamp/help.rb', line 101

def add_list(heading, items)
  line
  line "#{heading}:"
  items.reject { |i| i.respond_to?(:hidden?) && i.hidden? }.each do |item|
    label, description = item.help
    description.each_line do |line|
      row(label, line)
      label = ""
    end
  end
end

#add_usage(invocation_path, usage_descriptions) ⇒ Object



86
87
88
89
90
91
# File 'lib/clamp/help.rb', line 86

def add_usage(invocation_path, usage_descriptions)
  line Clamp.message(:usage_heading) + ":"
  usage_descriptions.each do |usage|
    line "    #{invocation_path} #{usage}".rstrip
  end
end

#line(text = "") ⇒ Object



78
79
80
# File 'lib/clamp/help.rb', line 78

def line(text = "")
  @lines << text
end

#row(lhs, rhs) ⇒ Object



82
83
84
# File 'lib/clamp/help.rb', line 82

def row(lhs, rhs)
  @lines << [lhs, rhs]
end

#stringObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/clamp/help.rb', line 62

def string
  left_column_width = lines.grep(Array).map(&:first).map(&:size).max
  StringIO.new.tap do |out|
    lines.each do |line|
      case line
      when Array
        line[0] = line[0].ljust(left_column_width)
        line.unshift("")
        out.puts(line.join("    "))
      else
        out.puts(line)
      end
    end
  end.string
end