Skip to content

Commit f94783f

Browse files
committed
up: update some logic for download progress handle
1 parent a4e38d8 commit f94783f

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/Traits/WriteMessageTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static function print($message, $quit = false, array $opts = []): int
125125
* Write a message to standard output stream.
126126
*
127127
* @param string|array $messages Output message
128-
* @param boolean $nl True Will add line breaks, False Raw output.
128+
* @param bool|mixed $nl True Will add line breaks, False Raw output.
129129
* @param int|boolean $quit If is Int, setting it is exit code.
130130
* 'True' translate as code 0 and exit
131131
* 'False' will not exit.
@@ -138,7 +138,7 @@ public static function print($message, $quit = false, array $opts = []): int
138138
*
139139
* @return int
140140
*/
141-
public static function write($messages, bool $nl = true, $quit = false, array $opts = []): int
141+
public static function write($messages, $nl = true, $quit = false, array $opts = []): int
142142
{
143143
if (is_array($messages)) {
144144
$messages = implode($nl ? PHP_EOL : '', $messages);

src/Util/Download.php

+32-18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use function getenv;
2222
use function is_resource;
2323
use function preg_replace;
24+
use function printf;
25+
use function round;
26+
use function str_repeat;
2427
use function stream_context_create;
2528
use function stream_context_set_params;
2629
use 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

Comments
 (0)