1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\ConsoleColour\Terminal; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
8
|
|
|
use function AlecRabbit\Helpers\bounds; |
9
|
|
|
|
10
|
|
|
class BenchmarkSymfonyProgressBar extends Benchmark |
11
|
|
|
{ |
12
|
|
|
public const DEFAULT_PROGRESSBAR_FORMAT = ' %percent:3s%% [%bar%] %elapsed:6s%/%estimated:-6s%'; |
13
|
|
|
public const PROGRESS_BAR_MIN_WIDTH = 60; |
14
|
|
|
public const PROGRESS_BAR_MAX_WIDTH = 80; |
15
|
|
|
|
16
|
|
|
/** @var ConsoleOutput */ |
17
|
|
|
protected $output; |
18
|
|
|
|
19
|
|
|
/** @var ProgressBar */ |
20
|
|
|
protected $progressBar; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $progressBarWidth; |
24
|
|
|
|
25
|
1 |
|
public function __construct( |
26
|
|
|
int $iterations = 1000, |
27
|
|
|
?int $progressBarMax = null, |
28
|
|
|
?int $progressBarWidth = null, |
29
|
|
|
?ConsoleOutput $output = null |
30
|
|
|
) { |
31
|
1 |
|
parent::__construct($iterations); |
32
|
1 |
|
$this->output = $output ?? new ConsoleOutput(); |
33
|
1 |
|
$this->advanceSteps = $progressBarMax ?? $this->advanceSteps; |
34
|
|
|
|
35
|
1 |
|
$this->progressBar = new ProgressBar($this->output, $this->advanceSteps); |
36
|
1 |
|
$this->progressBarWidth = $this->refineProgressBarWidth($progressBarWidth); |
37
|
1 |
|
$this->progressBar->setBarWidth($this->progressBarWidth); |
38
|
1 |
|
$this->progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT); |
39
|
|
|
|
40
|
|
|
$progressStart = |
41
|
|
|
function (): void { |
42
|
1 |
|
$this->progressBar->start(); |
43
|
1 |
|
}; |
44
|
|
|
|
45
|
|
|
$progressAdvance = |
46
|
|
|
function (): void { |
47
|
1 |
|
$this->progressBar->advance(); |
48
|
1 |
|
}; |
49
|
|
|
|
50
|
|
|
$progressFinish = |
51
|
|
|
function (): void { |
52
|
1 |
|
$this->progressBar->finish(); |
53
|
1 |
|
$this->progressBar->clear(); |
54
|
1 |
|
}; |
55
|
|
|
|
56
|
1 |
|
$this->showProgressBy($progressStart, $progressAdvance, $progressFinish); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param null|int $progressBarWidth |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
1 |
|
protected function refineProgressBarWidth(?int $progressBarWidth): int |
64
|
|
|
{ |
65
|
|
|
return |
66
|
1 |
|
(int)bounds( |
67
|
1 |
|
$progressBarWidth ?? $this->terminalWidth, |
68
|
1 |
|
static::PROGRESS_BAR_MIN_WIDTH, |
69
|
1 |
|
static::PROGRESS_BAR_MAX_WIDTH |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ConsoleOutput |
75
|
|
|
*/ |
76
|
1 |
|
public function getOutput(): ConsoleOutput |
77
|
|
|
{ |
78
|
1 |
|
return $this->output; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return ProgressBar |
83
|
|
|
*/ |
84
|
1 |
|
public function getProgressBar(): ProgressBar |
85
|
|
|
{ |
86
|
1 |
|
return $this->progressBar; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
1 |
|
public function getProgressBarWidth(): int |
93
|
|
|
{ |
94
|
1 |
|
return $this->progressBarWidth; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function showComment(string $comment = ''): void |
98
|
|
|
{ |
99
|
|
|
$this->output->writeln('<comment>' . $comment . '</>'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function sectionSeparator(?string $char): string |
103
|
|
|
{ |
104
|
|
|
return |
105
|
|
|
' ' . str_repeat( |
106
|
|
|
$char ?? static::DEFAULT_SEPARATOR_CHAR, |
107
|
|
|
$this->terminalWidth - 2 |
108
|
|
|
) . ' ' . PHP_EOL . PHP_EOL; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|