Class: Fuelcell::Help::UsageFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- Fuelcell::Help::UsageFormatter
- Defined in:
- lib/fuelcell/help/usage_formatter.rb
Overview
Formats help text to be displayed on the command line. The formatter works with help text structured in a hash.
Constant Summary
Constants inherited from BaseFormatter
BaseFormatter::DEFAULT_PADDING, BaseFormatter::DEFAULT_WIDTH
Instance Attribute Summary
Attributes inherited from BaseFormatter
Instance Method Summary collapse
-
#append_current_line(text, str, line_width) ⇒ Array
Used to append to the usage line when its shorted then the terminal window.
-
#append_nextline(text, str, align_width) ⇒ Object
Used append a newline to the usage and wrap the text because it is long then the terminal width.
-
#append_opts(text, opt, line_width, align_width) ⇒ Object
Append a single opt text to the usage line.
- #call(data) ⇒ Object
-
#format_cmd(data) ⇒ Array
Format the label and the path which make the up the first line of usage text.
-
#format_opts(opts, align_width) ⇒ String
Format the options so that they wrap at the max width of the terminal and that they align after the command path.
- #opt_display(data) ⇒ Object
Methods inherited from BaseFormatter
#initialize, #long_opt, #short_opt
Constructor Details
This class inherits a constructor from Fuelcell::Help::BaseFormatter
Instance Method Details
#append_current_line(text, str, line_width) ⇒ Array
Used to append to the usage line when its shorted then the terminal window
67 68 69 70 71 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 67 def append_current_line(text, str, line_width) line_width += str.size + 1 text << str + ' ' [text, line_width] end |
#append_nextline(text, str, align_width) ⇒ Object
Used append a newline to the usage and wrap the text because it is long then the terminal width
76 77 78 79 80 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 76 def append_nextline(text, str, align_width) text = text.strip + "\n" text << (' ' * align_width) + str [text, text.size] end |
#append_opts(text, opt, line_width, align_width) ⇒ Object
Append a single opt text to the usage line. Ensure wrapping occurs correctly & wrapped opt aligns with the start of the first opt.
The align width is the amount of padding necessary in order to align all the opt text from the left.
51 52 53 54 55 56 57 58 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 51 def append_opts(text, opt, line_width, align_width) str = opt_display(opt) if (line_width + str.size) < max_width return append_current_line(text, str, line_width) end append_nextline(text, str, align_width) end |
#call(data) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 7 def call(data) usage = data[:usage] || {} text = format_cmd(usage) opts = format_opts(usage[:opts], text.size + 1) text += ' ' + opts unless opts.empty? "#{text}\n" end |
#format_cmd(data) ⇒ Array
Format the label and the path which make the up the first line of usage text.
36 37 38 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 36 def format_cmd(data) "#{data[:label]} #{data[:path]}" end |
#format_opts(opts, align_width) ⇒ String
Format the options so that they wrap at the max width of the terminal and that they align after the command path.
22 23 24 25 26 27 28 29 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 22 def format_opts(opts, align_width) text = '' line_width = align_width opts.each do |opt| text, line_width = append_opts(text, opt, line_width, align_width) end text.strip end |
#opt_display(data) ⇒ Object
82 83 84 85 |
# File 'lib/fuelcell/help/usage_formatter.rb', line 82 def opt_display(data) text = "#{short_opt(data)} #{long_opt(data)}".strip "[#{text}]" end |