Skip to content

Commit 6e89d60

Browse files
committed
feat: Show download progress on updater.phar
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
1 parent 7a184f7 commit 6e89d60

9 files changed

Lines changed: 97 additions & 70 deletions

File tree

index.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LogException extends \Exception {
2727
}
2828

2929

30+
use Closure;
3031
use CurlHandle;
3132

3233
class Updater {
@@ -38,6 +39,7 @@ class Updater {
3839
private ?string $requestID = null;
3940
private bool $disabled = false;
4041
private int $previousProgress = 0;
42+
private ?Closure $downloadProgress = null;
4143

4244
/**
4345
* Updater constructor
@@ -555,8 +557,9 @@ private function getUpdateServerResponse(): array {
555557
*
556558
* @throws \Exception
557559
*/
558-
public function downloadUpdate(?string $url = null): void {
560+
public function downloadUpdate(?string $url = null, ?Closure $downloadProgress = null): void {
559561
$this->silentLog('[info] downloadUpdate()');
562+
$this->downloadProgress = $downloadProgress;
560563

561564
if ($url) {
562565
// If a URL is provided, use it directly
@@ -716,6 +719,10 @@ private function downloadProgressCallback(\CurlHandle $resource, int $download_s
716719
// log every 2% increment for the first 10% then only log every 10% increment after that
717720
if ($progress % 10 === 0 || ($progress < 10 && $progress % 2 === 0)) {
718721
$this->silentLog("[info] download progress: $progress% (" . $this->formatBytes($downloaded) . ' of ' . $this->formatBytes($download_size) . ')');
722+
723+
if ($this->downloadProgress) {
724+
($this->downloadProgress)($progress, $this->formatBytes($downloaded), $this->formatBytes($download_size));
725+
}
719726
}
720727
}
721728
}

lib/UpdateCommand.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
229229

230230
$output->write('[ ] ' . $this->checkTexts[$i] . ' ...');
231231

232-
$result = $this->executeStep($i);
232+
$result = $this->executeStep($i, $output);
233233

234234
// Move the cursor to the beginning of the line
235235
$output->write("\x0D");
@@ -380,7 +380,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
380380
/**
381381
* @return array{proceed:bool,response:string|list<string>} with options 'proceed' which is a boolean and defines if the step succeeded and an optional 'response' string or array
382382
*/
383-
protected function executeStep(int $step): array {
383+
protected function executeStep(int $step, OutputInterface $output): array {
384384
if ($this->updater === null) {
385385
return ['proceed' => false, 'response' => 'Initialization problem'];
386386
}
@@ -404,7 +404,14 @@ protected function executeStep(int $step): array {
404404
}
405405
break;
406406
case 4:
407-
$this->updater->downloadUpdate($this->urlOverride);
407+
// Ensure that we have the same number of characters, that we want to override in the progress method
408+
$output->write(str_pad(' 0%', 5, ' ', STR_PAD_LEFT));
409+
410+
$this->updater->downloadUpdate($this->urlOverride, function (int $progress, string $downloaded, string $download_size) use ($output) {
411+
// Move cursor 5 to the left and write the new progress
412+
$output->write("\x1B[5D");
413+
$output->write(str_pad(' ' . $progress . '%', 5, ' ', STR_PAD_LEFT));
414+
});
408415
break;
409416
case 5:
410417
$this->updater->verifyIntegrity($this->urlOverride);

lib/Updater.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace NC\Updater;
1111

12+
use Closure;
1213
use CurlHandle;
1314

1415
class Updater {
@@ -20,6 +21,7 @@ class Updater {
2021
private ?string $requestID = null;
2122
private bool $disabled = false;
2223
private int $previousProgress = 0;
24+
private ?Closure $downloadProgress = null;
2325

2426
/**
2527
* Updater constructor
@@ -537,8 +539,9 @@ private function getUpdateServerResponse(): array {
537539
*
538540
* @throws \Exception
539541
*/
540-
public function downloadUpdate(?string $url = null): void {
542+
public function downloadUpdate(?string $url = null, ?Closure $downloadProgress = null): void {
541543
$this->silentLog('[info] downloadUpdate()');
544+
$this->downloadProgress = $downloadProgress;
542545

543546
if ($url) {
544547
// If a URL is provided, use it directly
@@ -698,6 +701,10 @@ private function downloadProgressCallback(\CurlHandle $resource, int $download_s
698701
// log every 2% increment for the first 10% then only log every 10% increment after that
699702
if ($progress % 10 === 0 || ($progress < 10 && $progress % 2 === 0)) {
700703
$this->silentLog("[info] download progress: $progress% (" . $this->formatBytes($downloaded) . ' of ' . $this->formatBytes($download_size) . ')');
704+
705+
if ($this->downloadProgress) {
706+
($this->downloadProgress)($progress, $this->formatBytes($downloaded), $this->formatBytes($download_size));
707+
}
701708
}
702709
}
703710
}

updater.phar

874 Bytes
Binary file not shown.

vendor/autoload.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
echo $err;
1515
}
1616
}
17-
throw new RuntimeException($err);
17+
trigger_error(
18+
$err,
19+
E_USER_ERROR
20+
);
1821
}
1922

2023
require_once __DIR__ . '/composer/autoload_real.php';

0 commit comments

Comments
 (0)