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