Skip to content

Commit f37b0b6

Browse files
committed
Rendering progress bar
1 parent d7f8e59 commit f37b0b6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/Psf/Helpers/ProgressBar.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class ProgressBar
1818
private $_current = 0;
1919
private $_percent = 0;
2020
private $_counterMask = '{current}/{all} ({percent}%)';
21+
private $_barWidth = 50;
22+
private $_currentBarWidth = 0;
2123

2224
public function initialize(Writer $output, $size)
2325
{
@@ -29,10 +31,12 @@ public function increment()
2931
{
3032
$this->_current++;
3133
$this->_calculatePercent();
34+
$this->_calculateBarWidth();
35+
$fill = $this->_fillMask() . ' ' . $this->_generateBar();
3236
if ($this->_current == $this->_size) {
33-
$this->_output->writeMessage($this->_fillMask());
37+
$this->_output->writeMessage($fill);
3438
} else {
35-
$this->_output->writeMessage($this->_fillMask(), 1, Writer::CR);
39+
$this->_output->writeMessage($fill, 1, Writer::CR);
3640
}
3741
}
3842

@@ -41,6 +45,11 @@ private function _calculatePercent()
4145
$this->_percent = round($this->_current * 100 / $this->_size);
4246
}
4347

48+
private function _calculateBarWidth()
49+
{
50+
$this->_currentBarWidth = round($this->_barWidth * $this->_percent / 100);
51+
}
52+
4453
private function _fillMask()
4554
{
4655
$matches = array(
@@ -50,4 +59,9 @@ private function _fillMask()
5059
);
5160
return str_replace(array_keys($matches), array_values($matches), $this->_counterMask);
5261
}
62+
63+
private function _generateBar()
64+
{
65+
return "[" . str_pad(str_repeat('=', $this->_currentBarWidth), $this->_barWidth, '.') . "]";
66+
}
5367
}

lib/Psf/Output/Writer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ private function _parseMessage($message)
6060
foreach ($parsedTags as $xmlTag) {
6161
if (!empty($this->_formatters[$xmlTag])) {
6262
$formatter = $this->_formatters[$xmlTag];
63-
6463
$formatMessage = $formatter->render($xmlTag, $formatMessage);
6564
}
6665
}

0 commit comments

Comments
 (0)