Class: Olib::HelpMenu
- Inherits:
-
Object
- Object
- Olib::HelpMenu
- Defined in:
- lib/Olib/utils/help_menu.rb
Instance Attribute Summary collapse
-
#cmds ⇒ Object
Returns the value of attribute cmds.
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#last_added ⇒ Object
Returns the value of attribute last_added.
-
#max_column_width ⇒ Object
Returns the value of attribute max_column_width.
-
#padding ⇒ Object
Returns the value of attribute padding.
-
#script ⇒ Object
Returns the value of attribute script.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #bar ⇒ Object
- #center(str) ⇒ Object
- #chunker(content) ⇒ Object
- #cmd(cmd, info) ⇒ Object
- #flag(flag, info) ⇒ Object
-
#initialize ⇒ HelpMenu
constructor
A new instance of HelpMenu.
- #n ⇒ Object
-
#offset(n, *eles) ⇒ Object
offset the entire array of eles by n number of blank strings.
- #pad ⇒ Object
- #row(*columns) ⇒ Object
- #width ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize ⇒ HelpMenu
Returns a new instance of HelpMenu.
5 6 7 8 9 10 11 12 13 |
# File 'lib/Olib/utils/help_menu.rb', line 5 def initialize @script = $lich_char+Olib.script.to_s @cmds = {} @flags = {} @padding = 5 @max_column_width = Vars.max_column_width.to_i || 100 @title = "#{@script} help menu".upcase self end |
Instance Attribute Details
#cmds ⇒ Object
Returns the value of attribute cmds.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def cmds @cmds end |
#cols ⇒ Object
Returns the value of attribute cols.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def cols @cols end |
#flags ⇒ Object
Returns the value of attribute flags.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def flags @flags end |
#last_added ⇒ Object
Returns the value of attribute last_added.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def last_added @last_added end |
#max_column_width ⇒ Object
Returns the value of attribute max_column_width.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def max_column_width @max_column_width end |
#padding ⇒ Object
Returns the value of attribute padding.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def padding @padding end |
#script ⇒ Object
Returns the value of attribute script.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def script @script end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/Olib/utils/help_menu.rb', line 3 def title @title end |
Instance Method Details
#bar ⇒ Object
80 81 82 |
# File 'lib/Olib/utils/help_menu.rb', line 80 def "|\n".rjust(width[:total]+10,"-") end |
#center(str) ⇒ Object
66 67 68 |
# File 'lib/Olib/utils/help_menu.rb', line 66 def center(str) "%#{width.values.reduce(&:+)/3-str.length}s\n" % str end |
#chunker(content) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/Olib/utils/help_menu.rb', line 88 def chunker(content) rows = [''] content.split.each { |chunk| if rows.last.length + chunk.length > @max_column_width then rows.push chunk else rows.last.concat " "+chunk end } rows end |
#cmd(cmd, info) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/Olib/utils/help_menu.rb', line 29 def cmd(cmd, info) @last_added = cmd @cmds[cmd] = {} @cmds[cmd][:info] = info @cmds[cmd][:flags] = {} self end |
#flag(flag, info) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/Olib/utils/help_menu.rb', line 15 def flag(flag, info) if @last_added @cmds[@last_added][:flags][flag] = info else @flags[flag] = info end self end |
#n ⇒ Object
84 85 86 |
# File 'lib/Olib/utils/help_menu.rb', line 84 def n "\n" end |
#offset(n, *eles) ⇒ Object
offset the entire array of eles by n number of blank strings
71 72 73 |
# File 'lib/Olib/utils/help_menu.rb', line 71 def offset(n, *eles) row *(eles.unshift *[''] * n) end |
#pad ⇒ Object
25 26 27 |
# File 'lib/Olib/utils/help_menu.rb', line 25 def pad [''] * @padding * ' ' end |
#row(*columns) ⇒ Object
75 76 77 78 |
# File 'lib/Olib/utils/help_menu.rb', line 75 def row(*columns) "%#{width[:one]}s %#{width[:two]}s#{pad}%-#{width[:three]}s#{pad}%-#{width[:four]}s\n" % columns.map(&:strip) #row2 *columns end |
#width ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/Olib/utils/help_menu.rb', line 37 def width return @cols if @cols @cols = {} @cols[:one] = @script.length+padding # global flags and commands @cols[:two] = @flags .keys .concat(@cmds.keys) .map(&:strip) .sort_by(&:length).last.length+padding # flags @cols[:three] = @cmds .keys .map { |cmd| @cmds[cmd][:flags].keys } .flatten .map(&:strip) .sort_by(&:length).last.length+padding # help text @cols[:four] = @max_column_width+padding @cols[:total] = @cols.values.reduce(&:+) @cols end |
#write ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/Olib/utils/help_menu.rb', line 98 def write m = [] m.push m.push n m.push "#{@title}:" m.push n m.push n m.push unless @flags.keys.empty? m.push *["global flags:", "", "", ""].map(&:upcase) m.push @flags.each { |flag, info| if info.length > @max_column_width chunks = chunker info m.push row( @script, '', '--'+flag, chunks.shift ) chunks.each { |chunk| m.push offset 3, chunk } m.push n else m.push row(@script, '', '--'+flag, info) m.push n end } end m.push n unless @cmds.keys.empty? m.push m.push row *['', "| cmd", "| flag", "| info"].map(&:upcase) m.push @cmds.keys.each { |cmd| # add top level command m.push n if @cmds[cmd][:info].length > @max_column_width chunks = chunker @cmds[cmd][:info] m.push row(@script, cmd, '', chunks.shift) chunks.each { |chunk| m.push offset 3, chunk } m.push n else m.push row(@script, cmd, '', @cmds[cmd][:info]) m.push n end # add flags for command @cmds[cmd][:flags].keys.each {|flag| if @cmds[cmd][:flags][flag].length > @max_column_width chunks = chunker @cmds[cmd][:flags][flag] m.push row( @script, cmd, '--'+flag, chunks.shift ) chunks.each { |chunk| m.push offset 3, chunk } m.push n else m.push row(@script, cmd, '--'+flag, @cmds[cmd][:flags][flag] ) m.push n end } } m.push m.push n end respond m.join('') end |