Skip to content

Commit 2ec254b

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 PHP CodeSniffer special-cased Windows in this code in 2014 (bfd095d). This 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.
1 parent d02c686 commit 2ec254b

File tree

3 files changed

+15
-42
lines changed

3 files changed

+15
-42
lines changed

src/Reports/Code.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
228228
if (strpos($tokenContent, "\t") !== false) {
229229
$token = $tokens[$i];
230230
$token['content'] = $tokenContent;
231-
if (stripos(PHP_OS, 'WIN') === 0) {
232-
$tab = "\000";
233-
} else {
234-
$tab = "\033[30;1m»\033[0m";
235-
}
231+
$tab = "\033[30;1m»\033[0m";
236232

237233
$phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000");
238234
$tokenContent = $token['content'];

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-28
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,21 @@ 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

306-
if (in_array(' ', $exclude, true) === false) {
307-
$content = str_replace(' ', "\033[30;1m·\033[0m", $content);
308-
}
309-
}//end if
292+
if (in_array(' ', $exclude, true) === false) {
293+
$content = str_replace(' ', "\033[30;1m·\033[0m", $content);
294+
}
310295

311296
return $content;
312297

0 commit comments

Comments
 (0)