Class: Fuelcell::Help::OptsFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/fuelcell/help/opts_formatter.rb

Constant Summary

Constants inherited from BaseFormatter

BaseFormatter::DEFAULT_PADDING, BaseFormatter::DEFAULT_WIDTH

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#max_width, #padding, #width

Instance Method Summary collapse

Methods inherited from BaseFormatter

#long_opt, #short_opt

Constructor Details

#initialize(config = {}) ⇒ OptsFormatter

Returns a new instance of OptsFormatter.



5
6
7
8
9
# File 'lib/fuelcell/help/opts_formatter.rb', line 5

def initialize(config = {})
  super
  @indent       = (config[:indent] || 2).to_i
  @banner_space = (config[:banner_space] || 2).to_i
end

Instance Attribute Details

Returns the value of attribute banner_space.



4
5
6
# File 'lib/fuelcell/help/opts_formatter.rb', line 4

def banner_space
  @banner_space
end

#indentObject (readonly)

Returns the value of attribute indent.



4
5
6
# File 'lib/fuelcell/help/opts_formatter.rb', line 4

def indent
  @indent
end

Instance Method Details

#call(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fuelcell/help/opts_formatter.rb', line 11

def call(data)
  return '' if empty?(data)

  str    = "Options:\n"
  widest = widest_opt(data[:options])
  data[:options].each do |opt|
    opt, banner = opt_data(opt)
    str << line(opt, widest, banner)
  end
  str
end

#create_padding(widest, opt) ⇒ Object



32
33
34
# File 'lib/fuelcell/help/opts_formatter.rb', line 32

def create_padding(widest, opt)
  ' ' * (widest - opt.size)
end

#empty?(data) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/fuelcell/help/opts_formatter.rb', line 36

def empty?(data)
  !data.key?(:options) || data[:options].empty?
end

#line(opt, widest_opt, banner_text) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/fuelcell/help/opts_formatter.rb', line 23

def line(opt, widest_opt, banner_text)
  max          = indent + banner_space + widest_opt
  banner       = wrap(banner_text, max)
  pad          = create_padding(widest_opt, opt)
  indent_str   = ' ' * indent
  column_space = ' ' * banner_space
  "#{indent_str}#{opt}#{pad}#{column_space}#{banner}\n"
end

#opt_data(data) ⇒ Object



57
58
59
# File 'lib/fuelcell/help/opts_formatter.rb', line 57

def opt_data(data)
  ["#{short_opt(data, '  ')} #{long_opt(data)}", data[:banner] || '']
end

#widest_opt(options) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/fuelcell/help/opts_formatter.rb', line 48

def widest_opt(options)
  max = 0
  options.each do |data|
    size = "#{short_opt(data, '  ')} #{long_opt(data)}".size
    max  = size if size > max
  end
  max
end

#wrap(text, widest_text) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/fuelcell/help/opts_formatter.rb', line 40

def wrap(text, widest_text)
  return text if (widest_text + text.size) < max_width

  pad = ' ' * widest_text
  pattern = /(.{1,#{max_width - widest_text}})(\s+|$)/
  text.gsub(pattern, "\\1\n#{pad}").strip
end