Class: Olib::HelpMenu

Inherits:
Object
  • Object
show all
Defined in:
lib/Olib/utils/help_menu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHelpMenu

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

#cmdsObject

Returns the value of attribute cmds


3
4
5
# File 'lib/Olib/utils/help_menu.rb', line 3

def cmds
  @cmds
end

#colsObject

Returns the value of attribute cols


3
4
5
# File 'lib/Olib/utils/help_menu.rb', line 3

def cols
  @cols
end

#flagsObject

Returns the value of attribute flags


3
4
5
# File 'lib/Olib/utils/help_menu.rb', line 3

def flags
  @flags
end

#last_addedObject

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_widthObject

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

#paddingObject

Returns the value of attribute padding


3
4
5
# File 'lib/Olib/utils/help_menu.rb', line 3

def padding
  @padding
end

#scriptObject

Returns the value of attribute script


3
4
5
# File 'lib/Olib/utils/help_menu.rb', line 3

def script
  @script
end

#titleObject

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

#barObject


80
81
82
# File 'lib/Olib/utils/help_menu.rb', line 80

def bar
  "|\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

#nObject


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

#padObject


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

#widthObject


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

#writeObject


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 bar
  m.push n
  m.push "#{@title}:"
  m.push n
  m.push n
  m.push bar
  unless @flags.keys.empty?
    m.push *["global flags:", "", "", ""].map(&:upcase)
    m.push bar
    @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 bar
    m.push row *['', "| cmd", "| flag", "| info"].map(&:upcase)
    m.push bar
    @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 bar
    m.push n
  end
  respond m.join('')
end