-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextprogressbar.m
301 lines (263 loc) · 9.7 KB
/
textprogressbar.m
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
function upd = textprogressbar(n, varargin)
% UPD = TEXTPROGRESSBAR(N) initializes a text progress bar for monitoring a
% task comprising N steps (e.g., the N rounds of an iteration) in the
% command line. It returns a function handle UPD that is used to update and
% render the progress bar. UPD takes a single argument i <= N which
% corresponds to the number of tasks completed and renders the progress bar
% accordingly.
%
% TEXTPROGRESSBAR(...,'barlength',L) determines the length L of the
% progress bar in number of characters (see 'barsymbol' option). L must be
% a positive integer.
% (Default value is 20 characters.)
%
% TEXTPROGRESSBAR(...,'updatestep',S) determines the minimum number of update
% steps S between consecutive bar re-renderings. The option controls how
% frequently the bar is rendered and in turn controls the computational
% overhead that the bar rendering incurs to the monitored task. It is
% especially useful when bar is used for loops with large number of rounds
% and short execution time per round.
% (Default value is S=10 steps.)
%
% TEXTPROGRESSBAR(...,'startmsg',str) determines the message string to be
% displayed before the progress bar.
% (Default is str='Completed '.)
%
% TEXTPROGRESSBAR(...,'endmsg',str) determines the message string to be
% displayed after progress bar when the task is completed.
% (Default is str=' Done.')
%
% TEXTPROGRESSBAR(...,'showremtime',b) logical parameter that controls
% whether an estimate of the remaining time is displayed.
% (Default is b=true.)
%
% TEXTPROGRESSBAR(...,'showbar',b) logical parameter that controls whether
% the progress bar is displayed. (Default is b=true.)
%
% TEXTPROGRESSBAR(...,'showpercentage',b) logical parameter that controls
% whether to display the percentage of completed items.
% (Default is true.)
%
% TEXTPROGRESSBAR(...,'showactualnum',b) logical parameter that controls
% whether to display the actual number of completed items.
% (Default is false.)
%
% TEXTPROGRESSBAR(...,'showfinaltime',b) logical parameter that controls
% whether to display the total run-time when completed.
% (Default is true.)
%
% TEXTPROGRESSBAR(...,'barsymbol',c) determines the symbol (character) to
% be used for the progress bar. c must be a single character.
% (Default is c='='.)
%
% TEXTPROGRESSBAR(...,'emptybarsymbol',c) determines the symbol (character)
% that is used to fill the un-completed part of the progress bar. c must be
% a single character.
% (Default is c=' '.)
%
% Example:
%
% n = 150;
% upd = textprogressbar(n);
% for i = 1:n
% pause(0.05);
% upd(i);
% end
%
% Default Parameter values:
defaultbarCharLen = 20;
defaultUpdStep = 10;
defaultstartMsg = 'Completed ';
defaultendMsg = ' Done.';
defaultShowremTime = true;
defaultShowBar = true;
defaultshowPercentage = true;
defaultshowActualNum = false;
defaultshowFinalTime = true;
defaultbarCharSymbol = '=';
defaultEmptybarCharSymbol = ' ';
% Auxiliary functions for checking parameter values:
ischarsymbol = @(c) (ischar(c) && length(c) == 1);
ispositiveint = @(x) (isnumeric(x) && mod(x, 1) == 0 && x > 0);
% Register input parameters:
p = inputParser;
addRequired(p,'n', ispositiveint);
addParameter(p, 'barlength', defaultbarCharLen, ispositiveint)
addParameter(p, 'updatestep', defaultUpdStep, ispositiveint)
addParameter(p, 'startmsg', defaultstartMsg, @ischar)
addParameter(p, 'endmsg', defaultendMsg, @ischar)
addParameter(p, 'showremtime', defaultShowremTime, @islogical)
addParameter(p, 'showbar', defaultShowBar, @islogical)
addParameter(p, 'showpercentage', defaultshowPercentage, @islogical)
addParameter(p, 'showactualnum', defaultshowActualNum, @islogical)
addParameter(p, 'showfinaltime', defaultshowFinalTime, @islogical)
addParameter(p, 'barsymbol', defaultbarCharSymbol, ischarsymbol)
addParameter(p, 'emptybarsymbol', defaultEmptybarCharSymbol, ischarsymbol)
% Parse input arguments:
parse(p, n, varargin{:});
n = p.Results.n;
barCharLen = p.Results.barlength;
updStep = p.Results.updatestep;
startMsg = p.Results.startmsg;
endMsg = p.Results.endmsg;
showremTime = p.Results.showremtime;
showBar = p.Results.showbar;
showPercentage = p.Results.showpercentage;
showActualNum = p.Results.showactualnum;
showFinalTime = p.Results.showfinaltime;
barCharSymbol = p.Results.barsymbol;
emptybarCharSymbol = p.Results.emptybarsymbol;
% Initialize progress bar:
bar = ['[', repmat(emptybarCharSymbol, 1, barCharLen), ']'];
nextRenderPoint = 0;
startTime = tic;
% Initalize block for actual number of completed items:
ind = 1;
% Start message block:
startMsgLen = length(startMsg);
startMsgStart = ind;
startMsgEnd = startMsgStart + startMsgLen - 1;
ind = ind + startMsgLen;
% Bar block:
barLen = length(bar);
barStart = 0;
barEnd = 0;
if showBar
barStart = ind;
barEnd = barStart + barLen - 1;
ind = ind + barLen;
end
% Actual Num block:
actualNumDigitLen = numel(num2str(n));
actualNumFormat = sprintf(' %%%dd/%d', actualNumDigitLen, n);
actualNumStr = sprintf(actualNumFormat, 0);
actualNumLen = length(actualNumStr);
actualNumStart = 0;
actualNumEnd = 0;
if showActualNum
actualNumStart = ind;
actualNumEnd = actualNumStart + actualNumLen-1;
ind = ind + actualNumLen;
end
% Percentage block:
percentageFormat = sprintf(' %%3d%%%%');
percentageStr = sprintf(percentageFormat, 0);
percentageLen = length(percentageStr);
percentageStart = 0;
percentageEnd = 0;
if showPercentage
percentageStart = ind;
percentageEnd = percentageStart + percentageLen-1;
ind = ind + percentageLen;
end
% Remaining Time block:
remTimeStr = time2str(Inf);
remTimeLen = length(remTimeStr);
remTimeStart = 0;
remTimeEnd = 0;
if showremTime
remTimeStart = ind;
remTimeEnd = remTimeStart + remTimeLen - 1;
ind = ind + remTimeLen;
end
% End msg block:
endMsgLen = length(endMsg);
if showBar
endMsgStart = barEnd + 1; % Place end message right after bar;
else
endMsgStart = startMsgEnd + 1;
end
endMsgEnd = endMsgStart + endMsgLen - 1;
ind = max([ind, endMsgEnd]);
% Determine size of buffer:
arrayLen = ind - 1;
array = repmat(' ', 1, arrayLen);
% Initial render:
array(startMsgStart:startMsgEnd) = sprintf('%s', startMsg);
delAll = repmat('\b', 1, arrayLen);
% Function to update the status of the progress bar:
function update(i)
if i < nextRenderPoint
return;
end
if i > 0
fprintf(delAll);
end
%pause(1)
nextRenderPoint = min([nextRenderPoint + updStep, n]);
if showremTime
% Delete remaining time block:
array(remTimeStart:remTimeEnd) = ' ';
end
if showPercentage
% Delete percentage block:
array(percentageStart:percentageEnd) = ' ';
end
if showActualNum
% Delete actual num block:
array(actualNumStart:actualNumEnd) = ' ';
end
if showBar
% Update progress bar (only if needed):
barsToPrint = floor( i / n * barCharLen );
bar(2:1+barsToPrint) = barCharSymbol;
array(barStart:barEnd) = bar;
end
% Check if done:
if i >= n
array(endMsgStart:endMsgEnd) = endMsg;
array(endMsgEnd+1:end) = ' ';
if showFinalTime
finalTimeStr = ...
sprintf(' [%d seconds]', round(toc(startTime)));
finalTimeLen = length(finalTimeStr);
if endMsgEnd + finalTimeLen < arrayLen
array(endMsgEnd+1:endMsgEnd+finalTimeLen) = ...
finalTimeStr;
else
array = [array(1:endMsgEnd), finalTimeStr];
end
end
fprintf('%s', array);
fprintf('\n');
return;
end
if showActualNum
% Delete actual num block:
actualNumStr = sprintf(actualNumFormat, i);
array(actualNumStart:actualNumEnd) = actualNumStr;
end
if showPercentage
% Render percentage block:
percentage = floor(i / n * 100);
percentageStr = sprintf(percentageFormat, percentage);
array(percentageStart:percentageEnd) = percentageStr;
end
% Print remaining time block:
if showremTime
t = toc(startTime);
remTime = t/ i * (n-i);
remTimeStr = time2str(remTime);
array(remTimeStart:remTimeEnd) = remTimeStr;
end
fprintf('%s', array);
end
% Do the first render:
update(0);
upd = @update;
end
% Auxiliary functions
function timestr = time2str(t)
if t == Inf
timestr = sprintf(' --:--:--');
else
[hh, mm, tt] = sec2hhmmss(t);
timestr = sprintf(' %02d:%02d:%02d', hh, mm, tt);
end
end
function [hh, mm, ss] = sec2hhmmss(t)
hh = floor(t / 3600);
t = t - hh * 3600;
mm = floor(t / 60);
ss = round(t - mm * 60);
end