91% of 403OPs |
86% of 51Lines |
93% of 43Branches |
10% of 159Paths |
| Method | OPs | OPs % | Lines | Line % | Branches | Branches % | Paths | Path % |
|---|---|---|---|---|---|---|---|---|
| mageekguy\atoum\cli\progressBar::__construct() | 31 | 100% | 5 | 100% | 1 | 0% | 1 | 100% |
| mageekguy\atoum\cli\progressBar::reset() | 23 | 0% | 6 | 0% | 1 | 0% | 1 | 0% |
| mageekguy\atoum\cli\progressBar::setIterations() | 15 | 0% | 2 | 50% | 1 | 0% | 1 | 0% |
| mageekguy\atoum\cli\progressBar::setCli() | 9 | 100% | 2 | 100% | 1 | 100% | 1 | 100% |
| mageekguy\atoum\cli\progressBar::getCli() | 6 | 100% | 1 | 100% | 1 | 100% | 1 | 100% |
| mageekguy\atoum\cli\progressBar::__toString() | 300 | 100% | 30 | 100% | 31 | 100% | 150 | 7% |
| mageekguy\atoum\cli\progressBar::refresh() | 19 | 100% | 5 | 100% | 7 | 100% | 4 | 75% |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\cli; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum |
| 7 |
; |
| 8 |
|
| 9 |
class progressBar |
| 10 |
{
|
| 11 |
const width = 60; |
| 12 |
const defaultProgressBarFormat = '[%s]'; |
| 13 |
const defaultCounterFormat = '[%s/%s]'; |
| 14 |
|
| 15 |
protected $cli = null; |
| 16 |
protected $refresh = null; |
| 17 |
protected $progressBar = null; |
| 18 |
protected $progressBarFormat = null; |
| 19 |
protected $counter = null; |
| 20 |
protected $counterFormat = null; |
| 21 |
protected $iterations = 0; |
| 22 |
protected $currentIteration = 0; |
| 23 |
|
| 24 |
public function __construct($iterations = 0, atoum\cli $cli = null)100% |
| 25 |
{
|
| 26 |
$this->iterations = $iterations; |
| 27 |
$this->progressBarFormat = self::defaultProgressBarFormat; |
| 28 |
$this->counterFormat = self::defaultCounterFormat; |
| 29 |
|
| 30 |
$this->setCli($cli ?: new atoum\cli()); |
| 31 |
} |
| 32 |
|
| 33 |
public function reset()0% |
| 34 |
{
|
| 35 |
$this->refresh = null; |
| 36 |
$this->iterations = 0; |
| 37 |
$this->currentIteration = 0; |
| 38 |
$this->progressBar = null; |
| 39 |
$this->counter = null; |
| 40 |
|
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
public function setIterations($iterations)50% |
| 45 |
{
|
| 46 |
$this->reset()->iterations = (int) $iterations; |
| 47 |
|
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
public function setCli(atoum\cli $cli)100% |
| 52 |
{
|
| 53 |
$this->cli = $cli; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
public function getCli()100% |
| 59 |
{
|
| 60 |
return $this->cli; |
| 61 |
} |
| 62 |
|
| 63 |
public function __toString()100% |
| 64 |
{
|
| 65 |
$string = ''; |
| 66 |
|
| 67 |
if ($this->progressBar === null && $this->counter === null) |
| 68 |
{
|
| 69 |
$this->progressBar = sprintf($this->progressBarFormat, ($this->iterations > self::width ? str_repeat('.', self::width - 1) . '>' : str_pad(str_repeat('.', $this->iterations), self::width, '_', STR_PAD_RIGHT)));
|
| 70 |
|
| 71 |
$this->counter = sprintf($this->counterFormat, sprintf('%' . strlen((string) $this->iterations) . 'd', $this->currentIteration), $this->iterations);
|
| 72 |
|
| 73 |
$string .= $this->progressBar . $this->counter; |
| 74 |
} |
| 75 |
|
| 76 |
if ($this->refresh !== null) |
| 77 |
{
|
| 78 |
$refreshLength = strlen($this->refresh); |
| 79 |
|
| 80 |
$this->currentIteration += $refreshLength; |
| 81 |
|
| 82 |
if ($this->cli->isTerminal() === false) |
| 83 |
{
|
| 84 |
$this->progressBar = substr($this->progressBar, 0, $this->currentIteration) . $this->refresh . substr($this->progressBar, $this->currentIteration + 1); |
| 85 |
$string .= PHP_EOL . $this->progressBar; |
| 86 |
} |
| 87 |
else |
| 88 |
{
|
| 89 |
$string .= str_repeat("\010", (strlen($this->progressBar) - $refreshLength) + strlen($this->counter));
|
| 90 |
$this->progressBar = $this->refresh . substr($this->progressBar, $refreshLength + 1); |
| 91 |
$string .= $this->progressBar; |
| 92 |
} |
| 93 |
|
| 94 |
$this->counter = sprintf($this->counterFormat, sprintf('%' . strlen((string) $this->iterations) . 'd', $this->currentIteration), $this->iterations);
|
| 95 |
|
| 96 |
$string .= $this->counter; |
| 97 |
|
| 98 |
if ($this->iterations > self::width && $this->iterations - $this->currentIteration && $this->currentIteration % (self::width - 1) == 0) |
| 99 |
{
|
| 100 |
$this->progressBar = '[' . (($this->iterations - $this->currentIteration) > (self::width - 1) ? str_repeat('.', self::width - 1) . '>' : str_pad(str_repeat('.', $this->iterations - $this->currentIteration), self::width, '_', STR_PAD_RIGHT)) . ']';
|
| 101 |
$this->counter = ''; |
| 102 |
|
| 103 |
$string .= PHP_EOL . $this->progressBar; |
| 104 |
} |
| 105 |
|
| 106 |
$this->refresh = null; |
| 107 |
} |
| 108 |
|
| 109 |
return $string; |
| 110 |
} |
| 111 |
|
| 112 |
public function refresh($value)100% |
| 113 |
{
|
| 114 |
if ($this->iterations > 0 && $this->currentIteration < $this->iterations) |
| 115 |
{
|
| 116 |
$this->refresh .= $value; |
| 117 |
} |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
} |