Skip to content

Commit

Permalink
Use ANSI color codes on Windows as well
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MatmaRex committed Nov 9, 2024
1 parent d02c686 commit 2ec254b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
6 changes: 1 addition & 5 deletions src/Reports/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
if (strpos($tokenContent, "\t") !== false) {
$token = $tokens[$i];
$token['content'] = $tokenContent;
if (stripos(PHP_OS, 'WIN') === 0) {
$tab = "\000";
} else {
$tab = "\033[30;1m»\033[0m";
}
$tab = "\033[30;1m»\033[0m";

$phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000");
$tokenContent = $token['content'];
Expand Down
10 changes: 1 addition & 9 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,6 @@ protected function tokenize($string)
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START PHP TOKENIZING ***".PHP_EOL;
$isWin = false;
if (stripos(PHP_OS, 'WIN') === 0) {
$isWin = true;
}
}

$tokens = @token_get_all($string);
Expand Down Expand Up @@ -584,11 +580,7 @@ protected function tokenize($string)
) {
$token[1] .= "\n";
if (PHP_CODESNIFFER_VERBOSITY > 1) {
if ($isWin === true) {
echo '\n';
} else {
echo "\033[30;1m\\n\033[0m";
}
echo "\033[30;1m\\n\033[0m";
}

if ($tokens[($stackPtr + 1)][1] === "\n") {
Expand Down
41 changes: 13 additions & 28 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ public static function escapeshellcmd($cmd)
/**
* Prepares token content for output to screen.
*
* Replaces invisible characters so they are visible. On non-Windows
* operating systems it will also colour the invisible characters.
* Replaces invisible characters so they are visible, and colour them.
*
* @param string $content The content to prepare.
* @param string[] $exclude A list of characters to leave invisible.
Expand All @@ -278,35 +277,21 @@ public static function escapeshellcmd($cmd)
*/
public static function prepareForOutput($content, $exclude=[])
{
if (stripos(PHP_OS, 'WIN') === 0) {
if (in_array("\r", $exclude, true) === false) {
$content = str_replace("\r", '\r', $content);
}

if (in_array("\n", $exclude, true) === false) {
$content = str_replace("\n", '\n', $content);
}

if (in_array("\t", $exclude, true) === false) {
$content = str_replace("\t", '\t', $content);
}
} else {
if (in_array("\r", $exclude, true) === false) {
$content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
}
if (in_array("\r", $exclude, true) === false) {
$content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
}

if (in_array("\n", $exclude, true) === false) {
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
}
if (in_array("\n", $exclude, true) === false) {
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
}

if (in_array("\t", $exclude, true) === false) {
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
}
if (in_array("\t", $exclude, true) === false) {
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
}

if (in_array(' ', $exclude, true) === false) {
$content = str_replace(' ', "\033[30;1m·\033[0m", $content);
}
}//end if
if (in_array(' ', $exclude, true) === false) {
$content = str_replace(' ', "\033[30;1m·\033[0m", $content);
}

return $content;

Expand Down

0 comments on commit 2ec254b

Please sign in to comment.