This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgressBar.php
342 lines (301 loc) · 8.61 KB
/
ProgressBar.php
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
<?php
/**
* PHP CLI Progress bar
*
* PHP 5
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2011, Andy Dawson
* @link http://ad7six.com
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* ProgressBar
*
* Static wrapper class for generating progress bars for cli tasks
*
*/
class ProgressBar
{
/**
* Merged with options passed in start function
*/
protected static $defaults = array(
'format' => "\r:message::padding:%.01f%% %2\$d/%3\$d ETC: %4\$s. Elapsed: %5\$s [%6\$s]",
'message' => 'Running',
'size' => 30,
'width' => null
);
/**
* Runtime options
*/
protected static $options = array();
/**
* How much have we done already
*/
protected static $done = 0;
/**
* The format string used for the rendered status bar - see $defaults
*/
protected static $format;
/**
* message to display prefixing the progress bar text
*/
protected static $message;
/**
* How many chars to use for the progress bar itself. Not to be confused with $width
*/
protected static $size = 30;
/**
* When did we start (timestamp)
*/
protected static $start;
/**
* The width in characters the whole rendered string must fit in. defaults to the width of the
* terminal window
*/
protected static $width;
/**
* What's the total number of times we're going to call set
*/
protected static $total;
/**
* Show a progress bar, actually not usually called explicitly. Called by next()
*
* @param int $done what fraction of $total to set as progress uses internal counter if not passed
*
* @static
* @return string, the formatted progress bar prefixed with a carriage return
*/
public static function display($done = null)
{
if ($done) {
self::$done = $done;
}
$now = time();
if (self::$total) {
$fractionComplete = (double) (self::$done / self::$total);
} else {
$fractionComplete = 0;
}
$bar = floor($fractionComplete * self::$size);
$barSize = min($bar, self::$size);
$barContents = str_repeat('=', $barSize);
if ($bar < self::$size) {
$barContents .= '>';
$barContents .= str_repeat(' ', self::$size - $barSize);
} elseif ($fractionComplete > 1) {
$barContents .= '!';
} else {
$barContents .= '=';
}
$percent = number_format($fractionComplete * 100, 0);
$elapsed = $now - self::$start;
if (self::$done) {
$rate = $elapsed / self::$done;
} else {
$rate = 0;
}
$left = self::$total - self::$done;
$etc = round($rate * $left, 2);
if (self::$done) {
$etcNowText = '< 1 sec';
} else {
$etcNowText = '???';
}
$timeRemaining = self::humanTime($etc, $etcNowText);
$timeElapsed = self::humanTime($elapsed);
$return = sprintf(
self::$format,
$percent,
self::$done,
self::$total,
$timeRemaining,
$timeElapsed,
$barContents
);
$width = strlen(preg_replace('@(?:\r|:\w+:)@', '', $return));
if (strlen(self::$message) > (self::$width - $width - 3)) {
$message = substr(self::$message, 0, (self::$width - $width - 4)) . '...';
$padding = '';
echo "\n" . strlen($return);
} else {
$message = self::$message;
$width += strlen($message);
$padding = str_repeat(' ', (self::$width - $width));
}
$return = str_replace(':message:', $message, $return);
$return = str_replace(':padding:', $padding, $return);
return $return;
}
/**
* reset internal state, and send a new line so that the progress bar text is "finished"
*
* @static
* @return string, a new line
*/
public static function finish()
{
self::reset();
return "\n";
}
/**
* Increment the internal counter, and returns the result of display
*
* @param int $inc Amount to increment the internal counter
* @param string $message If passed, overrides the existing message
*
* @static
* @return string - the progress bar
*/
public static function next($inc = 1, $message = '')
{
self::$done += $inc;
if ($message) {
self::$message = $message;
}
return self::display();
}
/**
* Called by start and finish
*
* @param array $options array
*
* @static
* @return void
*/
public static function reset($options = array())
{
$options = array_merge(self::$defaults, $options);
if (empty($options['done'])) {
$options['done'] = 0;
}
if (empty($options['start'])) {
$options['start'] = time();
}
if (empty($options['total'])) {
$options['total'] = 0;
}
self::$done = $options['done'];
self::$format = $options['format'];
self::$message = $options['message'];
self::$size = $options['size'];
self::$start = $options['start'];
self::$total = $options['total'];
self::setWidth($options['width']);
}
/**
* change the message to be used the next time the display method is called
*
* @param string $message the string to display
*
* @static
* @return void
*/
public static function setMessage($message = '')
{
self::$message = $message;
}
/**
* change the total on a running progress bar
*
* @param int $total the new number of times we're expecting to run for
*
* @static
* @return void
*/
public static function setTotal($total = '')
{
self::$total = $total;
}
/**
* Initialize a progress bar
*
* @param mixed $total number of times we're going to call set
* @param int $message message to prefix the bar with
* @param int $options overrides for default options
*
* @static
* @return string - the progress bar string with 0 progress
*/
public static function start($total = null, $message = '', $options = array())
{
if ($message) {
$options['message'] = $message;
}
$options['total'] = $total;
$options['start'] = time();
self::reset($options);
return self::display();
}
/**
* Convert a number of seconds into something human readable like "2 days, 4 hrs"
*
* @param int $seconds how far in the future/past to display
* @param string $nowText if there are no seconds, what text to display
*
* @static
* @return string representation of the time
*/
protected static function humanTime($seconds, $nowText = '< 1 sec')
{
$prefix = '';
if ($seconds < 0) {
$prefix = '- ';
$seconds = -$seconds;
}
$days = $hours = $minutes = 0;
if ($seconds >= 86400) {
$days = (int) ($seconds / 86400);
$seconds = $seconds - $days * 86400;
}
if ($seconds >= 3600) {
$hours = (int) ($seconds / 3600);
$seconds = $seconds - $hours * 3600;
}
if ($seconds >= 60) {
$minutes = (int) ($seconds / 60);
$seconds = $seconds - $minutes * 60;
}
$seconds = (int) $seconds;
$return = array();
if ($days) {
$return[] = "$days days";
}
if ($hours) {
$return[] = "$hours hrs";
}
if ($minutes) {
$return[] = "$minutes mins";
}
if ($seconds) {
$return[] = "$seconds secs";
}
if (!$return) {
return $nowText;
}
return $prefix . implode(array_slice($return, 0, 2), ', ');
}
/**
* Set the width the rendered text must fit in
*
* @param int $width passed in options
*
* @static
* @return void
*/
protected static function setWidth($width = null)
{
if ($width === null) {
if (DIRECTORY_SEPARATOR === '/') {
$width = `tput cols`;
}
if ($width < 80) {
$width = 80;
}
}
self::$width = $width;
}
}