From 134d7812d6fcecb717d4573e4dd4de01421a9f29 Mon Sep 17 00:00:00 2001 From: Mason Phillips Date: Fri, 16 Sep 2016 13:41:35 -0400 Subject: [PATCH] Adding ability to insert text, updating readme and including contribs --- README.md | 21 ++++++++++++++++++++- examples/basicWithText.php | 24 ++++++++++++++++++++++++ src/Dariuszp/CliProgressBar.php | 29 ++++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 examples/basicWithText.php diff --git a/README.md b/README.md index 5ac97fc..993a36a 100644 --- a/README.md +++ b/README.md @@ -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) @@ -53,4 +71,5 @@ in [example](examples/) directory. License: [MIT](https://opensource.org/licenses/MIT) -Author: Półtorak Dariusz \ No newline at end of file +Author: Półtorak Dariusz +Contributors: [@mathmatrix828 - Mason Phillips](https://github.com/mathmatrix828/) diff --git a/examples/basicWithText.php b/examples/basicWithText.php new file mode 100644 index 0000000..cca3770 --- /dev/null +++ b/examples/basicWithText.php @@ -0,0 +1,24 @@ +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(); diff --git a/src/Dariuszp/CliProgressBar.php b/src/Dariuszp/CliProgressBar.php index 744bba2..3041f1f 100644 --- a/src/Dariuszp/CliProgressBar.php +++ b/src/Dariuszp/CliProgressBar.php @@ -30,6 +30,11 @@ class CliProgressBar */ protected $currentStep = 0; + /** + * @var string + */ + protected $detail = ""; + /** * @var string */ @@ -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') { @@ -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 @@ -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); } /** @@ -416,4 +439,4 @@ public function nl() { print "\n"; } -} \ No newline at end of file +}