Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding ability to insert text, updating readme and including contribs
  • Loading branch information
MatrixSenpai committed Sep 16, 2016
1 parent 68a94b8 commit 134d781
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
21 changes: 20 additions & 1 deletion README.md
Expand Up @@ -41,6 +41,24 @@ Output will be:
XXXX____________________________________ 10.0% (10/100)
```

Add text to the progress bar using the following methods
```php
use Dariuszp\CliProgressBar;
$bar = new CliProgressBar(50, 0, "My Custom Text");
$bar->display();
$bar->end();
```
or
```php
use Dariuszp\CliProgressBar;
$bar = new CliProgressBar();
$bar->setDetails("My Custom Text");
$bar->display();
$bar->end();
```

Also update asynchronously with setDetails()

More features like:
- changing progress bar length (basicWithShortBar.php)
- changing bar color (colors.php)
Expand All @@ -53,4 +71,5 @@ in [example](examples/) directory.

License: [MIT](https://opensource.org/licenses/MIT)

Author: Półtorak Dariusz
Author: Półtorak Dariusz
Contributors: [@mathmatrix828 - Mason Phillips](https://github.com/mathmatrix828/)
24 changes: 24 additions & 0 deletions examples/basicWithText.php
@@ -0,0 +1,24 @@
<?php

require_once('./../vendor/autoload.php');

use Dariuszp\CliProgressBar;

$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();
29 changes: 26 additions & 3 deletions src/Dariuszp/CliProgressBar.php
Expand Up @@ -30,6 +30,11 @@ class CliProgressBar
*/
protected $currentStep = 0;

/**
* @var string
*/
protected $detail = "";

/**
* @var string
*/
Expand Down Expand Up @@ -59,10 +64,11 @@ class CliProgressBar
*/
protected $alternateCharFull = 'X';

public function __construct($steps = 100, $currentStep = 0, $forceDefaultProgressBar = false)
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(substr(PHP_OS, 0, 3)) === 'WIN') {
Expand Down Expand Up @@ -277,6 +283,21 @@ public function setAlternateCharFull($alternateCharFull)
return $this;
}

/**
* @param string $details
* @return $this
*/
public function setDetails($details)
{
$this->detail = $details;
return $this;
}

public function getDetails()
{
return $this->detail;
}

/**
* @param int $step
* @param bool $display
Expand Down Expand Up @@ -341,8 +362,10 @@ public function draw()
$colorEnd = $this->color[1];
}

$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, $fullValue), str_repeat($this->charEmpty, $emptyValue));
return sprintf("\r%s%s%s", $colorStart, $bar, $colorEnd);
return sprintf("\r%s%s%s%s", $colorStart, $userDetail, $bar, $colorEnd);
}

/**
Expand Down Expand Up @@ -416,4 +439,4 @@ public function nl()
{
print "\n";
}
}
}

0 comments on commit 134d781

Please sign in to comment.