Skip to content

Commit 0f3e9fc

Browse files
Merge pull request #100 from TheDragonCode/4.x
Add `disableProgressBar` method to suppress progress bar display in console
2 parents ce1c246 + 32d3a62 commit 0f3e9fc

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,20 @@ new Benchmark()
686686
->toBeTotalMemory(till: 4096); // the memory footprint should not exceed 4096 bytes
687687
```
688688

689+
### Disable Progress Bar
690+
691+
In some cases, it is necessary to disable the progressbar so that it does not interfere in the console.
692+
693+
To do this, use the `disableProgressBar` method:
694+
695+
```PHP
696+
use DragonCode\Benchmark\Benchmark;
697+
698+
new Benchmark()
699+
->disableProgressBar()
700+
// ...
701+
```
702+
689703
## License
690704

691705
This package is licensed under the [MIT License](LICENSE).

src/Benchmark.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ public function round(?int $precision): static
113113
return $this;
114114
}
115115

116+
public function disableProgressBar(): static
117+
{
118+
$this->view->progressBar()->disable();
119+
120+
return $this;
121+
}
122+
116123
public function compare(array|Closure ...$callbacks): static
117124
{
118125
$this->clear();

src/View/ProgressBarView.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010

1111
class ProgressBarView extends View
1212
{
13+
protected bool $enabled = true;
14+
1315
protected int $current = 0;
1416

1517
protected int $barWidth = 28;
1618

1719
protected int $total = 100;
1820

21+
public function disable(): static
22+
{
23+
$this->enabled = false;
24+
25+
return $this;
26+
}
27+
1928
public function create(int $total): static
2029
{
2130
$this->total = max(1, $total);
@@ -38,11 +47,17 @@ public function finish(): void
3847

3948
$this->display();
4049

41-
$this->write(PHP_EOL);
50+
if ($this->enabled) {
51+
$this->write(PHP_EOL);
52+
}
4253
}
4354

4455
protected function display(): void
4556
{
57+
if (! $this->enabled) {
58+
return;
59+
}
60+
4661
$percent = $this->current / $this->total;
4762
$filled = (int) floor($percent * $this->barWidth);
4863
$empty = $this->barWidth - $filled;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
4+
+-------+-------------------------------+------------------------------+
5+
| # | 0 | 1 |
6+
+-------+-------------------------------+------------------------------+
7+
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
8+
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
9+
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
10+
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
11+
+-------+-------------------------------+------------------------------+
12+
| order | 2 | 1 |
13+
+-------+-------------------------------+------------------------------+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
test('to console', function () {
6+
benchmark()
7+
->disableProgressBar()
8+
->toConsole();
9+
10+
expectOutputToMatchSnapshot();
11+
});
12+
13+
test('to data', function () {
14+
benchmark()
15+
->disableProgressBar()
16+
->toData();
17+
18+
expectOutputToMatchSnapshot();
19+
});
20+
21+
test('to assert', function () {
22+
benchmark()
23+
->disableProgressBar()
24+
->toAssert();
25+
26+
expectOutputToMatchSnapshot();
27+
});

0 commit comments

Comments
 (0)