Class: TTY::Option::Formatter Private
- Inherits:
-
Object
- Object
- TTY::Option::Formatter
- Includes:
- UsageWrapper
- Defined in:
- lib/tty/option/formatter.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Responsible for formatting help display
Constant Summary collapse
- BOOLEANS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[true, false].freeze
- DEFAULT_WIDTH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
80
- DOUBLE_SPACE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
" "
- ELLIPSIS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"..."
- EMPTY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
""
- LIST_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
", "
- MAP_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
":"
- NEWLINE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"\n"
- SPACE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
" "
- DEFAULT_NAME_SELECTOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
->(param) { param.name }
- DEFAULT_ORDER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
->(params) { params.sort }
- DEFAULT_PARAM_DISPLAY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
->(str) { str.to_s.upcase }
- NOOP_PROC =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
->(param) { param }
Class Method Summary collapse
-
.help(parameters, usage, **config, &block) ⇒ String
Generate help for parameters and usage.
Instance Method Summary collapse
-
#help {|sections| ... } ⇒ String
Generate help display.
-
#help_arguments ⇒ String
Generate help arguments.
-
#help_banner ⇒ String
Generate help banner.
-
#help_description ⇒ String
Generate help description.
-
#help_environments ⇒ String
Generate help environment variables.
-
#help_examples ⇒ String
Generate help examples.
-
#help_footer ⇒ String
Generate help footer.
-
#help_header ⇒ String
Generate help header.
-
#help_keywords ⇒ String
Generate help keywords.
-
#help_options ⇒ String
Generate help options.
-
#initialize(parameters, usage, param_display: DEFAULT_PARAM_DISPLAY, width: DEFAULT_WIDTH, order: DEFAULT_ORDER, indent: 0) ⇒ Formatter
constructor
Create a Formatter instance.
Methods included from UsageWrapper
Constructor Details
#initialize(parameters, usage, param_display: DEFAULT_PARAM_DISPLAY, width: DEFAULT_WIDTH, order: DEFAULT_ORDER, indent: 0) ⇒ Formatter
Create a Formatter instance
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tty/option/formatter.rb', line 59 def initialize(parameters, usage, param_display: DEFAULT_PARAM_DISPLAY, width: DEFAULT_WIDTH, order: DEFAULT_ORDER, indent: 0) @parameters = parameters @usage = usage @param_display = param_display @order = order @width = width @indent = indent @space_indent = SPACE * indent @param_indent = indent + 2 @section_names = { usage: "Usage:", arguments: "Arguments:", keywords: "Keywords:", options: "Options:", env: "Environment:", examples: "Examples:" } end |
Class Method Details
.help(parameters, usage, **config, &block) ⇒ String
Generate help for parameters and usage
39 40 41 |
# File 'lib/tty/option/formatter.rb', line 39 def self.help(parameters, usage, **config, &block) new(parameters, usage, **config).help(&block) end |
Instance Method Details
#help {|sections| ... } ⇒ String
Generate help display
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tty/option/formatter.rb', line 89 def help sections = Sections.new sections.add(:header, help_header) if @usage.header? sections.add(:banner, ) sections.add(:description, help_description) if @usage.desc? if @parameters.arguments.any?(&:display?) sections.add(:arguments, help_arguments) end if @parameters.keywords.any?(&:display?) sections.add(:keywords, help_keywords) end if @parameters. sections.add(:options, ) end if @parameters.environments.any?(&:display?) sections.add(:environments, help_environments) end sections.add(:examples, help_examples) if @usage.example? sections.add(:footer, ) if @usage. yield(sections) if block_given? formatted = sections.reject(&:empty?).join(NEWLINE) formatted.end_with?(NEWLINE) ? formatted : formatted + NEWLINE end |
#help_arguments ⇒ String
Generate help arguments
165 166 167 168 169 170 |
# File 'lib/tty/option/formatter.rb', line 165 def help_arguments "#{NEWLINE}#{@space_indent}#{@section_names[:arguments]}#{NEWLINE}" + format_section(@parameters.arguments, ->(param) do @param_display.(param.name) end) end |
#help_banner ⇒ String
Generate help banner
141 142 143 |
# File 'lib/tty/option/formatter.rb', line 141 def (@usage. ? @usage. : format_usage) end |
#help_description ⇒ String
Generate help description
153 154 155 |
# File 'lib/tty/option/formatter.rb', line 153 def help_description "#{NEWLINE}#{format_description}" end |
#help_environments ⇒ String
Generate help environment variables
208 209 210 211 |
# File 'lib/tty/option/formatter.rb', line 208 def help_environments "#{NEWLINE}#{@space_indent}#{@section_names[:env]}#{NEWLINE}" + format_section(@order.(@parameters.environments)) end |
#help_examples ⇒ String
Generate help examples
221 222 223 224 |
# File 'lib/tty/option/formatter.rb', line 221 def help_examples "#{NEWLINE}#{@space_indent}#{@section_names[:examples]}#{NEWLINE}" + format_examples end |
#help_footer ⇒ String
Generate help footer
234 235 236 |
# File 'lib/tty/option/formatter.rb', line 234 def "#{NEWLINE}#{format_multiline(@usage., @indent)}" end |
#help_header ⇒ String
Generate help header
129 130 131 |
# File 'lib/tty/option/formatter.rb', line 129 def help_header "#{format_multiline(@usage.header, @indent)}#{NEWLINE}" end |
#help_keywords ⇒ String
Generate help keywords
180 181 182 183 184 185 |
# File 'lib/tty/option/formatter.rb', line 180 def help_keywords "#{NEWLINE}#{@space_indent}#{@section_names[:keywords]}#{NEWLINE}" + format_section(@parameters.keywords, ->(param) do kwarg_param_display(param).split("=").map(&@param_display).join("=") end) end |