21
21
use function getenv ;
22
22
use function is_resource ;
23
23
use function preg_replace ;
24
+ use function printf ;
25
+ use function round ;
26
+ use function str_repeat ;
24
27
use function stream_context_create ;
25
28
use function stream_context_set_params ;
26
29
use function strpos ;
@@ -112,8 +115,7 @@ public function __construct(string $url, string $saveAs = '', string $type = sel
112
115
{
113
116
$ this ->setUrl ($ url );
114
117
$ this ->setSaveAs ($ saveAs );
115
-
116
- $ this ->showType = $ type === self ::PROGRESS_BAR ? self ::PROGRESS_BAR : self ::PROGRESS_TEXT ;
118
+ $ this ->setShowType ($ type );
117
119
}
118
120
119
121
/**
@@ -271,32 +273,42 @@ public function progressShow(
271
273
272
274
/**
273
275
* @param $transferredBytes
274
- *
275
- * @return string
276
276
*/
277
- public function showProgressByType ($ transferredBytes ): string
277
+ public function showProgressByType ($ transferredBytes ): void
278
278
{
279
279
if ($ transferredBytes <= 0 ) {
280
- return '' ;
280
+ return ;
281
281
}
282
282
283
- $ tfKb = $ transferredBytes / 1024 ;
283
+ $ tfKbSize = $ transferredBytes / 1024 ;
284
+ if ($ this ->fileSize === null ) {
285
+ printf ("\r\rUnknown file size... %2d kb done.. " , $ tfKbSize );
286
+ return ;
287
+ }
284
288
285
- if ($ this ->showType === self ::PROGRESS_BAR ) {
286
- $ size = $ this ->fileSize ;
289
+ $ totalSize = $ this ->fileSize / 1024 ;
287
290
288
- if ($ size === null ) {
289
- printf ("\rUnknown file size... %2d kb done.. " , $ tfKb );
291
+ $ percent = $ transferredBytes / $ this ->fileSize ;
292
+ if ($ this ->showType === self ::PROGRESS_BAR ) {
293
+ $ barWidth = 60 ;
294
+ $ boxNumber = (int )round ($ percent * $ barWidth ); // ■ =
295
+ if ($ barWidth === $ boxNumber ) {
296
+ $ completed = 100 ;
297
+ $ tfKbSize = $ totalSize ;
290
298
} else {
291
- $ length = (int )ceil (($ transferredBytes / $ size ) * 100 ); // ■ =
292
- printf ("\r[%-100s] %d%% (%2d/%2d kb) " , str_repeat ('= ' , $ length ) . '> ' , $ length , $ tfKb , $ size / 1024 );
299
+ $ completed = (int )round ($ percent * 100 );
293
300
}
301
+
302
+ $ paddingBar = str_repeat ('= ' , $ boxNumber ) . '> ' ;
303
+ printf ("\r\r[%-60s] %d%% (%2d/%2d kb) " , $ paddingBar , $ completed , $ tfKbSize , $ totalSize );
294
304
} else {
295
- printf ("\r\rMade some progress, downloaded %2d kb so far " , $ tfKb );
305
+ if ((int )round ($ percent * 100 ) === 100 ) {
306
+ $ tfKbSize = $ totalSize ;
307
+ }
308
+
296
309
//$msg = "Made some progress, downloaded <info>$transferredBytes</info> so far";
310
+ printf ("\r\rMade some progress, downloaded %2d kb so far " , $ tfKbSize );
297
311
}
298
-
299
- return '' ;
300
312
}
301
313
302
314
/**
@@ -323,7 +335,7 @@ public function getShowType(): string
323
335
*/
324
336
public function setShowType (string $ showType ): void
325
337
{
326
- $ this ->showType = $ showType ;
338
+ $ this ->showType = $ showType === self :: PROGRESS_BAR ? self :: PROGRESS_BAR : self :: PROGRESS_TEXT ;
327
339
}
328
340
329
341
/**
@@ -355,7 +367,9 @@ public function getSaveAs(): string
355
367
*/
356
368
public function setSaveAs (string $ saveAs ): void
357
369
{
358
- $ this ->saveAs = trim ($ saveAs );
370
+ if ($ saveAs ) {
371
+ $ this ->saveAs = trim ($ saveAs );
372
+ }
359
373
}
360
374
361
375
/**
0 commit comments