Skip to content

Commit 880a25b

Browse files
committed
Common: prepareForOutput(): Use single color code per run of characters
This reduces the size of the output, and lessens weird effects of the ANSI color code characters on text wrapping, when the result of this method is used in a sniff error message (e.g. see LanguageConstructSpacingSniff).
1 parent 98c8972 commit 880a25b

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

src/Util/Common.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -278,35 +278,26 @@ public static function escapeshellcmd($cmd)
278278
*/
279279
public static function prepareForOutput($content, $exclude=[])
280280
{
281+
$replacements = [
282+
"\r" => '\r',
283+
"\n" => '\n',
284+
"\t" => '\t',
285+
" " => '·',
286+
];
281287
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-
}
288+
// Do not replace spaces on Windows.
289+
unset($replacements[" "]);
290+
}
297291

298-
if (in_array("\n", $exclude, true) === false) {
299-
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
300-
}
292+
$replacements = array_diff_key($replacements, array_fill_keys($exclude, true));
301293

302-
if (in_array("\t", $exclude, true) === false) {
303-
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
304-
}
294+
if (stripos(PHP_OS, 'WIN') !== 0) {
295+
// On non-Windows, colour runs of invisible characters.
296+
$match = implode('', array_keys($replacements));
297+
$content = preg_replace("/([$match]+)/", "\033[30;1m$1\033[0m", $content);
298+
}
305299

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

311302
return $content;
312303

tests/Core/Util/Common/PrepareForOutputTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ public static function dataPrepareForOutput()
7575
'Special characters are replaced with their escapes' => [
7676
'content' => "\r\n\t",
7777
'exclude' => [],
78-
'expected' => "\033[30;1m\\r\033[0m\033[30;1m\\n\033[0m\033[30;1m\\t\033[0m",
78+
'expected' => "\033[30;1m\\r\\n\\t\033[0m",
7979
'expectedWin' => "\\r\\n\\t",
8080
],
8181
'Spaces are replaced with a unique mark' => [
8282
'content' => " ",
8383
'exclude' => [],
84-
'expected' => "\033[30;1m·\033[0m\033[30;1m·\033[0m\033[30;1m·\033[0m\033[30;1m·\033[0m",
84+
'expected' => "\033[30;1m····\033[0m",
8585
'expectedWin' => " ",
8686
],
8787
'Other characters are unaffected' => [
@@ -102,7 +102,7 @@ public static function dataPrepareForOutput()
102102
"\r",
103103
"\n",
104104
],
105-
'expected' => "\r\n\033[30;1m\\t\033[0m\033[30;1m·\033[0m",
105+
'expected' => "\r\n\033[30;1m\\\033[0m",
106106
'expectedWin' => "\r\n\\t ",
107107
],
108108
];

0 commit comments

Comments
 (0)