cowtech-lib 1.9.8.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -2
- data/README.rdoc +2 -2
- data/Rakefile +17 -11
- data/cowtech-lib.gemspec +3 -5
- data/lib/cowtech-lib.rb +9 -27
- data/lib/cowtech-lib/console.rb +325 -346
- data/lib/cowtech-lib/option_parser.rb +326 -347
- data/lib/cowtech-lib/script.rb +92 -113
- data/lib/cowtech-lib/shell.rb +351 -372
- data/lib/cowtech-lib/version.rb +14 -9
- metadata +7 -9
- data/LICENSE.txt +0 -20
data/Gemfile
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the cowtech-lib gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
2
6
|
|
3
|
-
|
7
|
+
source "http://rubygems.org"
|
8
|
+
|
9
|
+
gem "jeweler"
|
data/README.rdoc
CHANGED
@@ -15,5 +15,5 @@ http://github.com/ShogunPanda/cowtech-lib
|
|
15
15
|
|
16
16
|
== Copyright
|
17
17
|
|
18
|
-
Copyright (
|
19
|
-
|
18
|
+
Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
19
|
+
Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
data/Rakefile
CHANGED
@@ -1,17 +1,23 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the cowtech-lib gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
require "jeweler"
|
3
9
|
require "./lib/cowtech-lib/version.rb"
|
4
10
|
|
5
11
|
Jeweler::Tasks.new do |gem|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
gem.name = "cowtech-lib"
|
13
|
+
gem.homepage = "http://github.com/ShogunPanda/cowtech-lib"
|
14
|
+
gem.license = "MIT"
|
15
|
+
gem.summary = %Q{A general purpose utility library.}
|
16
|
+
gem.description = %Q{A general purpose utility library.}
|
17
|
+
gem.email = "shogun_panda@me.com"
|
18
|
+
gem.authors = ["Shogun"]
|
19
|
+
gem.version = Cowtech::Lib::Version::STRING
|
20
|
+
gem.add_dependency "open4"
|
15
21
|
end
|
16
22
|
|
17
23
|
Jeweler::RubygemsDotOrgTasks.new
|
data/cowtech-lib.gemspec
CHANGED
@@ -5,21 +5,19 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "cowtech-lib"
|
8
|
-
s.version = "
|
8
|
+
s.version = "2.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Shogun"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-04-16"
|
13
13
|
s.description = "A general purpose utility library."
|
14
14
|
s.email = "shogun_panda@me.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
|
-
"LICENSE.txt",
|
17
16
|
"README.rdoc"
|
18
17
|
]
|
19
18
|
s.files = [
|
20
19
|
".document",
|
21
20
|
"Gemfile",
|
22
|
-
"LICENSE.txt",
|
23
21
|
"README.rdoc",
|
24
22
|
"Rakefile",
|
25
23
|
"cowtech-lib.gemspec",
|
@@ -33,7 +31,7 @@ Gem::Specification.new do |s|
|
|
33
31
|
s.homepage = "http://github.com/ShogunPanda/cowtech-lib"
|
34
32
|
s.licenses = ["MIT"]
|
35
33
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = "1.8.
|
34
|
+
s.rubygems_version = "1.8.15"
|
37
35
|
s.summary = "A general purpose utility library."
|
38
36
|
|
39
37
|
if s.respond_to? :specification_version then
|
data/lib/cowtech-lib.rb
CHANGED
@@ -1,33 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# Copyright © 2011 and above Shogun
|
6
|
-
# Released under the MIT License, which follows.
|
7
|
-
#
|
8
|
-
# This program is free software; you can redistribute it and/or
|
9
|
-
# modify it under the terms of the GNU General Public License
|
10
|
-
# as published by the Free Software Foundation; either version 2
|
11
|
-
# of the License, or (at your option) any later version.
|
12
|
-
#
|
13
|
-
# This program is distributed in the hope that it will be useful,
|
14
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
-
# GNU General Public License for more details.
|
17
|
-
#
|
18
|
-
# You should have received a copy of the GNU General Public License
|
19
|
-
# along with this program; if not, write to the Free Software
|
20
|
-
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
21
|
-
#
|
22
|
-
# A copy of the GNU General Public License is available at: http://www.gnu.org/licenses/gpl.txt
|
3
|
+
# This file is part of the cowtech-lib gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
23
5
|
#
|
24
6
|
|
25
7
|
module Cowtech
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
8
|
+
module Lib
|
9
|
+
autoload :Version, "cowtech-lib/version"
|
10
|
+
autoload :Console, "cowtech-lib/console"
|
11
|
+
autoload :OptionParser, "cowtech-lib/option_parser"
|
12
|
+
autoload :Shell, "cowtech-lib/shell"
|
13
|
+
autoload :Script, "cowtech-lib/script"
|
14
|
+
end
|
33
15
|
end
|
data/lib/cowtech-lib/console.rb
CHANGED
@@ -1,360 +1,339 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# Copyright © 2011 and above Shogun
|
6
|
-
# Released under the MIT License, which follows.
|
7
|
-
#
|
8
|
-
# The MIT License
|
9
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
10
|
-
# of this software and associated documentation files (the "Software"), to deal
|
11
|
-
# in the Software without restriction, including without limitation the rights
|
12
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13
|
-
# copies of the Software, and to permit persons to whom the Software is
|
14
|
-
# furnished to do so, subject to the following conditions:
|
15
|
-
#
|
16
|
-
# The above copyright notice and this permission notice shall be included in
|
17
|
-
# all copies or substantial portions of the Software.
|
18
|
-
#
|
19
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
22
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
25
|
-
# THE SOFTWARE.
|
3
|
+
# This file is part of the cowtech-lib gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
26
5
|
#
|
27
6
|
|
28
7
|
require "rexml/document"
|
29
8
|
|
30
9
|
class Object
|
31
|
-
|
32
|
-
|
33
|
-
|
10
|
+
def force_array
|
11
|
+
self.is_a?(Array) ? self : [self]
|
12
|
+
end
|
34
13
|
end
|
35
14
|
|
36
15
|
class Hash
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
16
|
+
def method_missing(method, *arg)
|
17
|
+
# PER ORA SUPPORTIAMO SOLO I GETTER
|
18
|
+
self[method.to_sym]
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to?(method)
|
22
|
+
self.has_key?(method.to_sym)
|
23
|
+
end
|
45
24
|
end
|
46
25
|
|
47
26
|
module Cowtech
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
27
|
+
module Lib
|
28
|
+
# A class which provides some method to operate with files and to format pretty messages.
|
29
|
+
# @author Shogun
|
30
|
+
class Console
|
31
|
+
# Indentation level
|
32
|
+
attr_accessor :indent_level
|
33
|
+
|
34
|
+
# Whether show executed commands
|
35
|
+
attr_accessor :show_commands
|
36
|
+
|
37
|
+
# Whether show output of executed commands
|
38
|
+
attr_accessor :show_outputs
|
39
|
+
|
40
|
+
# Whether simply print commands rather than executing them
|
41
|
+
attr_accessor :skip_commands
|
42
|
+
|
43
|
+
# Exit status for commands
|
44
|
+
attr_reader :statuses
|
45
|
+
|
46
|
+
# Indentation string(s)
|
47
|
+
attr_accessor :indentator
|
48
|
+
|
49
|
+
# Sets indentation level.
|
50
|
+
# Arguments:
|
51
|
+
# * <em>indent</em>: The new indentation level
|
52
|
+
# * <em></em>: If the level is absolute or relative to the current level
|
53
|
+
def indent_set(level, absolute = false)
|
54
|
+
@indent_level = [(!absolute ? @indent_level : 0) + level, 0].max
|
55
|
+
end
|
56
|
+
|
57
|
+
# Resets indentation level to 0.
|
58
|
+
def indent_reset
|
59
|
+
@indent_level = 0
|
60
|
+
end
|
61
|
+
|
62
|
+
# Execute codes in an indented block
|
63
|
+
def indent_region(level, absolute = false)
|
64
|
+
old_level = @indent_level
|
65
|
+
self.indent_set(level, absolute)
|
66
|
+
yield
|
67
|
+
@indent_level = old_level
|
68
|
+
end
|
69
|
+
|
70
|
+
# Indents a message.
|
71
|
+
#
|
72
|
+
# Arguments:
|
73
|
+
# * <em>msg</em>: The message to indent
|
74
|
+
# * <em>add_additional</em>: Whether add extra space to align to initial message "*"
|
75
|
+
# Returns: The indentated message
|
76
|
+
def indent(msg, level = nil)
|
77
|
+
(@@indentator * (level || @indent_level)) + msg
|
78
|
+
end
|
79
|
+
|
80
|
+
# Substitute tag with color.
|
81
|
+
#
|
82
|
+
# Arguments:
|
83
|
+
# * <em>node</em>: The node which operate on
|
84
|
+
# * <em>stack</em>: The stack of old styles. <b>Do not set this by yourself!</b>
|
85
|
+
#
|
86
|
+
# Returns: The new text
|
87
|
+
def parse_message(node, stack = [])
|
88
|
+
rv = ""
|
89
|
+
styles = (node.name == "text" && node.attributes["style"]) ? node.attributes["style"].split(" ") : nil
|
90
|
+
|
91
|
+
# Add style of current tag
|
92
|
+
if styles then
|
93
|
+
styles.each do |style|
|
94
|
+
rv += @@styles[style] || ""
|
95
|
+
end
|
96
|
+
|
97
|
+
stack.push(styles)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Now parse subnodes
|
101
|
+
node.children.each do |child|
|
102
|
+
if child.node_type == :text then
|
103
|
+
rv += child.to_s
|
104
|
+
elsif child.name == "text" then
|
105
|
+
rv += self.parse_message(child, stack)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Remove style of current tag
|
110
|
+
if styles then
|
111
|
+
stack.pop()
|
112
|
+
|
113
|
+
# Restore previous style
|
114
|
+
(stack.pop || ["default"]).each do |style|
|
115
|
+
rv += @@styles[style]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
rv
|
120
|
+
end
|
121
|
+
|
122
|
+
# Prints a message.
|
123
|
+
# Arguments:
|
124
|
+
# * <em>msg</em>: The message to print
|
125
|
+
# * <em>dots</em>: Whether add "..." to the message
|
126
|
+
# * <em>newline</em>: Whether add a newline to the message
|
127
|
+
# * <em>plain</em>: Whether ignore tags
|
128
|
+
# * <em>must_indent</em>: Whether indent the message
|
129
|
+
# * <em>internal</em>: If the method is called by another method. <b>Do not set this by yourself!</b>
|
130
|
+
def write(args)
|
131
|
+
msg = args[:msg]
|
132
|
+
|
133
|
+
# Check some alternative syntax
|
134
|
+
[:begin, :warn, :error, :debug, :info, :right, :end].each do |t|
|
135
|
+
if args[t] then
|
136
|
+
msg = args[t]
|
137
|
+
args[:type] = t
|
138
|
+
args[t] = nil
|
139
|
+
end
|
140
|
+
end
|
141
|
+
args[:fatal] = true if args[:status] == :fail
|
142
|
+
|
143
|
+
# Check for specific msg type
|
144
|
+
if [:begin, :warn, :error, :debug, :info].include?(args[:type]) then
|
145
|
+
mc = {begin: "bold green", warn: "bold yellow", error: "bold red", debug: "magenta", info: "bold cyan"}
|
146
|
+
color = args[:color] || mc[args[:type]]
|
147
|
+
|
148
|
+
if args[:full_color] then
|
149
|
+
msg = self.indent("<text style=\"#{color}\">#{msg}</text>")
|
150
|
+
else
|
151
|
+
msg = " <text style=\"#{color}\">*</text> #{self.indent(msg)}"
|
152
|
+
end
|
153
|
+
else # Add dots and indentation if needed
|
154
|
+
msg = self.indent(msg + (args.fetch(:dots, true) ? "..." : ""), args[:indent] ? args[:indent] : @indent_level)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Parse the message
|
158
|
+
if !args[:plain] then
|
159
|
+
begin
|
160
|
+
xml = "<text>#{msg}</text>"
|
161
|
+
msg = self.parse_message(REXML::Document.new(xml).root)
|
162
|
+
rescue Exception => e
|
163
|
+
print "[ERROR] Invalid message tagging, check XML syntax (or color requested) of the following message:\n\n\t#{xml}\n\n"
|
164
|
+
print "\tThe errors was: #{e.message} (#{e.class.to_s})\n\n"
|
165
|
+
exit(1)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Add newline if needed
|
170
|
+
msg += "\n" if args.fetch(:newline, true)
|
171
|
+
|
172
|
+
if args[:internal] then
|
173
|
+
msg
|
174
|
+
else
|
175
|
+
if [:end, :right].include?(args[:type]) then
|
176
|
+
# Get screen width
|
177
|
+
@tty_width = `tput cols`.to_i if @tty_width < 0
|
178
|
+
|
179
|
+
# Get padding
|
180
|
+
pad = @tty_width - msg.inspect.gsub(/(\\e\[[0-9]+m)|(\")|(\\n)/, "").length
|
181
|
+
|
182
|
+
print "\033[A" if args[:up]
|
183
|
+
print "\033[0G\033[#{pad}C"
|
184
|
+
end
|
185
|
+
|
186
|
+
print(msg)
|
187
|
+
end
|
188
|
+
|
189
|
+
exit(args[:code] || 0) if args[:exit_after] || args[:fatal]
|
190
|
+
end
|
191
|
+
|
192
|
+
# Syntatic sugar
|
193
|
+
# Prints a warning message.
|
194
|
+
def warn(msg, args = nil)
|
195
|
+
args ||= {}
|
196
|
+
|
197
|
+
if msg.is_a?(Hash) then
|
198
|
+
args.merge!(msg)
|
199
|
+
else
|
200
|
+
args[:msg] = msg
|
201
|
+
end
|
202
|
+
|
203
|
+
args[:warn] = args[:msg]
|
204
|
+
|
205
|
+
self.write(args)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Prints an error message.
|
209
|
+
def error(msg, args = nil)
|
210
|
+
args ||= {}
|
211
|
+
|
212
|
+
if msg.is_a?(Hash) then
|
213
|
+
args.merge!(msg)
|
214
|
+
else
|
215
|
+
args[:msg] = msg
|
216
|
+
end
|
217
|
+
|
218
|
+
args[:error] = args[:msg]
|
219
|
+
|
220
|
+
self.write(args)
|
221
|
+
end
|
222
|
+
|
223
|
+
# Prints and error message then abort.
|
224
|
+
def fatal(msg, args = nil)
|
225
|
+
args ||= {}
|
226
|
+
|
227
|
+
if msg.is_a?(Hash) then
|
228
|
+
args.merge!(msg)
|
229
|
+
else
|
230
|
+
args[:msg] = msg
|
231
|
+
end
|
232
|
+
|
233
|
+
args[:error] = args[:msg]
|
234
|
+
args[:exit_after] = true
|
235
|
+
args[:code] ||= 1
|
236
|
+
|
237
|
+
self.write(args)
|
238
|
+
end
|
239
|
+
|
240
|
+
# Prints an error status
|
241
|
+
def status(status, args = nil)
|
242
|
+
args ||= {}
|
243
|
+
|
244
|
+
if status.is_a?(Hash) then
|
245
|
+
args.merge!(status)
|
246
|
+
else
|
247
|
+
args[:status] = status
|
248
|
+
end
|
249
|
+
|
250
|
+
status = status.is_a?(Hash) ? status[:status] : status
|
251
|
+
args[:end] = @@statuses[status] || @@statuses[:ok]
|
252
|
+
args[:dots] = false
|
253
|
+
args[:up] = true if args[:up] == nil
|
254
|
+
self.write(args)
|
255
|
+
end
|
256
|
+
|
257
|
+
# Read input from the user.
|
258
|
+
#
|
259
|
+
# Arguments:
|
260
|
+
# * <em>msg</em>: The prompt to show
|
261
|
+
# * <em>valids</em>: A list of regexp to validate the input
|
262
|
+
# * <em>case_sensitive</em>: Wheter the validation is case_sensitive
|
263
|
+
#
|
264
|
+
# Returns: The read input
|
265
|
+
def read(args)
|
266
|
+
# Adjust prompt
|
267
|
+
msg = args[:msg] + ((msg !~ /([:?](\s*))$/) ? ":" : "")
|
268
|
+
msg += " " if msg !~ / ^/
|
269
|
+
|
270
|
+
# Turn choices into regular expressions
|
271
|
+
regexps = (args[:valids] || []).force_array.collect do |valid|
|
272
|
+
if !valid.is_a?(Regexp) then
|
273
|
+
valid = Regexp.new((valid !~ /^\^/ ? "^" : "") + valid + (valid !~ /\$$/ ? "$" : ""), Regexp::EXTENDED + (args[:case_sensitive] ? Regexp::IGNORECASE : 0), "U")
|
274
|
+
else
|
275
|
+
valid
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
rv = nil
|
280
|
+
|
281
|
+
# Read input
|
282
|
+
while true do
|
283
|
+
# Show message
|
284
|
+
print(msg)
|
285
|
+
|
286
|
+
# Get reply
|
287
|
+
bufs = gets.chop()
|
288
|
+
|
289
|
+
# If we don't have any regexp
|
290
|
+
if regexps.length == 0 then
|
291
|
+
rv = bufs
|
292
|
+
break
|
293
|
+
end
|
294
|
+
|
295
|
+
# Validate inputs
|
296
|
+
regexps.each do |re|
|
297
|
+
if bufs =~ re then
|
298
|
+
rv = bufs
|
299
|
+
break
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
break if rv
|
304
|
+
self.write(warn: "Sorry, your reply was not understood. Please try again")
|
305
|
+
end
|
306
|
+
|
307
|
+
rv
|
308
|
+
end
|
309
|
+
|
310
|
+
# Create a new Console.
|
311
|
+
def initialize
|
312
|
+
@indent_level = 0
|
313
|
+
@show_commands = false
|
314
|
+
@show_outputs = false
|
315
|
+
@skip_commands = false
|
316
|
+
@tty_width = -1
|
317
|
+
@@indentator= " "
|
318
|
+
|
319
|
+
@@styles = {
|
320
|
+
# Default color
|
321
|
+
"default" => "\33[0m",
|
322
|
+
# Text style
|
323
|
+
"bold" => "\33[1m", "underline" => "\33[4m", "blink" => "\33[5m", "reverse" => "\33[7m", "concealed" => "\33[8m",
|
324
|
+
# Foreground colors
|
325
|
+
"black" => "\33[30m", "red" => "\33[31m", "green" => "\33[32m", "yellow" => "\33[33m", "blue" => "\33[34m", "magenta" => "\33[35m", "cyan" => "\33[36m", "white" => "\33[37m",
|
326
|
+
# Background colors
|
327
|
+
"bg_black" => "\33[40m", "bg_red" => "\33[41m", "bg_green" => "\33[42m", "bg_yellow" => "\33[43m", "bg_blue" => "\33[44m", "bg_magenta" => "\33[45m", "bg_cyan" => "\33[46m", "bg_white" => "\33[47m"
|
328
|
+
}
|
329
|
+
|
330
|
+
@@statuses = {
|
331
|
+
ok: '<text style="bold blue">[ <text style="bold green">OK</text> ]</text> ',
|
332
|
+
pass: '<text style="bold blue">[<text style="bold cyan">PASS</text>]</text> ',
|
333
|
+
fail: '<text style="bold blue">[<text style="bold red">FAIL</text>]</text> ',
|
334
|
+
warn: '<text style="bold blue">[<text style="bold yellow">WARN</text>]</text> ',
|
335
|
+
}
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
360
339
|
end
|