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"

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



56
57
58
# File 'lib/clamp/help.rb', line 56

def initialize
  @lines = []
end

Instance Method Details

#add_description(description) ⇒ Object



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

def add_description(description)
  return unless description

  line
  line description.gsub(/^/, "  ")
end

#add_list(heading, items) ⇒ Object



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

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



84
85
86
87
88
89
# File 'lib/clamp/help.rb', line 84

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



76
77
78
# File 'lib/clamp/help.rb', line 76

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

#row(lhs, rhs) ⇒ Object



80
81
82
# File 'lib/clamp/help.rb', line 80

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

#stringObject



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

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