forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorsConsoleStyle.php
More file actions
169 lines (139 loc) · 4.17 KB
/
ErrorsConsoleStyle.php
File metadata and controls
169 lines (139 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use OndraM\CiDetector\CiDetector;
use Override;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
use function array_unshift;
use function explode;
use function implode;
use function sprintf;
use function strlen;
use const DIRECTORY_SEPARATOR;
final class ErrorsConsoleStyle extends SymfonyStyle
{
public const OPTION_NO_PROGRESS = 'no-progress';
private bool $showProgress;
private ProgressBar $progressBar;
private ?bool $isCiDetected = null;
public function __construct(InputInterface $input, OutputInterface $output)
{
parent::__construct($input, $output);
$showProgress = $input->hasOption(self::OPTION_NO_PROGRESS) && !(bool) $input->getOption(self::OPTION_NO_PROGRESS);
$this->showProgress = $showProgress && !AgentDetector::isAgentDetected();
}
private function isCiDetected(): bool
{
if ($this->isCiDetected === null) {
$ciDetector = new CiDetector();
$this->isCiDetected = $ciDetector->isCiDetected();
}
return $this->isCiDetected;
}
/**
* @param string[] $headers
* @param string[][] $rows
*/
#[Override]
public function table(array $headers, array $rows): void
{
/** @var int $terminalWidth */
$terminalWidth = (new Terminal())->getWidth() - 2;
$maxHeaderWidth = strlen($headers[0]);
foreach ($rows as $row) {
$length = Helper::width(Helper::removeDecoration($this->getFormatter(), $row[0]));
if ($maxHeaderWidth !== 0 && $length <= $maxHeaderWidth) {
continue;
}
$maxHeaderWidth = $length;
}
foreach ($headers as $i => $header) {
$newHeader = [];
foreach (explode("\n", $header) as $h) {
$newHeader[] = sprintf('<info>%s</info>', $h);
}
$headers[$i] = implode("\n", $newHeader);
}
$table = $this->createTable();
// -5 because there are 5 padding spaces: One on each side of the table, one on each side of a cell and one between columns.
$table->setColumnMaxWidth(1, $terminalWidth - $maxHeaderWidth - 5);
array_unshift($rows, $headers, new TableSeparator());
$table->setRows($rows);
$table->render();
$this->newLine();
}
#[Override]
public function createProgressBar(int $max = 0): ProgressBar
{
$this->progressBar = parent::createProgressBar($max);
$format = $this->getProgressBarFormat();
if ($format !== null) {
$this->progressBar->setFormat($format);
}
$ci = $this->isCiDetected();
$agent = AgentDetector::isAgentDetected();
$this->progressBar->setOverwrite(!$ci && !$agent);
if ($ci || $agent) {
$this->progressBar->minSecondsBetweenRedraws(15);
$this->progressBar->maxSecondsBetweenRedraws(30);
} elseif (DIRECTORY_SEPARATOR === '\\') {
$this->progressBar->minSecondsBetweenRedraws(0.5);
$this->progressBar->maxSecondsBetweenRedraws(2);
} else {
$this->progressBar->minSecondsBetweenRedraws(0.1);
$this->progressBar->maxSecondsBetweenRedraws(0.5);
}
return $this->progressBar;
}
private function getProgressBarFormat(): ?string
{
switch ($this->getVerbosity()) {
case OutputInterface::VERBOSITY_NORMAL:
$formatName = ProgressBar::FORMAT_NORMAL;
break;
case OutputInterface::VERBOSITY_VERBOSE:
$formatName = ProgressBar::FORMAT_VERBOSE;
break;
case OutputInterface::VERBOSITY_VERY_VERBOSE:
case OutputInterface::VERBOSITY_DEBUG:
$formatName = ProgressBar::FORMAT_VERY_VERBOSE;
break;
default:
$formatName = null;
break;
}
if ($formatName === null) {
return null;
}
return ProgressBar::getFormatDefinition($formatName);
}
#[Override]
public function progressStart(int $max = 0): void
{
if (!$this->showProgress) {
return;
}
parent::progressStart($max);
}
#[Override]
public function progressAdvance(int $step = 1): void
{
if (!$this->showProgress) {
return;
}
parent::progressAdvance($step);
}
#[Override]
public function progressFinish(): void
{
if (!$this->showProgress) {
return;
}
parent::progressFinish();
}
}