Too tired to write descriptive message
This commit is contained in:
22
LICENSE
Normal file
22
LICENSE
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Siteworx Professionals LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
92
README.md
Normal file
92
README.md
Normal file
@@ -0,0 +1,92 @@
|
||||
# cli-progress-bar [](https://travis-ci.org/siteworxpro/cli-progress-bar)
|
||||
Progress bar for cli apps
|
||||
|
||||
[Forked From dariuszp/cli-progress-bar](https://github.com/dariuszp/cli-progress-bar)
|
||||
|
||||

|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
composer require siteworx/cli-progress-bar
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```php
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
$bar = new CliProgressBar(10, 5);
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
```
|
||||
|
||||
Code above will show half full progress bar:
|
||||
|
||||
```
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░ 50.0% (5/10)
|
||||
```
|
||||
|
||||
Windows can't handle some UTF characters so there is an alternate method to display progress bar:
|
||||
|
||||
```php
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
$bar = new CliProgressBar();
|
||||
$bar->displayAlternateProgressBar(); // this only switch style
|
||||
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
```
|
||||
|
||||
Output will be:
|
||||
|
||||
```
|
||||
XXXX____________________________________ 10.0% (10/100)
|
||||
```
|
||||
|
||||
Add text to the progress bar using the following methods
|
||||
```php
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
$bar = new CliProgressBar(50, 0, "My Custom Text");
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
```
|
||||
or
|
||||
```php
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setDetails("My Custom Text");
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
```
|
||||
|
||||
Estimated time to completion is available. At least 2 iterations are required to calculate an ET.
|
||||
The more iterations the better the estimate calculation will be.
|
||||
|
||||
```php
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
$bar = new CliProgressBar();
|
||||
$bar->displayTimeRemaining()->display();
|
||||
$bar->end();
|
||||
```
|
||||
|
||||
will output
|
||||
```
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░ 50.0% (5/10) 02:14
|
||||
```
|
||||
|
||||
|
||||
Also update asynchronously with setDetails()
|
||||
|
||||
More features like:
|
||||
- changing progress bar length (basicWithShortBar.php)
|
||||
- changing bar color (colors.php)
|
||||
- animation example (basic.php)
|
||||
- etc...
|
||||
|
||||
in [example](examples/) directory.
|
||||
|
||||
----
|
||||
Author: Półtorak Dariusz
|
||||
Contributors: [@mathmatrix828 - Mason Phillips](https://github.com/mathmatrix828/)
|
||||
|
||||
License: [MIT](https://opensource.org/licenses/MIT)
|
18
composer.json
Normal file
18
composer.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "siteworxpro/cli-progress-bar",
|
||||
"description": "Cli progress bar",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Siteworxpro",
|
||||
"email": "websites@siteworxpro.com"
|
||||
}
|
||||
],
|
||||
"require": {},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Siteworx\\ProgressBar\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
24
examples/basic.php
Normal file
24
examples/basic.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(48);
|
||||
$bar->display();
|
||||
|
||||
$bar->setColorToRed();
|
||||
|
||||
while($bar->getCurrentstep() < $bar->getSteps()) {
|
||||
usleep(50000);
|
||||
$bar->progress();
|
||||
|
||||
if ($bar->getCurrentstep() >= ($bar->getSteps() / 2)) {
|
||||
$bar->setColorToYellow();
|
||||
}
|
||||
}
|
||||
|
||||
$bar->setColorToGreen();
|
||||
$bar->display();
|
||||
|
||||
$bar->end();
|
24
examples/basicWithEta.php
Normal file
24
examples/basicWithEta.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(100, 0, "Testing Text");
|
||||
$bar->displayTimeRemaining()->display();
|
||||
|
||||
$bar->setColorToRed();
|
||||
|
||||
while($bar->getCurrentstep() < $bar->getSteps()) {
|
||||
usleep(random_int(50000, 200000));
|
||||
$bar->progress();
|
||||
|
||||
if ($bar->getCurrentstep() >= ($bar->getSteps() / 2)) {
|
||||
$bar->setColorToYellow();
|
||||
}
|
||||
}
|
||||
|
||||
$bar->setColorToGreen();
|
||||
$bar->display();
|
||||
|
||||
$bar->end();
|
25
examples/basicWithShortBar.php
Normal file
25
examples/basicWithShortBar.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(48);
|
||||
$bar->setBarLength(5);
|
||||
$bar->display();
|
||||
|
||||
$bar->setColorToRed();
|
||||
|
||||
while($bar->getCurrentstep() < $bar->getSteps()) {
|
||||
usleep(50000);
|
||||
$bar->progress();
|
||||
|
||||
if ($bar->getCurrentstep() >= ($bar->getSteps() / 2)) {
|
||||
$bar->setColorToYellow();
|
||||
}
|
||||
}
|
||||
|
||||
$bar->setColorToGreen();
|
||||
$bar->display();
|
||||
|
||||
$bar->end();
|
24
examples/basicWithText.php
Normal file
24
examples/basicWithText.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(48, 0, "Testing Text");
|
||||
$bar->display();
|
||||
|
||||
$bar->setColorToRed();
|
||||
|
||||
while($bar->getCurrentstep() < $bar->getSteps()) {
|
||||
usleep(50000);
|
||||
$bar->progress();
|
||||
|
||||
if ($bar->getCurrentstep() >= ($bar->getSteps() / 2)) {
|
||||
$bar->setColorToYellow();
|
||||
}
|
||||
}
|
||||
|
||||
$bar->setColorToGreen();
|
||||
$bar->display();
|
||||
|
||||
$bar->end();
|
56
examples/colors.php
Normal file
56
examples/colors.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(10, 3);
|
||||
|
||||
print("THIS WILL NOT WORK UNDER WINDOWS (PROBABLY)\n\n");
|
||||
|
||||
print("BLACK\n");
|
||||
$bar->setColorToBlack();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nRED\n");
|
||||
$bar->setColorToRed();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nGREEN\n");
|
||||
$bar->setColorToGreen();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nYELLOW\n");
|
||||
$bar->setColorToYellow();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nBLUE\n\n");
|
||||
$bar->setColorToBlue();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nMAGENTA\n\n");
|
||||
$bar->setColorToMagenta();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nCYAN\n\n");
|
||||
$bar->setColorToCyan();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nWHITE\n\n");
|
||||
$bar->setColorToWhite();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nDEFAULT\n\n");
|
||||
$bar->setColorToDefault();
|
||||
$bar->display();
|
||||
$bar->end();
|
||||
|
||||
print("\nDONE!\n\n");
|
BIN
examples/img/terminal.gif
Normal file
BIN
examples/img/terminal.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.3 MiB |
8
examples/toString.php
Normal file
8
examples/toString.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(10, 3);
|
||||
print $bar . "\n";
|
10
examples/windows.php
Normal file
10
examples/windows.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
require_once('./../vendor/autoload.php');
|
||||
|
||||
$bar = new CliProgressBar(10, 3);
|
||||
$bar->displayAlternateProgressBar();
|
||||
$bar->display();
|
||||
$bar->end();
|
22
phpunit.xml
Normal file
22
phpunit.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1" />
|
||||
<ini name="intl.default_locale" value="en" />
|
||||
<ini name="intl.error_level" value="0" />
|
||||
<ini name="memory_limit" value="-1" />
|
||||
</php>
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Default">
|
||||
<directory suffix='Test.php'>./test/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
530
src/CliProgressBar.php
Normal file
530
src/CliProgressBar.php
Normal file
@@ -0,0 +1,530 @@
|
||||
<?php
|
||||
|
||||
namespace Siteworx\ProgressBar;
|
||||
|
||||
/**
|
||||
* Class CliProgressBar
|
||||
* @package Siteworx\ProgressBar
|
||||
*/
|
||||
class CliProgressBar
|
||||
{
|
||||
private const COLOR_CODE_FORMAT = "\033[%dm";
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $barLength = 40;
|
||||
|
||||
/**
|
||||
* @var array|bool
|
||||
*/
|
||||
protected $color = false;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $steps = 100;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $currentStep = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $detail = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $charEmpty = '░';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $charFull = '▓';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultCharEmpty = '░';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultCharFull = '▓';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $alternateCharEmpty = '_';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $alternateCharFull = 'X';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $displayTimeRemaining = false;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $lastIterationTime;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $totalTime = 0;
|
||||
|
||||
/**
|
||||
* CliProgressBar constructor.
|
||||
*
|
||||
* @param int $steps
|
||||
* @param int $currentStep
|
||||
* @param string $details
|
||||
* @param bool $forceDefaultProgressBar
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct($steps = 100, $currentStep = 0, $details = '', $forceDefaultProgressBar = false)
|
||||
{
|
||||
$this->setSteps($steps);
|
||||
$this->setProgressTo($currentStep);
|
||||
$this->setDetails($details);
|
||||
|
||||
// Windows terminal is unable to display utf characters and colors
|
||||
if (!$forceDefaultProgressBar && strtoupper(strpos(PHP_OS,'WIN') !== 0)) {
|
||||
$this->displayDefaultProgressBar();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CliProgressBar
|
||||
*/
|
||||
public function displayTimeRemaining(): CliProgressBar
|
||||
{
|
||||
$this->displayTimeRemaining = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currentStep
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function setProgressTo($currentStep): CliProgressBar
|
||||
{
|
||||
$this->setCurrentStep($currentStep);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function displayDefaultProgressBar(): CliProgressBar
|
||||
{
|
||||
$this->charEmpty = $this->defaultCharEmpty;
|
||||
$this->charFull = $this->defaultCharFull;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setColorToDefault(): CliProgressBar
|
||||
{
|
||||
$this->color = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setColorToBlack(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(30, 39);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $start
|
||||
* @param $end
|
||||
* @return $this
|
||||
*/
|
||||
protected function setColor($start, $end): CliProgressBar
|
||||
{
|
||||
$this->color = array(
|
||||
sprintf(self::COLOR_CODE_FORMAT, $start),
|
||||
sprintf(self::COLOR_CODE_FORMAT, $end),
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setColorToRed(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(31, 39);
|
||||
}
|
||||
|
||||
public function setColorToGreen(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(32, 39);
|
||||
}
|
||||
|
||||
public function setColorToYellow(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(33, 39);
|
||||
}
|
||||
|
||||
public function setColorToBlue(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(34, 39);
|
||||
}
|
||||
|
||||
public function setColorToMagenta(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(35, 39);
|
||||
}
|
||||
|
||||
public function setColorToCyan(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(36, 39);
|
||||
}
|
||||
|
||||
public function setColorToWhite(): CliProgressBar
|
||||
{
|
||||
return $this->setColor(37, 39);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultCharEmpty(): string
|
||||
{
|
||||
return $this->defaultCharEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $defaultCharEmpty
|
||||
*/
|
||||
public function setDefaultCharEmpty($defaultCharEmpty): void
|
||||
{
|
||||
$this->defaultCharEmpty = $defaultCharEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultCharFull(): string
|
||||
{
|
||||
return $this->defaultCharFull;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $defaultCharFull
|
||||
*/
|
||||
public function setDefaultCharFull($defaultCharFull): void
|
||||
{
|
||||
$this->defaultCharFull = $defaultCharFull;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function displayAlternateProgressBar(): CliProgressBar
|
||||
{
|
||||
$this->charEmpty = $this->alternateCharEmpty;
|
||||
$this->charFull = $this->alternateCharFull;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currentStep
|
||||
* @return $this
|
||||
*/
|
||||
public function addCurrentStep($currentStep): CliProgressBar
|
||||
{
|
||||
$this->currentStep += (int) $currentStep;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCharEmpty(): string
|
||||
{
|
||||
return $this->charEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $charEmpty
|
||||
* @return $this
|
||||
*/
|
||||
public function setCharEmpty($charEmpty): CliProgressBar
|
||||
{
|
||||
$this->charEmpty = $charEmpty;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCharFull(): string
|
||||
{
|
||||
return $this->charFull;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $charFull
|
||||
* @return $this
|
||||
*/
|
||||
public function setCharFull($charFull): CliProgressBar
|
||||
{
|
||||
$this->charFull = $charFull;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAlternateCharEmpty(): string
|
||||
{
|
||||
return $this->alternateCharEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $alternateCharEmpty
|
||||
* @return $this
|
||||
*/
|
||||
public function setAlternateCharEmpty($alternateCharEmpty): CliProgressBar
|
||||
{
|
||||
$this->alternateCharEmpty = $alternateCharEmpty;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAlternateCharFull(): string
|
||||
{
|
||||
return $this->alternateCharFull;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $alternateCharFull
|
||||
* @return $this
|
||||
*/
|
||||
public function setAlternateCharFull($alternateCharFull): CliProgressBar
|
||||
{
|
||||
$this->alternateCharFull = $alternateCharFull;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $details
|
||||
* @return $this
|
||||
*/
|
||||
public function setDetails($details): CliProgressBar
|
||||
{
|
||||
$this->detail = $details;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDetails(): string
|
||||
{
|
||||
return $this->detail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $step
|
||||
* @param bool $display
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function progress($step = 1, $display = true): CliProgressBar
|
||||
{
|
||||
$step = (int) $step;
|
||||
$this->setCurrentStep($this->getCurrentStep() + $step);
|
||||
|
||||
if ($display) {
|
||||
$this->display();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getCurrentStep(): int
|
||||
{
|
||||
return $this->currentStep;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currentStep
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function setCurrentStep($currentStep): CliProgressBar
|
||||
{
|
||||
$currentStep = (int) $currentStep;
|
||||
if ($currentStep < 0) {
|
||||
throw new \InvalidArgumentException('Current step must be 0 or above');
|
||||
}
|
||||
|
||||
$this->currentStep = $currentStep;
|
||||
if ($this->currentStep > $this->getSteps()) {
|
||||
$this->currentStep = $this->getSteps();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function display(): void
|
||||
{
|
||||
print $this->draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function draw(): string
|
||||
{
|
||||
$fullValue = $this->getSteps() !== 0 ? floor($this->getCurrentStep() / $this->getSteps() * $this->getBarLength()) : 0;
|
||||
$emptyValue = $this->getBarLength() - $fullValue;
|
||||
$prc = $this->getSteps() !== 0 ? number_format(($this->getCurrentStep() / $this->getSteps()) * 100, 1, '.', ' ') : 0.0;
|
||||
|
||||
$colorStart = '';
|
||||
$colorEnd = '';
|
||||
$timeString = '';
|
||||
if ($this->color) {
|
||||
$colorStart = $this->color[0];
|
||||
$colorEnd = $this->color[1];
|
||||
}
|
||||
|
||||
if ($this->displayTimeRemaining) {
|
||||
$timeString = $this->calculateTimeRemaining();
|
||||
}
|
||||
|
||||
$userDetail = $this->getDetails();
|
||||
$userDetail = ((\strlen($userDetail) > 1) ? "{$userDetail} " : '');
|
||||
$bar = sprintf("%4\$s%5\$s %3\$.1f%% (%1\$d/%2\$d) ", $this->getCurrentStep(), $this->getSteps(), $prc, str_repeat($this->charFull, (int) $fullValue), str_repeat($this->charEmpty, (int) $emptyValue));
|
||||
return sprintf("\r%s%s%s%s%s", $colorStart, $userDetail, $bar, $timeString, $colorEnd);
|
||||
}
|
||||
|
||||
private function calculateTimeRemaining(): string
|
||||
{
|
||||
$now = microtime(true);
|
||||
|
||||
if ($this->lastIterationTime === null) {
|
||||
$this->lastIterationTime = $now;
|
||||
return '--:--:--';
|
||||
}
|
||||
|
||||
$interval = $now - $this->lastIterationTime;
|
||||
$this->lastIterationTime = $now;
|
||||
|
||||
$this->totalTime += $interval;
|
||||
|
||||
if ($this->currentStep < 3) {
|
||||
return '--:--:--';
|
||||
}
|
||||
|
||||
$stepsRemaining = $this->steps - $this->currentStep;
|
||||
|
||||
if ($this->totalTime !== 0) {
|
||||
$avgPerStep = $this->totalTime / $this->currentStep;
|
||||
} else {
|
||||
$avgPerStep = $interval;
|
||||
}
|
||||
|
||||
$timeRemaining = round($avgPerStep * $stepsRemaining, 0);
|
||||
|
||||
$minutes = 0;
|
||||
while ($timeRemaining >= 60) {
|
||||
$minutes++;
|
||||
$timeRemaining -= 60;
|
||||
}
|
||||
|
||||
$hours = 0;
|
||||
while ($minutes >= 60) {
|
||||
$hours++;
|
||||
$minutes -= 60;
|
||||
}
|
||||
|
||||
$seconds = $timeRemaining;
|
||||
|
||||
return str_pad($hours, 2, 0, STR_PAD_LEFT) . ':' . str_pad($minutes, 2, 0, STR_PAD_LEFT) . ':' . str_pad($seconds, 2, 0, STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSteps(): int
|
||||
{
|
||||
return $this->steps;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $steps
|
||||
* @throws \Exception
|
||||
* @return $this
|
||||
*/
|
||||
public function setSteps($steps): CliProgressBar
|
||||
{
|
||||
$steps = (int) $steps;
|
||||
if ($steps < 0) {
|
||||
throw new \InvalidArgumentException('Steps amount must be 0 or above');
|
||||
}
|
||||
|
||||
$this->steps = (int) $steps;
|
||||
|
||||
$this->setCurrentStep($this->getCurrentStep());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getBarLength(): int
|
||||
{
|
||||
return $this->barLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $barLength
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function setBarLength($barLength): CliProgressBar
|
||||
{
|
||||
$barLength = (int) $barLength;
|
||||
if ($barLength < 1) {
|
||||
throw new \InvalidArgumentException('Progress bar length must be above 0');
|
||||
}
|
||||
$this->barLength = $barLength;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias to new line (nl)
|
||||
*/
|
||||
public function end(): void
|
||||
{
|
||||
$this->nl();
|
||||
}
|
||||
|
||||
/**
|
||||
* display new line
|
||||
*/
|
||||
public function nl(): void
|
||||
{
|
||||
print "\n";
|
||||
}
|
||||
}
|
117
test/CliProgressBarTest.php
Normal file
117
test/CliProgressBarTest.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
use Siteworx\ProgressBar\CliProgressBar;
|
||||
|
||||
class CliProgressBarTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testDefaultSettings(): void
|
||||
{
|
||||
$bar = new CliProgressBar();
|
||||
$this->assertEquals($bar->getBarLength(), 40, 'Bar length should be 40 at the beginning');
|
||||
$this->assertEquals($bar->getCurrentstep(), 0, 'Progress bar should start with 0');
|
||||
$this->assertEquals($bar->getSteps(), 100, 'Default number of steps should be 100');
|
||||
}
|
||||
|
||||
public function testDefaultBarString(): void
|
||||
{
|
||||
$bar = new CliProgressBar(100, 10);
|
||||
$this->assertEquals((string)$bar, "\r▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10.0% (10/100) ");
|
||||
}
|
||||
|
||||
public function testFullBarStringFromSources(): void
|
||||
{
|
||||
$fullBar = "\r▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.0% (100/100) ";
|
||||
$bar = new CliProgressBar(100, 100);
|
||||
$this->assertEquals((string)$bar, $fullBar);
|
||||
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setProgressTo(100);
|
||||
$this->assertEquals((string)$bar, $fullBar);
|
||||
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setProgressTo(99);
|
||||
$bar->progress();
|
||||
$this->assertEquals((string)$bar, $fullBar);
|
||||
}
|
||||
|
||||
public function testBarTimeEt(): void
|
||||
{
|
||||
$bar = new CliProgressBar(5);
|
||||
$bar->displayTimeRemaining()->display();
|
||||
|
||||
while ($bar->getCurrentstep() < 3) {
|
||||
$bar->progress();
|
||||
sleep(5);
|
||||
}
|
||||
|
||||
$expected = "\r▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░ 60.0% (3/5) 00:00:10";
|
||||
|
||||
$this->assertEquals($expected, (string) $bar);
|
||||
}
|
||||
|
||||
public function testBarProgress(): void
|
||||
{
|
||||
$bar = new CliProgressBar(10, 1);
|
||||
$this->assertEquals((string)$bar, "\r▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10.0% (1/10) ");
|
||||
$bar->progress();
|
||||
$this->assertEquals((string)$bar, "\r▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 20.0% (2/10) ");
|
||||
$bar->progress(2);
|
||||
$this->assertEquals((string)$bar, "\r▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░ 40.0% (4/10) ");
|
||||
$bar->setProgressTo(3);
|
||||
$this->assertEquals((string)$bar, "\r▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 30.0% (3/10) ");
|
||||
$bar->setProgressTo(99);
|
||||
$this->assertEquals((string)$bar, "\r▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.0% (10/10) ");
|
||||
}
|
||||
|
||||
public function testBarOverflow(): void
|
||||
{
|
||||
$fullBar = "\r▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.0% (100/100) ";
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setProgressTo(99);
|
||||
$bar->progress();
|
||||
$bar->progress();
|
||||
$bar->progress();
|
||||
$bar->progress();
|
||||
$this->assertEquals((string)$bar, $fullBar);
|
||||
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setProgressTo(99);
|
||||
$bar->progress(5);
|
||||
$this->assertEquals((string)$bar, $fullBar);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidConstructorStepsArgument(): void
|
||||
{
|
||||
$bar = new CliProgressBar(10, -5);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidConstructorCurrentStepArgument(): void
|
||||
{
|
||||
$bar = new CliProgressBar(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidSetCurrentStep(): void
|
||||
{
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setCurrentStep(-10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidSetProgressTo(): void
|
||||
{
|
||||
$bar = new CliProgressBar();
|
||||
$bar->setProgressTo(-10);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user