From: Michael Wallner Date: Wed, 8 Apr 2015 09:21:50 +0000 (+0200) Subject: fix stream progress notification with resetting bytes_max X-Git-Tag: v3.0.1~1 X-Git-Url: https://git.m6w6.name/?p=pharext%2Fpharext;a=commitdiff_plain;h=66d7a880cb37f809df6806e5dc62d191dc257d08 fix stream progress notification with resetting bytes_max --- diff --git a/bin/pharext b/bin/pharext index 9bb285c..3de5018 100755 Binary files a/bin/pharext and b/bin/pharext differ diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index 81bac81..cd1d436 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -144,13 +144,20 @@ class Packager implements Command if ($this->args->git) { $task = new Task\GitClone($source); } else { - $task = new Task\StreamFetch($source, function($bytes_pct) { - $this->info(" %3d%% [%s>%s] \r%s", - floor($bytes_pct*100), - str_repeat("=", round(50*$bytes_pct)), - str_repeat(" ", round(50*(1-$bytes_pct))), - $bytes_pct == 1 ? "\n":"" - ); + /* print newline only once */ + $done = false; + $task = new Task\StreamFetch($source, function($bytes_pct) use(&$done) { + if (!$done) { + $this->info(" %3d%% [%s>%s] \r", + floor($bytes_pct*100), + str_repeat("=", round(50*$bytes_pct)), + str_repeat(" ", round(50*(1-$bytes_pct))) + ); + if ($bytes_pct == 1) { + $done = true; + printf("\n"); + } + } }); } $local = $task->run($this->verbosity()); diff --git a/src/pharext/Task/StreamFetch.php b/src/pharext/Task/StreamFetch.php index 3b1fc29..029754e 100644 --- a/src/pharext/Task/StreamFetch.php +++ b/src/pharext/Task/StreamFetch.php @@ -33,16 +33,21 @@ class StreamFetch implements Task private function createStreamContext() { $progress = $this->progress; - return stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) use($progress) { + /* avoid bytes_max bug of older PHP versions */ + $maxbytes = 0; + return stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) use($progress, &$maxbytes) { + if ($bytes_max > $maxbytes) { + $maxbytes = $bytes_max; + } switch ($notification) { case STREAM_NOTIFY_CONNECT: $progress(0); break; case STREAM_NOTIFY_PROGRESS: - $progress($bytes_max ? $bytes_cur/$bytes_max : .5); + $progress($maxbytes > 0 ? $bytes_cur/$maxbytes : .5); break; case STREAM_NOTIFY_COMPLETED: - /* this is not generated, why? */ + /* this is sometimes not generated, why? */ $progress(1); break; }