Skip to content

Commit

Permalink
Show more detailed progress percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Nov 3, 2024
1 parent 5a44bef commit e88d3c9
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function floor;
use function microtime;
use function min;
use function number_format;
use function str_pad;
use function str_repeat;
use function strlen;
Expand Down Expand Up @@ -39,11 +40,13 @@ protected function buildProgressBar(float $percent): string
{
$fill = (int) floor($percent * $this->width);

$progress = number_format($percent * 100, 2, '.', '');

return str_pad($this->progress, strlen($this->itemCount), '0', STR_PAD_LEFT)
. "/{$this->itemCount} "
. str_repeat($this->progressBar->getFilled(), $fill)
. str_repeat($this->progressBar->getEmpty(), ($this->width - $fill))
. str_pad(' ' . ((int) ($percent * 100)) . '% ', 6, ' ', STR_PAD_LEFT);
. str_pad(" {$progress}% ", 9, ' ', STR_PAD_LEFT);
}

/**
Expand Down
128 changes: 83 additions & 45 deletions tests/unit/cli/output/components/ProgressIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testProgressWithZeroItems(): void
$output->shouldReceive('showCursor')->once();
$output->shouldReceive('restoreCursor'); // Destructor

$output->shouldReceive('write')->once()->with("\r0/0 ████████████████████ 100% ");
$output->shouldReceive('write')->once()->with("\r0/0 ████████████████████ 100.00% ");
$output->shouldReceive('write')->once()->with(PHP_EOL);

$progress = new ProgressIterator($output, []);
Expand All @@ -51,17 +51,17 @@ public function testProgressWithItems(): void
$output->shouldReceive('showCursor')->once();
$output->shouldReceive('restoreCursor'); // Destructor

$output->shouldReceive('write')->once()->with("\r00/10 ──────────────────── 0% ");
$output->shouldReceive('write')->once()->with("\r01/10 ██────────────────── 10% ");
$output->shouldReceive('write')->once()->with("\r02/10 ████──────────────── 20% ");
$output->shouldReceive('write')->once()->with("\r03/10 ██████────────────── 30% ");
$output->shouldReceive('write')->once()->with("\r04/10 ████████──────────── 40% ");
$output->shouldReceive('write')->once()->with("\r05/10 ██████████────────── 50% ");
$output->shouldReceive('write')->once()->with("\r06/10 ████████████──────── 60% ");
$output->shouldReceive('write')->once()->with("\r07/10 ██████████████────── 70% ");
$output->shouldReceive('write')->once()->with("\r08/10 ████████████████──── 80% ");
$output->shouldReceive('write')->once()->with("\r09/10 ██████████████████── 90% ");
$output->shouldReceive('write')->once()->with("\r10/10 ████████████████████ 100% ");
$output->shouldReceive('write')->once()->with("\r00/10 ──────────────────── 0.00% ");
$output->shouldReceive('write')->once()->with("\r01/10 ██────────────────── 10.00% ");
$output->shouldReceive('write')->once()->with("\r02/10 ████──────────────── 20.00% ");
$output->shouldReceive('write')->once()->with("\r03/10 ██████────────────── 30.00% ");
$output->shouldReceive('write')->once()->with("\r04/10 ████████──────────── 40.00% ");
$output->shouldReceive('write')->once()->with("\r05/10 ██████████────────── 50.00% ");
$output->shouldReceive('write')->once()->with("\r06/10 ████████████──────── 60.00% ");
$output->shouldReceive('write')->once()->with("\r07/10 ██████████████────── 70.00% ");
$output->shouldReceive('write')->once()->with("\r08/10 ████████████████──── 80.00% ");
$output->shouldReceive('write')->once()->with("\r09/10 ██████████████████── 90.00% ");
$output->shouldReceive('write')->once()->with("\r10/10 ████████████████████ 100.00% ");
$output->shouldReceive('write')->once()->with(PHP_EOL);

$progress = new class ($output, range(1, 10)) extends ProgressIterator {
Expand All @@ -76,6 +76,44 @@ protected function shouldRedraw(): bool
}
}

/**
*
*/
public function testProgressWithUnevenItems(): void
{
/** @var \Mockery\MockInterface|Output $output */
$output = Mockery::mock(Output::class);

$output->shouldReceive('hideCursor')->once();
$output->shouldReceive('showCursor')->once();
$output->shouldReceive('restoreCursor'); // Destructor

$output->shouldReceive('write')->once()->with("\r00/11 ──────────────────── 0.00% ");
$output->shouldReceive('write')->once()->with("\r01/11 █─────────────────── 9.09% ");
$output->shouldReceive('write')->once()->with("\r02/11 ███───────────────── 18.18% ");
$output->shouldReceive('write')->once()->with("\r03/11 █████─────────────── 27.27% ");
$output->shouldReceive('write')->once()->with("\r04/11 ███████───────────── 36.36% ");
$output->shouldReceive('write')->once()->with("\r05/11 █████████─────────── 45.45% ");
$output->shouldReceive('write')->once()->with("\r06/11 ██████████────────── 54.55% ");
$output->shouldReceive('write')->once()->with("\r07/11 ████████████──────── 63.64% ");
$output->shouldReceive('write')->once()->with("\r08/11 ██████████████────── 72.73% ");
$output->shouldReceive('write')->once()->with("\r09/11 ████████████████──── 81.82% ");
$output->shouldReceive('write')->once()->with("\r10/11 ██████████████████── 90.91% ");
$output->shouldReceive('write')->once()->with("\r11/11 ████████████████████ 100.00% ");
$output->shouldReceive('write')->once()->with(PHP_EOL);

$progress = new class ($output, range(1, 11)) extends ProgressIterator {
protected function shouldRedraw(): bool
{
return ($this->progress % 1) === 0;
}
};

foreach ($progress as $_) {
//
}
}

/**
*
*/
Expand All @@ -88,17 +126,17 @@ public function testProgressWithCustomWidth(): void
$output->shouldReceive('showCursor')->once();
$output->shouldReceive('restoreCursor'); // Destructor

$output->shouldReceive('write')->once()->with("\r00/10 ──────────────────────────────────────── 0% ");
$output->shouldReceive('write')->once()->with("\r01/10 ████──────────────────────────────────── 10% ");
$output->shouldReceive('write')->once()->with("\r02/10 ████████──────────────────────────────── 20% ");
$output->shouldReceive('write')->once()->with("\r03/10 ████████████──────────────────────────── 30% ");
$output->shouldReceive('write')->once()->with("\r04/10 ████████████████──────────────────────── 40% ");
$output->shouldReceive('write')->once()->with("\r05/10 ████████████████████──────────────────── 50% ");
$output->shouldReceive('write')->once()->with("\r06/10 ████████████████████████──────────────── 60% ");
$output->shouldReceive('write')->once()->with("\r07/10 ████████████████████████████──────────── 70% ");
$output->shouldReceive('write')->once()->with("\r08/10 ████████████████████████████████──────── 80% ");
$output->shouldReceive('write')->once()->with("\r09/10 ████████████████████████████████████──── 90% ");
$output->shouldReceive('write')->once()->with("\r10/10 ████████████████████████████████████████ 100% ");
$output->shouldReceive('write')->once()->with("\r00/10 ──────────────────────────────────────── 0.00% ");
$output->shouldReceive('write')->once()->with("\r01/10 ████──────────────────────────────────── 10.00% ");
$output->shouldReceive('write')->once()->with("\r02/10 ████████──────────────────────────────── 20.00% ");
$output->shouldReceive('write')->once()->with("\r03/10 ████████████──────────────────────────── 30.00% ");
$output->shouldReceive('write')->once()->with("\r04/10 ████████████████──────────────────────── 40.00% ");
$output->shouldReceive('write')->once()->with("\r05/10 ████████████████████──────────────────── 50.00% ");
$output->shouldReceive('write')->once()->with("\r06/10 ████████████████████████──────────────── 60.00% ");
$output->shouldReceive('write')->once()->with("\r07/10 ████████████████████████████──────────── 70.00% ");
$output->shouldReceive('write')->once()->with("\r08/10 ████████████████████████████████──────── 80.00% ");
$output->shouldReceive('write')->once()->with("\r09/10 ████████████████████████████████████──── 90.00% ");
$output->shouldReceive('write')->once()->with("\r10/10 ████████████████████████████████████████ 100.00% ");
$output->shouldReceive('write')->once()->with(PHP_EOL);

$progress = new class ($output, range(1, 10), width: 40) extends ProgressIterator {
Expand All @@ -125,17 +163,17 @@ public function testProgressWithCustomTemplates(): void
$output->shouldReceive('showCursor')->once();
$output->shouldReceive('restoreCursor'); // Destructor

$output->shouldReceive('write')->once()->with("\r00/10 -------------------- 0% ");
$output->shouldReceive('write')->once()->with("\r01/10 ==------------------ 10% ");
$output->shouldReceive('write')->once()->with("\r02/10 ====---------------- 20% ");
$output->shouldReceive('write')->once()->with("\r03/10 ======-------------- 30% ");
$output->shouldReceive('write')->once()->with("\r04/10 ========------------ 40% ");
$output->shouldReceive('write')->once()->with("\r05/10 ==========---------- 50% ");
$output->shouldReceive('write')->once()->with("\r06/10 ============-------- 60% ");
$output->shouldReceive('write')->once()->with("\r07/10 ==============------ 70% ");
$output->shouldReceive('write')->once()->with("\r08/10 ================---- 80% ");
$output->shouldReceive('write')->once()->with("\r09/10 ==================-- 90% ");
$output->shouldReceive('write')->once()->with("\r10/10 ==================== 100% ");
$output->shouldReceive('write')->once()->with("\r00/10 -------------------- 0.00% ");
$output->shouldReceive('write')->once()->with("\r01/10 ==------------------ 10.00% ");
$output->shouldReceive('write')->once()->with("\r02/10 ====---------------- 20.00% ");
$output->shouldReceive('write')->once()->with("\r03/10 ======-------------- 30.00% ");
$output->shouldReceive('write')->once()->with("\r04/10 ========------------ 40.00% ");
$output->shouldReceive('write')->once()->with("\r05/10 ==========---------- 50.00% ");
$output->shouldReceive('write')->once()->with("\r06/10 ============-------- 60.00% ");
$output->shouldReceive('write')->once()->with("\r07/10 ==============------ 70.00% ");
$output->shouldReceive('write')->once()->with("\r08/10 ================---- 80.00% ");
$output->shouldReceive('write')->once()->with("\r09/10 ==================-- 90.00% ");
$output->shouldReceive('write')->once()->with("\r10/10 ==================== 100.00% ");
$output->shouldReceive('write')->once()->with(PHP_EOL);

$progress = new class ($output, range(1, 10), progressBar: new AsciiProgressBar) extends ProgressIterator {
Expand All @@ -162,17 +200,17 @@ public function testProgressWithDescription(): void
$output->shouldReceive('showCursor')->once();
$output->shouldReceive('restoreCursor'); // Destructor

$output->shouldReceive('write')->once()->with("\rProcessing files: 00/10 ──────────────────── 0% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 01/10 ██────────────────── 10% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 02/10 ████──────────────── 20% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 03/10 ██████────────────── 30% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 04/10 ████████──────────── 40% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 05/10 ██████████────────── 50% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 06/10 ████████████──────── 60% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 07/10 ██████████████────── 70% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 08/10 ████████████████──── 80% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 09/10 ██████████████████── 90% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 10/10 ████████████████████ 100% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 00/10 ──────────────────── 0.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 01/10 ██────────────────── 10.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 02/10 ████──────────────── 20.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 03/10 ██████────────────── 30.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 04/10 ████████──────────── 40.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 05/10 ██████████────────── 50.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 06/10 ████████████──────── 60.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 07/10 ██████████████────── 70.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 08/10 ████████████████──── 80.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 09/10 ██████████████████── 90.00% ");
$output->shouldReceive('write')->once()->with("\rProcessing files: 10/10 ████████████████████ 100.00% ");
$output->shouldReceive('write')->once()->with(PHP_EOL);

$progress = new class ($output, range(1, 10), description: 'Processing files:') extends ProgressIterator {
Expand Down
Loading

0 comments on commit e88d3c9

Please sign in to comment.