Class: Fuelcell::Help::UsageFormatter

Inherits:
BaseFormatter show all
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

#max_width, #padding, #width

Instance Method Summary collapse

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

Parameters:

  • text (String)

    the current usage line

  • str (String)

    the string to be appended

  • line_width (Int)

    width of current line

Returns:

  • (Array)

    append text & size



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.

Parameters:

  • text (String)

    text of all currently appended options

  • opt (String)

    text of the current option

  • line_width (Int)

    width of the current line including padding

  • align_width (Int)

    opts will align to this width after wrapping



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.

Parameters:

  • data (Hash)

Returns:

  • (Array)

    formatted text, size of 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.

Parameters:

  • opts (Hash)
  • align (Int)

Returns:

  • (String)


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