Skip to content

Commit 294252d

Browse files
committed
Use ANSI color codes on Windows as well
Windows' console supports ANSI escape sequences since Windows 10. https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences PHP enables this by default since PHP 7.2. https://www.php.net/manual/en/function.sapi-windows-vt100-support.php On older PHP versions, the output with --colors will not be correct, but this is already the case when using this option. PHP CodeSniffer special-cased Windows in this code in 2014 (bfd095d). This workaround is no longer needed today in 2024. Note that the --colors option was already supported on Windows. This only affects inline highlighting in error and debug messages. Also document problems with printing Unicode characters to Windows console before PHP 7.1. https://www.php.net/manual/en/migration71.windows-support.php Perhaps this workaround can be removed too one day.
1 parent 98c8972 commit 294252d

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

src/Reports/Code.php

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
229229
$token = $tokens[$i];
230230
$token['content'] = $tokenContent;
231231
if (stripos(PHP_OS, 'WIN') === 0) {
232+
// Printing Unicode characters like '»' to Windows console only works since PHP 7.1.
232233
$tab = "\000";
233234
} else {
234235
$tab = "\033[30;1m»\033[0m";

src/Tokenizers/PHP.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,6 @@ protected function tokenize($string)
516516
{
517517
if (PHP_CODESNIFFER_VERBOSITY > 1) {
518518
echo "\t*** START PHP TOKENIZING ***".PHP_EOL;
519-
$isWin = false;
520-
if (stripos(PHP_OS, 'WIN') === 0) {
521-
$isWin = true;
522-
}
523519
}
524520

525521
$tokens = @token_get_all($string);
@@ -584,11 +580,7 @@ protected function tokenize($string)
584580
) {
585581
$token[1] .= "\n";
586582
if (PHP_CODESNIFFER_VERBOSITY > 1) {
587-
if ($isWin === true) {
588-
echo '\n';
589-
} else {
590-
echo "\033[30;1m\\n\033[0m";
591-
}
583+
echo "\033[30;1m\\n\033[0m";
592584
}
593585

594586
if ($tokens[($stackPtr + 1)][1] === "\n") {

src/Util/Common.php

+13-25
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ public static function escapeshellcmd($cmd)
267267
/**
268268
* Prepares token content for output to screen.
269269
*
270-
* Replaces invisible characters so they are visible. On non-Windows
271-
* operating systems it will also colour the invisible characters.
270+
* Replaces invisible characters so they are visible, and colour them.
272271
*
273272
* @param string $content The content to prepare.
274273
* @param string[] $exclude A list of characters to leave invisible.
@@ -278,35 +277,24 @@ public static function escapeshellcmd($cmd)
278277
*/
279278
public static function prepareForOutput($content, $exclude=[])
280279
{
281-
if (stripos(PHP_OS, 'WIN') === 0) {
282-
if (in_array("\r", $exclude, true) === false) {
283-
$content = str_replace("\r", '\r', $content);
284-
}
285-
286-
if (in_array("\n", $exclude, true) === false) {
287-
$content = str_replace("\n", '\n', $content);
288-
}
289-
290-
if (in_array("\t", $exclude, true) === false) {
291-
$content = str_replace("\t", '\t', $content);
292-
}
293-
} else {
294-
if (in_array("\r", $exclude, true) === false) {
295-
$content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
296-
}
280+
if (in_array("\r", $exclude, true) === false) {
281+
$content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
282+
}
297283

298-
if (in_array("\n", $exclude, true) === false) {
299-
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
300-
}
284+
if (in_array("\n", $exclude, true) === false) {
285+
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
286+
}
301287

302-
if (in_array("\t", $exclude, true) === false) {
303-
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
304-
}
288+
if (in_array("\t", $exclude, true) === false) {
289+
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
290+
}
305291

292+
if (stripos(PHP_OS, 'WIN') !== 0) {
293+
// Printing Unicode characters like '·' to Windows console only works since PHP 7.1.
306294
if (in_array(' ', $exclude, true) === false) {
307295
$content = str_replace(' ', "\033[30;1m·\033[0m", $content);
308296
}
309-
}//end if
297+
}
310298

311299
return $content;
312300

0 commit comments

Comments
 (0)