-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogressBar.lua
More file actions
281 lines (204 loc) · 8.01 KB
/
Copy pathprogressBar.lua
File metadata and controls
281 lines (204 loc) · 8.01 KB
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
--require('mobdebug').start()
-- syntax:
-- before running a loop, initialize the progress bar with
-- progressBar.init(([<startAt>, ntrials], <nbars>, style); -- (the 'startAt' and <nbars> parameters are optional)
-- style can be '' (for inline progressbar) , or '=', to run on a separate line with a bar above
-- then, at some point during the loop (for i = startAt:ntrials, for each iteration,)
-- run:
-- progressBar.step(i);
-- or, alternatively, just:
-- progressBar.step
-- that's it!
-- to return to the next line, after the loop has finished, type
-- progressBar.done;
require 'math'
progressBar = {}
progressBar.init = function(stepsToDoDetails, nTotalBars, displayStyle_flag, autoRestart_flag)
local pb = progressBar
pb.allowBreakWithFile = false; -- at beginning and end of bar
pb.X = '*';
pb.laststr = '';
pb.timer = torch.Timer()
pb.saveProgressInFile = false;
pb.showTimeRemaining = true
-- A. PARSE INPUTS
-- (1) Determine output style:
-- if (arg.n >= 3)
-- switch varargin{1}
-- case 'init', displayStyle = SAME_LINE;
-- case 'init-', displayStyle = SEPARATE_LINE;
-- case 'init=', displayStyle = GUI;
-- end
pb.displayStyle = 'same-line'
-- (2) Determine where to start & where to end:
pb.nStepsDoneAtStart = 0
pb.timeTaken_sec = 0
if type(stepsToDoDetails) == 'number' then
pb.nStepsTotal = stepsToDoDetails
elseif (#stepsToDoDetails) == 1 then
pb.nStepsTotal = stepsToDoDetails[1]
elseif (#stepsToDoDetails) == 2 then
pb.nStepsDoneAtStart = stepsToDoDetails[1]
pb.nStepsTotal = stepsToDoDetails[2];
end
-- (3) Determine how many bars
if not nTotalBars then
if pb.displayStyle == 'same line' then
nTotalBars = torch.min(10, pb.nStepsTotal);
else --if displayStyle == 'new line' then
nTotalBars = torch.min(40, pb.nStepsTotal);
end
end
pb.nTotalBars = nTotalBars
pb.stepsPerBar = pb.nStepsTotal / pb.nTotalBars
pb.nStepsDone = pb.nStepsDoneAtStart
-- B. INITIALIZE PROGRESS BAR
-- 1. Basics
pb.autoRestart = (autoRestart_flag ~= nil) and (autoRestart_flag ~= false)
if type(autoRestart_flag) == 'boolean' then
pb.nStarts = 1
elseif type(autoRestart_flag) == 'number' then
pb.nStarts = autoRestart_flag
end
if (displayStyle_flag == '=') then
pb.displayStyle = 'new line'
elseif (displayStyle_flag == '-') then
pb.displayStyle = 'same line'
end
local nExtraTopBars = iff(pb.autoRestart, 6, 0)
if (pb.displayStyle == 'new line') then
io.write('\n' .. string.rep('-', nTotalBars+nExtraTopBars) .. '\n')
end
if pb.autoRestart then
io.write(string.format('(%3d )', pb.nStarts))
end
-- 2. Starting bars
pb.nBarsDisplayed = 0;
io.write('|')
io.flush()
if (pb.nStepsDoneAtStart > 0) then
pb.nBarsDisplayed = math.floor(pb.nStepsDoneAtStart/pb.stepsPerBar)
io.write( string.rep('=', pb.nBarsDisplayed) )
pb.laststr = '';
end
if pb.allowBreakWithFile then
end
end
progressBar.step = function(stepsDone_in)
local pb = progressBar
if not stepsDone_in then
pb.nStepsDone = pb.nStepsDone + 1
else
pb.nStepsDone = stepsDone_in
end
local barsCompleted = math.floor(pb.nStepsDone/pb.stepsPerBar)
if (barsCompleted > pb.nBarsDisplayed) then
pb.timeTaken_sec = pb.timer:time().real
local nColumns = os.getenv('COLUMNS')
if nColumns == nil then
nColumns = 90
--pb.showTimeRemaining = false
end
pb.timeRemaing_sec = pb.timeTaken_sec/(pb.nStepsDone - pb.nStepsDoneAtStart) * (pb.nStepsTotal - pb.nStepsDone)
pb.startedAtStr = iff(pb.nStepsDoneAtStart ==0, '', '[Started at ' .. tostring(nStepsDoneAtStart) .. ']: ')
local stepsStr = 'Completed ' .. pb.startedAtStr .. pb.nStepsDone .. '/' .. pb.nStepsTotal
local elapStr = 'Elapsed: ' .. sec2hms(pb.timeTaken_sec)
local remStr = '[Remaining: ' .. sec2hms(pb.timeRemaing_sec) .. ']'
local barsToDisplay = barsCompleted - pb.nBarsDisplayed
if barsToDisplay > 0 then
pb.nBarsDisplayed = pb.nBarsDisplayed + barsToDisplay;
if pb.showTimeRemaining then
local dispStr = elapStr .. '. ' .. remStr;
local space = string.rep(' ', pb.nTotalBars - pb.nBarsDisplayed ) .. '| ';
backspace(pb.laststr);
io.write(string.rep(pb.X, barsToDisplay));
local textToShow = space .. dispStr
local columnsLeft = nColumns - (pb.nBarsDisplayed + barsToDisplay) - 1
if columnsLeft < #textToShow then
textToShow = string.sub(textToShow, 1, columnsLeft)
end
pb.laststr = textToShow
io.write(pb.laststr);
else
io.write(string.rep(pb.X, barsToDisplay))
end
io.flush()
end
if pb.autoRestart and (pb.nStepsDone >= pb.nStepsTotal) then
progressBar.done()
pb.nStarts = pb.nStarts + 1
progressBar.init(pb.nStepsTotal, pb.nTotalBars, '-', pb.nStarts)
end
end
end
progressBar.done = function(removeFlag)
local pb = progressBar
if removeFlag then
backspace(pb.laststr)
backspace(pb.nBarsDisplayed + 1)
else
if pb.showTimeRemaining then
backspace(pb.laststr)
blnk = string.rep(' ', pb.laststr:len())
io.write(blnk)
backspace(blnk)
pb.timeTaken_sec = pb.timer:time().real
local completedStr = '| Completed in: ' .. sec2hms(pb.timeTaken_sec)
io.write(completedStr)
end
io.write('\n')
end
if pb.allowBreakWithFile then
if paths.filep(stop_file) then
os.rename(stop_file, stopping_file)
error('Stop.')
end
end
end
progressBar.printf = function(...)
local pb = progressBar
printf(...)
pb.laststr = ''
end
progressBar.test = function()
local ntrials = 80
local nstars = 40
local startAt = 0
io.write('\nTesting... ' .. ntrials .. ' trials, ' .. nstars .. ' stars; starting at ' .. startAt);
-- progressBar.init({startAt, ntrials}, nstars, '=')
progressBar.init(ntrials, nstars, '=', false)
for i = startAt, ntrials do
progressBar.step()
sys.sleep(0.01)
end
progressBar.done();
--[[
io.write('Testing removal of progressBar after completion...\n')
for i = 1,10 do
io.write('[testing]')
progressBar.init(ntrials, 10)
for i = startAt, ntrials do
progressBar.step()
sys.sleep(0.01)
end
progressBar.done(1);
end
--]]
--[[
for _,ntrials in pairs({20, 40, 60}) do
for _,nstars in pairs({10, 30, 50}) do
for _,startAt in pairs({0 ,5}) do
io.write('\nTesting... ' .. ntrials .. ' trials, ' .. nstars .. ' stars; starting at ' .. startAt);
progressBar.init({startAt, ntrials}, nstars, '=')
for i = startAt, ntrials do
progressBar.step(i)
os.execute("sleep 0.01")
end
progressBar.done();
os.execute("sleep 1")
end
end
end
--]]
end
--progressBar.test()