diff --git a/CHANGELOG.md b/CHANGELOG.md index d08bbbf..61d8657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.0.1] - Unreleased +### Added +- Added file downloads progress + ### Fixed - Bug fix where resources are not decoded correctly when installing packages [#31](https://git.n64.cc/nosial/ncc/-/issues/42) diff --git a/src/ncc/Classes/HttpClient.php b/src/ncc/Classes/HttpClient.php index 11b47e9..eca7d9b 100644 --- a/src/ncc/Classes/HttpClient.php +++ b/src/ncc/Classes/HttpClient.php @@ -54,6 +54,28 @@ namespace ncc\Classes; curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_HTTPHEADER, $request->Headers); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->Type); + curl_setopt($curl, CURLOPT_NOPROGRESS, false); + curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, function($curl, $downloadSize, $downloaded, $uploadSize, $uploaded) use ($request) + { + if($downloadSize > 0 && ($downloaded !== $downloadSize)) + { + if(Main::getLogLevel() !== null) + { + switch(Main::getLogLevel()) + { + case LogLevel::Verbose: + case LogLevel::Debug: + case LogLevel::Silent: + Console::outVerbose(sprintf(' <= %s of %s bytes downloaded', $downloaded, $downloadSize)); + break; + + default: + Console::inlineProgressBar($downloaded, $downloadSize + 1); + break; + } + } + } + }); switch($request->Type) { @@ -188,35 +210,6 @@ namespace ncc\Classes; fclose($fp); } - /** - * Displays the download progress in the console - * - * @param $downloadSize - * @param $downloaded - * @return void - */ - public static function displayProgress($downloadSize, $downloaded): void - { - if(Main::getLogLevel() !== null) - { - switch(Main::getLogLevel()) - { - case LogLevel::Verbose: - case LogLevel::Debug: - case LogLevel::Silent: - Console::outVerbose(sprintf(' <= %s of %s bytes downloaded', $downloaded, $downloadSize)); - break; - - default: - if ($downloadSize > 0) - Console::inlineProgressBar($downloaded, $downloadSize); - break; - } - } - - - } - /** * Takes the return headers of a cURL request and parses them into an array. *