-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.rb
executable file
·356 lines (277 loc) · 6.27 KB
/
work.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
#!/usr/bin/ruby
class ProgressBar
attr_accessor :title, :title_width, :bar_mark
def title=(t)
@title = t
@title_width = t.length
@format = "%-#{@title_width}s %3d%% %s %s"
end
def title_width=(tw)
@title_width = tw
@format = "%-#{@title_width}s %3d%% %s %s"
end
end
module Work # Beginning of Work module
require "P4"
require "singleton"
require "delegate"
require "rubygems"
require "parseconfig"
require "colorize"
require "util"
require "progressbar"
class Config
include Singleton
CFG_FILE = "/etc/work.rc"
CFG_SEP = '='
@@config = nil
def initialize
reload
end
def var(name, group = nil)
if (group.nil? || @@config.params[group].nil?)
@@config.params[name]
else
@@config.params[group][name]
end
end
def [](var)
@@config.params[var]
end
def reload
@@config = ParseConfig.new(CFG_FILE, CFG_SEP)
end
end
class P4_Work < DelegateClass(P4)
include Singleton
attr_accessor :client
def initialize
begin
@p4obj = P4.new
# to ignore "File(s) up-to-date"
@p4obj.exception_level = P4::RAISE_ERRORS
@p4obj.connect
@p4obj.run_login
super(@p4obj)
@client = @p4obj.run_client("-o").shift
rescue P4Exception
@p4obj.errors.each { |e| puts(e) }
end
end
def revert_changelist(changelist)
spec = run("describe", "-s", changelist)
revs = spec[0]["rev"];
files = spec[0]["depotFile"];
revs.each_index { |x|
# run("sync", files[x] + "#" + revs[x])
print files[x] + " # " + revs[x] + "\n"
}
end
end
class Term_Meta
attr_accessor :out
def self.metaclass
class << self
self
end
end
def self.style(name, params)
raise "params must be a hash" unless params.class == Hash
@out = STDOUT
func =<<-EOT1
def #{name}(str)
@out.print "\r"
#{params[:before]}.times { @out.print "\n" }
EOT1
if params.has_key?(:apply)
func << <<-EOT
if str.respond_to?("#{params[:apply]}")
s = str.send("#{params[:apply]}")
else
s = str.dup
end
EOT
else
func << <<-EOT
s = str.dup
EOT
end
func << <<-EOT
@out.print s.colorize("#{params[:fg]}".to_sym) + "\n"
#{params[:after]}.times { @out.print "\n" }
end
EOT
instance_eval func
end
instance_eval <<-EOT
def out= (o)
@out = o
end
def out
@out
end
EOT
end
class Term < Term_Meta
include Utils
style "h1", :before => 2, :after => 1, :fg => :light_yellow, :bg => :default, :apply => :upcase
style "h2", :before => 1, :after => 0, :fg => :light_green, :bg => :default, :apply => :capitalize
TEXT_COLOR = :light_black
INFO_BULLET_COLOR = :light_green
INFO_TEXT_COLOR = :light_black
WARN_BULLET_COLOR = :light_yellow
WARN_TEXT_COLOR = :yellow
ERROR_BULLET_COLOR = :red
ERROR_TEXT_COLOR = :light_red
ACTION_BULLET_COLOR = :light_green
ACTION_TEXT_COLOR = :light_black
OK_COLOR = :light_green
FAILURE_COLOR = :light_red
BRACKET_COLOR = :light_black
BULLET = "* "
TAB_SIZE = 2
TAB = " "
DOT = "."
OK = "ok"
FAILURE = "failed"
LBRACKET = "["
RBRACKET = "]"
LPAD = 0
RPAD = 10
SPINNER = ['|', '/', '-', '\\', '|']
SPINNER_SLEEP = 0.1
@out = STDOUT
@dent_val = 1
@last_str = ""
@line = ""
@line_len = 0
def self.reset_line(cols = nil)
cols, = Utils.term_width_height if cols.nil?
@out.print "\r" + " " * cols + "\r"
@line = ""
@line_len = 0
end
def self.indent(x = 1)
@dent_val += x
end
def self.setdent(x = 0)
@dent_val = x
end
def self.indent_str
' ' * LPAD + TAB * TAB_SIZE * @dent_val
end
def self.prefix
indent_str + BULLET
end
def self.print(msg, color = nil)
str = msg
@line_len += msg.length
str = str.colorize(color) if not color.nil?
@line << str
@out.print str
@out.flush
end
def self.text(msg, bullet = nil, eol = nil)
e = eol
e = "\n" if eol.nil?
if bullet.nil?
format(msg, TEXT_COLOR, indent_str, nil, e)
else
format(msg, TEXT_COLOR, indent_str + BULLET, INFO_BULLET_COLOR, e)
end
end
def self.eol
print "\n"
@line_len = 0
@line = ""
end
def self.check_eol
eol if @line_len > 0
end
def self.info(msg)
check_eol
display(msg, INFO_TEXT_COLOR, prefix, INFO_BULLET_COLOR)
end
def self.warn(msg)
check_eol
display(msg, WARN_TEXT_COLOR, prefix, WARN_BULLET_COLOR)
end
def self.error(msg)
check_eol
display(msg, ERROR_TEXT_COLOR, prefix, ERROR_BULLET_COLOR)
end
def self.action(msg)
check_eol
print prefix, ACTION_BULLET_COLOR
print msg, ACTION_TEXT_COLOR
end
def self.ok(msg = nil)
cols, = Utils.term_width_height
if not msg.nil?
action(msg)
end
ok_str_len = (LBRACKET + OK + RBRACKET).length
ok_str = LBRACKET.colorize(BRACKET_COLOR) + OK.colorize(OK_COLOR) + \
RBRACKET.colorize(BRACKET_COLOR)
print ' ' + DOT * (cols - @line_len - ok_str_len - 2 - RPAD) + \
' ' + ok_str
eol
end
def self.failure(msg = nil)
cols, = Utils.term_width_height
if not msg.nil?
reset_line(cols)
action(msg)
end
failure_str_len = (LBRACKET + FAILURE + RBRACKET).length
failure_str = LBRACKET.colorize(BRACKET_COLOR) + \
FAILURE.colorize(FAILURE_COLOR) + \
RBRACKET.colorize(BRACKET_COLOR)
print ' ' + DOT * (cols - @line_len - failure_str_len - 2 - RPAD) \
+ ' ' + failure_str
eol
end
def self.spinner_start
return if @spinner_running
return if not SPINNER.instance_of? Array
@spinner_running = true
@spinner_thread = Thread.new do
until not @spinner_running
prev = nil
SPINNER.each {|s|
@out.print("\b" * prev.length) if not prev.nil?
@out.print(" " * prev.length) if not prev.nil?
@out.print("\b" * prev.length) if not prev.nil?
@out.print s
@out.flush
prev = s
sleep SPINNER_SLEEP
}
@out.print("\b" * SPINNER[SPINNER.length - 1].length)
@out.print(" " * SPINNER[SPINNER.length - 1].length)
@out.print("\b" * SPINNER[SPINNER.length - 1].length)
end
end
end
def self.spinner_stop
@spinner_running = false;
@spinner_thread.join
end
private
def self.display(msg, msg_color = nil, sep = nil, sep_color = nil)
cols, = Utils.term_width_height
cols -= 1
cols -= sep.length if not sep.nil?
msg_color = TEXT_COLOR if msg_color.nil?
arr = Utils.wrap_arr(msg, cols - 1)
arr.each { |line|
print sep, sep_color
print line, msg_color
eol
}
end
end
end # End of Work module
TERM = Work::Term
CONFIG = Work::Config.instance
PERF = Work::P4_Work.instance