2121use function getenv ;
2222use function is_resource ;
2323use function preg_replace ;
24+ use function printf ;
25+ use function round ;
26+ use function str_repeat ;
2427use function stream_context_create ;
2528use function stream_context_set_params ;
2629use function strpos ;
@@ -112,8 +115,7 @@ public function __construct(string $url, string $saveAs = '', string $type = sel
112115 {
113116 $ this ->setUrl ($ url );
114117 $ this ->setSaveAs ($ saveAs );
115-
116- $ this ->showType = $ type === self ::PROGRESS_BAR ? self ::PROGRESS_BAR : self ::PROGRESS_TEXT ;
118+ $ this ->setShowType ($ type );
117119 }
118120
119121 /**
@@ -271,32 +273,42 @@ public function progressShow(
271273
272274 /**
273275 * @param $transferredBytes
274- *
275- * @return string
276276 */
277- public function showProgressByType ($ transferredBytes ): string
277+ public function showProgressByType ($ transferredBytes ): void
278278 {
279279 if ($ transferredBytes <= 0 ) {
280- return '' ;
280+ return ;
281281 }
282282
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+ }
284288
285- if ($ this ->showType === self ::PROGRESS_BAR ) {
286- $ size = $ this ->fileSize ;
289+ $ totalSize = $ this ->fileSize / 1024 ;
287290
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 ;
290298 } 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 );
293300 }
301+
302+ $ paddingBar = str_repeat ('= ' , $ boxNumber ) . '> ' ;
303+ printf ("\r\r[%-60s] %d%% (%2d/%2d kb) " , $ paddingBar , $ completed , $ tfKbSize , $ totalSize );
294304 } 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+
296309 //$msg = "Made some progress, downloaded <info>$transferredBytes</info> so far";
310+ printf ("\r\rMade some progress, downloaded %2d kb so far " , $ tfKbSize );
297311 }
298-
299- return '' ;
300312 }
301313
302314 /**
@@ -323,7 +335,7 @@ public function getShowType(): string
323335 */
324336 public function setShowType (string $ showType ): void
325337 {
326- $ this ->showType = $ showType ;
338+ $ this ->showType = $ showType === self :: PROGRESS_BAR ? self :: PROGRESS_BAR : self :: PROGRESS_TEXT ;
327339 }
328340
329341 /**
@@ -355,7 +367,9 @@ public function getSaveAs(): string
355367 */
356368 public function setSaveAs (string $ saveAs ): void
357369 {
358- $ this ->saveAs = trim ($ saveAs );
370+ if ($ saveAs ) {
371+ $ this ->saveAs = trim ($ saveAs );
372+ }
359373 }
360374
361375 /**
0 commit comments