Olib 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,81 +1,81 @@
1
- module Olib
2
- class ScriptVars
3
- attr_accessor :opts
4
- def initialize
5
- opts = {}
6
- opts[:flags] = {}
7
- return opts if Script.current.vars.empty?
8
- list = Script.current.vars.map(&:downcase).last(Script.current.vars.length-1)
9
-
10
- unless list.first.start_with?('--')
11
- opts[:cmd] = list.shift
12
- end
13
-
14
- # iterate over list for flag values
15
- flags = list.compact.join(' ').split('--')
16
- flags.shift
17
- flags.each { |flag|
18
- segments = flag.split(' ')
19
- name = symbolize(segments.shift)
20
- opts[:flags][name] = true
21
- if !segments.empty?
22
- value = segments.join(' ').strip
23
- if value =~ /[\d]+/
24
- value = value.to_i
25
- end
26
- opts[:flags][name] = value
27
- end
28
- }
29
-
30
- @opts = opts
31
- self
32
- end
33
-
34
- def cmd
35
- @opts[:cmd]
36
- end
37
-
38
- def empty?(flag)
39
- opts[:flags][flag].class == TrueClass || opts[:flags][flag].class == NilClass
40
- end
41
-
42
- def cmd?(action)
43
- cmd == action
44
- end
45
-
46
- def symbolize(flag)
47
- flag.gsub('--', '').gsub('-', '_').to_sym
48
- end
49
-
50
- def help?
51
- cmd =~ /help/
52
- end
53
-
54
- def flags
55
- opts[:flags].keys
56
- end
57
-
58
- def to_s
59
- @opts.to_s
60
- end
61
-
62
- def flag
63
- self
64
- end
65
-
66
- def flag?(f)
67
- opts[:flags][ symbolize(f) ]
68
- end
69
-
70
- def method_missing(arg1, arg2=nil)
71
- @opts[:flags][arg1]
72
- end
73
-
74
- end
75
- end
76
-
77
- module Olib
78
- def Olib.CLI
79
- Olib::ScriptVars.new
80
- end
1
+ module Olib
2
+ class ScriptVars
3
+ attr_accessor :opts
4
+ def initialize
5
+ opts = {}
6
+ opts[:flags] = {}
7
+ return opts if Script.current.vars.empty?
8
+ list = Script.current.vars.map(&:downcase).last(Script.current.vars.length-1)
9
+
10
+ unless list.first.start_with?('--')
11
+ opts[:cmd] = list.shift
12
+ end
13
+
14
+ # iterate over list for flag values
15
+ flags = list.compact.join(' ').split('--')
16
+ flags.shift
17
+ flags.each { |flag|
18
+ segments = flag.split(' ')
19
+ name = symbolize(segments.shift)
20
+ opts[:flags][name] = true
21
+ if !segments.empty?
22
+ value = segments.join(' ').strip
23
+ if value =~ /[\d]+/
24
+ value = value.to_i
25
+ end
26
+ opts[:flags][name] = value
27
+ end
28
+ }
29
+
30
+ @opts = opts
31
+ self
32
+ end
33
+
34
+ def cmd
35
+ @opts[:cmd]
36
+ end
37
+
38
+ def empty?(flag)
39
+ opts[:flags][flag].class == TrueClass || opts[:flags][flag].class == NilClass
40
+ end
41
+
42
+ def cmd?(action)
43
+ cmd == action
44
+ end
45
+
46
+ def symbolize(flag)
47
+ flag.gsub('--', '').gsub('-', '_').to_sym
48
+ end
49
+
50
+ def help?
51
+ cmd =~ /help/
52
+ end
53
+
54
+ def flags
55
+ opts[:flags].keys
56
+ end
57
+
58
+ def to_s
59
+ @opts.to_s
60
+ end
61
+
62
+ def flag
63
+ self
64
+ end
65
+
66
+ def flag?(f)
67
+ opts[:flags][ symbolize(f) ]
68
+ end
69
+
70
+ def method_missing(arg1, arg2=nil)
71
+ @opts[:flags][arg1]
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ module Olib
78
+ def Olib.CLI
79
+ Olib::ScriptVars.new
80
+ end
81
81
  end
@@ -1,166 +1,166 @@
1
- module Olib
2
- class HelpMenu
3
- attr_accessor :script, :cmds, :last_added, :flags, :title, :padding, :cols, :max_column_width
4
-
5
- def initialize
6
- @script = $lich_char+Olib.script.to_s
7
- @cmds = {}
8
- @flags = {}
9
- @padding = 5
10
- @max_column_width = Vars.max_column_width.to_i || 50
11
- @title = "#{@script} help menu".upcase
12
- self
13
- end
14
-
15
- def flag(flag, info)
16
- if @last_added
17
- @cmds[@last_added][:flags][flag] = info
18
- else
19
- @flags[flag] = info
20
- end
21
-
22
- self
23
- end
24
-
25
- def pad
26
- [''] * @padding * ' '
27
- end
28
-
29
- def cmd(cmd, info)
30
- @last_added = cmd
31
- @cmds[cmd] = {}
32
- @cmds[cmd][:info] = info
33
- @cmds[cmd][:flags] = {}
34
- self
35
- end
36
-
37
- def width
38
- return @cols if @cols
39
-
40
- @cols = {}
41
- @cols[:one] = @script.length+padding
42
-
43
- # global flags and commands
44
- @cols[:two] = @flags
45
- .keys
46
- .concat(@cmds.keys)
47
- .map(&:strip)
48
- .sort_by(&:length).last.length+padding
49
-
50
- # flags
51
- @cols[:three] = @cmds
52
- .keys
53
- .map { |cmd| @cmds[cmd][:flags].keys }
54
- .flatten
55
- .map(&:strip)
56
- .sort_by(&:length).last.length+padding
57
-
58
- # help text
59
- @cols[:four] = @max_column_width+padding
60
-
61
- @cols[:total] = @cols.values.reduce(&:+)
62
-
63
- @cols
64
- end
65
-
66
- def center(str)
67
- "%#{width.values.reduce(&:+)/3-str.length}s\n" % str
68
- end
69
-
70
- # offset the entire array of eles by n number of blank strings
71
- def offset(n, *eles)
72
- row *(eles.unshift *[''] * n)
73
- end
74
-
75
- def row(*columns)
76
- "%#{width[:one]}s %#{width[:two]}s#{pad}%-#{width[:three]}s#{pad}%-#{width[:four]}s\n" % columns.map(&:strip)
77
- #row2 *columns
78
- end
79
-
80
- def bar
81
- "|\n".rjust(width[:total]+10,"-")
82
- end
83
-
84
- def n
85
- "\n"
86
- end
87
-
88
- def chunker(content)
89
- rows = ['']
90
-
91
- content.split.each { |chunk|
92
- if rows.last.length + chunk.length > @max_column_width then rows.push chunk else rows.last.concat " "+chunk end
93
- }
94
-
95
- rows
96
- end
97
-
98
- def write
99
- m = []
100
- m.push bar
101
- m.push n
102
- m.push "#{@title}:"
103
- m.push n
104
- m.push n
105
- m.push bar
106
- unless @flags.keys.empty?
107
- m.push *["global flags:", "", "", ""].map(&:upcase)
108
- m.push bar
109
- @flags.each { |flag, info|
110
- if info.length > @max_column_width
111
- chunks = chunker info
112
- m.push row( @script, '', '--'+flag, chunks.shift )
113
- chunks.each { |chunk| m.push offset 3, chunk }
114
- m.push n
115
- else
116
- m.push row(@script, '', '--'+flag, info)
117
- m.push n
118
- end
119
-
120
- }
121
- end
122
- m.push n
123
- unless @cmds.keys.empty?
124
- m.push bar
125
- m.push row *['', "| cmd", "| flag", "| info"].map(&:upcase)
126
- m.push bar
127
- @cmds.keys.each { |cmd|
128
- # add top level command
129
- m.push n
130
- if @cmds[cmd][:info].length > @max_column_width
131
- chunks = chunker @cmds[cmd][:info]
132
- m.push row(@script, cmd, '', chunks.shift)
133
- chunks.each { |chunk| m.push offset 3, chunk }
134
- m.push n
135
- else
136
- m.push row(@script, cmd, '', @cmds[cmd][:info])
137
- m.push n
138
- end
139
-
140
- # add flags for command
141
- @cmds[cmd][:flags].keys.each {|flag|
142
- if @cmds[cmd][:flags][flag].length > @max_column_width
143
- chunks = chunker @cmds[cmd][:flags][flag]
144
- m.push row( @script, cmd, '--'+flag, chunks.shift )
145
- chunks.each { |chunk| m.push offset 3, chunk }
146
- m.push n
147
- else
148
- m.push row(@script, cmd, '--'+flag, @cmds[cmd][:flags][flag] )
149
- m.push n
150
- end
151
- }
152
-
153
- }
154
- m.push bar
155
- m.push n
156
- end
157
- respond m.join('')
158
- end
159
- end
160
- end
161
-
162
-
163
-
164
-
165
-
166
-
1
+ module Olib
2
+ class HelpMenu
3
+ attr_accessor :script, :cmds, :last_added, :flags, :title, :padding, :cols, :max_column_width
4
+
5
+ def initialize
6
+ @script = $lich_char+Olib.script.to_s
7
+ @cmds = {}
8
+ @flags = {}
9
+ @padding = 5
10
+ @max_column_width = Vars.max_column_width.to_i || 100
11
+ @title = "#{@script} help menu".upcase
12
+ self
13
+ end
14
+
15
+ def flag(flag, info)
16
+ if @last_added
17
+ @cmds[@last_added][:flags][flag] = info
18
+ else
19
+ @flags[flag] = info
20
+ end
21
+
22
+ self
23
+ end
24
+
25
+ def pad
26
+ [''] * @padding * ' '
27
+ end
28
+
29
+ def cmd(cmd, info)
30
+ @last_added = cmd
31
+ @cmds[cmd] = {}
32
+ @cmds[cmd][:info] = info
33
+ @cmds[cmd][:flags] = {}
34
+ self
35
+ end
36
+
37
+ def width
38
+ return @cols if @cols
39
+
40
+ @cols = {}
41
+ @cols[:one] = @script.length+padding
42
+
43
+ # global flags and commands
44
+ @cols[:two] = @flags
45
+ .keys
46
+ .concat(@cmds.keys)
47
+ .map(&:strip)
48
+ .sort_by(&:length).last.length+padding
49
+
50
+ # flags
51
+ @cols[:three] = @cmds
52
+ .keys
53
+ .map { |cmd| @cmds[cmd][:flags].keys }
54
+ .flatten
55
+ .map(&:strip)
56
+ .sort_by(&:length).last.length+padding
57
+
58
+ # help text
59
+ @cols[:four] = @max_column_width+padding
60
+
61
+ @cols[:total] = @cols.values.reduce(&:+)
62
+
63
+ @cols
64
+ end
65
+
66
+ def center(str)
67
+ "%#{width.values.reduce(&:+)/3-str.length}s\n" % str
68
+ end
69
+
70
+ # offset the entire array of eles by n number of blank strings
71
+ def offset(n, *eles)
72
+ row *(eles.unshift *[''] * n)
73
+ end
74
+
75
+ def row(*columns)
76
+ "%#{width[:one]}s %#{width[:two]}s#{pad}%-#{width[:three]}s#{pad}%-#{width[:four]}s\n" % columns.map(&:strip)
77
+ #row2 *columns
78
+ end
79
+
80
+ def bar
81
+ "|\n".rjust(width[:total]+10,"-")
82
+ end
83
+
84
+ def n
85
+ "\n"
86
+ end
87
+
88
+ def chunker(content)
89
+ rows = ['']
90
+
91
+ content.split.each { |chunk|
92
+ if rows.last.length + chunk.length > @max_column_width then rows.push chunk else rows.last.concat " "+chunk end
93
+ }
94
+
95
+ rows
96
+ end
97
+
98
+ def write
99
+ m = []
100
+ m.push bar
101
+ m.push n
102
+ m.push "#{@title}:"
103
+ m.push n
104
+ m.push n
105
+ m.push bar
106
+ unless @flags.keys.empty?
107
+ m.push *["global flags:", "", "", ""].map(&:upcase)
108
+ m.push bar
109
+ @flags.each { |flag, info|
110
+ if info.length > @max_column_width
111
+ chunks = chunker info
112
+ m.push row( @script, '', '--'+flag, chunks.shift )
113
+ chunks.each { |chunk| m.push offset 3, chunk }
114
+ m.push n
115
+ else
116
+ m.push row(@script, '', '--'+flag, info)
117
+ m.push n
118
+ end
119
+
120
+ }
121
+ end
122
+ m.push n
123
+ unless @cmds.keys.empty?
124
+ m.push bar
125
+ m.push row *['', "| cmd", "| flag", "| info"].map(&:upcase)
126
+ m.push bar
127
+ @cmds.keys.each { |cmd|
128
+ # add top level command
129
+ m.push n
130
+ if @cmds[cmd][:info].length > @max_column_width
131
+ chunks = chunker @cmds[cmd][:info]
132
+ m.push row(@script, cmd, '', chunks.shift)
133
+ chunks.each { |chunk| m.push offset 3, chunk }
134
+ m.push n
135
+ else
136
+ m.push row(@script, cmd, '', @cmds[cmd][:info])
137
+ m.push n
138
+ end
139
+
140
+ # add flags for command
141
+ @cmds[cmd][:flags].keys.each {|flag|
142
+ if @cmds[cmd][:flags][flag].length > @max_column_width
143
+ chunks = chunker @cmds[cmd][:flags][flag]
144
+ m.push row( @script, cmd, '--'+flag, chunks.shift )
145
+ chunks.each { |chunk| m.push offset 3, chunk }
146
+ m.push n
147
+ else
148
+ m.push row(@script, cmd, '--'+flag, @cmds[cmd][:flags][flag] )
149
+ m.push n
150
+ end
151
+ }
152
+
153
+ }
154
+ m.push bar
155
+ m.push n
156
+ end
157
+ respond m.join('')
158
+ end
159
+ end
160
+ end
161
+
162
+
163
+
164
+
165
+
166
+