Class: Fuelcell::Help::CmdsFormatter
Constant Summary
BaseFormatter::DEFAULT_PADDING, BaseFormatter::DEFAULT_WIDTH
Instance Attribute Summary collapse
#max_width, #padding, #width
Instance Method Summary
collapse
#long_opt, #short_opt
Constructor Details
#initialize(config = {}) ⇒ CmdsFormatter
Returns a new instance of CmdsFormatter.
5
6
7
8
9
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 5
def initialize(config = {})
super
@indent = (config[:indent] || 2).to_i
@desc_space = (config[:banner_space] || 2).to_i
end
|
Instance Attribute Details
#desc_space ⇒ Object
Returns the value of attribute desc_space.
4
5
6
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 4
def desc_space
@desc_space
end
|
#indent ⇒ Object
Returns the value of attribute indent.
4
5
6
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 4
def indent
@indent
end
|
Instance Method Details
#call(data) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 11
def call(data)
return '' if empty?(data)
str = "Commands:\n"
widest = widest_cmd(data[:commands])
data[:commands].each do |cmd|
str << line(cmd[:name], widest, cmd[:desc])
end
str
end
|
#create_padding(widest, cmd) ⇒ Object
30
31
32
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 30
def create_padding(widest, cmd)
' ' * (widest - cmd.size)
end
|
#empty?(data) ⇒ Boolean
34
35
36
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 34
def empty?(data)
!data.key?(:commands) || data[:commands].empty?
end
|
#line(cmd, widest_cmd, desc) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 21
def line(cmd, widest_cmd, desc)
max = indent + desc_space + widest_cmd
desc = wrap(desc || '', max)
pad = create_padding(widest_cmd, cmd)
indent_str = ' ' * indent
column_space = ' ' * desc_space
"#{indent_str}#{cmd}#{pad}#{column_space}# #{desc}\n"
end
|
#widest_cmd(commands) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 47
def widest_cmd(commands)
max = 0
commands.each do |data|
size = data[:name].size
max = size if size > max
end
max
end
|
#wrap(text, widest_text) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/fuelcell/help/cmds_formatter.rb', line 38
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 = text.gsub(pattern, "\\1\n#{pad}# ")
text.gsub(/# $/, '').strip
end
|