Skip to content

Commit 9241537

Browse files
committed
Use colors when outputting information
1 parent 4b76676 commit 9241537

File tree

6 files changed

+214
-75
lines changed

6 files changed

+214
-75
lines changed

Scripts/DocCodeExamples/Check.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ public function run(): int
137137
$validationFailedCount = 0;
138138

139139
if (empty($this->xmlFiles)) {
140-
$this->writer->toStderr('ERROR: No XML documentation files found.');
140+
$message = 'ERROR: No XML documentation files found.';
141+
142+
if ($this->config->getProperty('showColored') === true) {
143+
$message = "\033[31m$message\033[0m";
144+
}
145+
146+
$this->writer->toStderr($message . PHP_EOL);
141147
return 1;
142148
}
143149

@@ -164,13 +170,21 @@ public function run(): int
164170
}
165171

166172
if ($exitCode === 0) {
167-
$feedback .= ' All code examples are valid.' . PHP_EOL;
168-
$this->writer->toStdout($feedback);
173+
$feedback .= ' All code examples are valid.';
174+
175+
if ($this->config->getProperty('showColored') === true) {
176+
$feedback = "\033[32m{$feedback}\033[0m";
177+
}
169178
} else {
170-
$feedback .= " Found incorrect code examples in {$validationFailedCount}." . PHP_EOL;
171-
$this->writer->toStdout($feedback);
179+
$feedback .= " Found incorrect code examples in {$validationFailedCount}.";
180+
181+
if ($this->config->getProperty('showColored') === true) {
182+
$feedback = "\033[31m{$feedback}\033[0m";
183+
}
172184
}
173185

186+
$this->writer->toStdout($feedback . PHP_EOL);
187+
174188
return $exitCode;
175189
}
176190

@@ -187,7 +201,8 @@ protected function getXmlDocValidator(string $xmlDocFilePath): XmlDocValidator
187201
$this->config->getProperty('projectRoot') . $xmlDocFilePath,
188202
$this->extractor,
189203
$this->phpcsConfig,
190-
$this->writer
204+
$this->writer,
205+
$this->config
191206
);
192207
}
193208
}

Scripts/DocCodeExamples/Config.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class Config
6767
*/
6868
protected $executeCheck = true;
6969

70+
/**
71+
* Whether or not to show colored output.
72+
*
73+
* This is automatically detected.
74+
*
75+
* @var bool
76+
*/
77+
protected $showColored;
78+
7079
/**
7180
* Help texts.
7281
*
@@ -123,7 +132,8 @@ class Config
123132
*/
124133
public function __construct(Writer $writer)
125134
{
126-
$this->writer = $writer;
135+
$this->writer = $writer;
136+
$this->showColored = HelpTextFormatter::isColorSupported();
127137
$this->processCliCommand();
128138
}
129139

@@ -173,7 +183,7 @@ private function processCliCommand()
173183
$argsFlipped = \array_flip($args);
174184

175185
if (isset($argsFlipped['--help'])) {
176-
$helpText = HelpTextFormatter::format($this->helpTexts, HelpTextFormatter::isColorSupported());
186+
$helpText = HelpTextFormatter::format($this->helpTexts, $this->showColored);
177187
$this->writer->toStdout($this->getVersion());
178188
$this->writer->toStdout($helpText);
179189
$this->executeCheck = false;

Scripts/DocCodeExamples/XmlDocValidator.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class XmlDocValidator
7373
*/
7474
private $writer;
7575

76+
/**
77+
* Configuration as passed on the command line.
78+
*
79+
* @var \PHPCSDevTools\Scripts\DocCodeExamples\Config
80+
*/
81+
private $config;
82+
7683
/**
7784
* Whether the first error message for this XML file has been displayed or not. Used to control
7885
* when to display a header for the error messages with the XML file name.
@@ -88,6 +95,7 @@ class XmlDocValidator
8895
* @param CodeBlocksExtractor $extractor CodeBlocksExtractor instance.
8996
* @param PHPCSConfig $phpcsConfig PHPCS configuration object.
9097
* @param Writer $writer A helper object to write output.
98+
* @param Config $config Configuration as passed on the command line.
9199
*
92100
* @throws \RuntimeException If the XML file doesn't exist, the file name or directory structure
93101
* is invalid, or the standard is not installed in PHPCS.
@@ -96,7 +104,8 @@ public function __construct(
96104
string $xmlDocFile,
97105
CodeBlocksExtractor $extractor,
98106
PHPCSConfig $phpcsConfig,
99-
Writer $writer
107+
Writer $writer,
108+
Config $config
100109
) {
101110
if (!\file_exists($xmlDocFile)) {
102111
throw new \RuntimeException("The XML file \"{$xmlDocFile}\" does not exist.");
@@ -105,12 +114,13 @@ public function __construct(
105114
$this->extractor = $extractor;
106115
$this->phpcsConfig = $phpcsConfig;
107116
$this->writer = $writer;
117+
$this->config = $config;
108118
$this->path = $xmlDocFile;
109119
$pathInfo = \pathinfo($this->path);
110120

111121
if (\substr($pathInfo['basename'], -12) !== 'Standard.xml') {
112122
throw new \RuntimeException(
113-
"The XML file \"{$pathInfo['basename']}\" is invalid. File names should end in \"Standard.xml\"."
123+
"ERROR: The XML file \"{$pathInfo['basename']}\" is invalid. File names should end in \"Standard.xml\"."
114124
);
115125
}
116126

@@ -119,7 +129,7 @@ public function __construct(
119129

120130
if (\count($dirs) < 3 || $dirs[\count($dirs) - 2] !== 'Docs') {
121131
throw new \RuntimeException(
122-
'Invalid directory structure in the XML file path. Expected: '
132+
'ERROR: Invalid directory structure in the XML file path. Expected: '
123133
. '{STANDARD_NAME}/Docs/{CATEGORY_NAME}/' . $pathInfo['basename'] . '.'
124134
);
125135
}
@@ -130,7 +140,7 @@ public function __construct(
130140

131141
if (Standards::isInstalledStandard($this->standard) === false) {
132142
throw new \RuntimeException(
133-
"The standard \"{$this->standard}\" is not installed in PHPCS."
143+
"ERROR: The standard \"{$this->standard}\" is not installed in PHPCS."
134144
);
135145
}
136146
}
@@ -230,7 +240,7 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
230240
// @phpstan-ignore function.resultUnused (maybe a PHPStan false positive?)
231241
\token_get_all($codeBlockContent, \TOKEN_PARSE);
232242
} catch (\Throwable $e) {
233-
$message = 'ERROR: There is a syntax error in the code block.' . \PHP_EOL;
243+
$message = 'There is a syntax error in the code block.' . \PHP_EOL;
234244
$message .= $e->getMessage() . \PHP_EOL;
235245
$this->displayErrorMessage(
236246
$this->prepareCodeBlockErrorMessage($message, $codeBlock)
@@ -248,7 +258,7 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
248258
$file->process();
249259

250260
if ($isExpectedToBeValid === true && ($file->getErrorCount() !== 0 || $file->getWarningCount() !== 0)) {
251-
$message = 'ERROR: Code block is valid and PHPCS should have returned nothing, ' .
261+
$message = 'Code block is valid and PHPCS should have returned nothing, ' .
252262
'but instead it returned an error.';
253263
$this->displayErrorMessage(
254264
$this->prepareCodeBlockErrorMessage($message, $codeBlock)
@@ -257,7 +267,7 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
257267
}
258268

259269
if ($isExpectedToBeValid === false && ($file->getErrorCount() === 0 && $file->getWarningCount() === 0)) {
260-
$message = 'ERROR: Code block is invalid and PHPCS should have returned an error message, ' .
270+
$message = 'Code block is invalid and PHPCS should have returned an error message, ' .
261271
'but instead it returned nothing.';
262272
$this->displayErrorMessage(
263273
$this->prepareCodeBlockErrorMessage($message, $codeBlock)
@@ -278,7 +288,13 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
278288
*/
279289
private function prepareCodeBlockErrorMessage(string $message, CodeBlock $codeBlock): string
280290
{
281-
$errorMessage = $message . \PHP_EOL;
291+
$errorPrefix = 'ERROR: ';
292+
293+
if ($this->config->getProperty('showColored') === true) {
294+
$errorPrefix = "\033[31m{$errorPrefix}\033[0m";
295+
}
296+
297+
$errorMessage = $errorPrefix . $message . \PHP_EOL;
282298
$errorMessage .= "Code block title: \"{$codeBlock->title}\"" . \PHP_EOL;
283299
$errorMessage .= "Code block content: \"{$codeBlock->content}\"" . \PHP_EOL;
284300

@@ -296,7 +312,13 @@ private function prepareCodeBlockErrorMessage(string $message, CodeBlock $codeBl
296312
private function displayErrorMessage(string $message)
297313
{
298314
if ($this->firstErrorMessageDisplayed === false) {
299-
$this->writer->toStderr("Errors found while processing {$this->path}" . \PHP_EOL . \PHP_EOL);
315+
$errorHeader = "Errors found while processing {$this->path}";
316+
317+
if ($this->config->getProperty('showColored') === true) {
318+
$errorHeader = "\033[31m{$errorHeader}\033[0m";
319+
}
320+
321+
$this->writer->toStderr($errorHeader . \PHP_EOL . \PHP_EOL);
300322
}
301323

302324
$this->firstErrorMessageDisplayed = true;

0 commit comments

Comments
 (0)