Skip to content

Commit

Permalink
Fix two line wrapping bugs in default report formatter
Browse files Browse the repository at this point in the history
The first is that the ANSI escape codes applied to bold the message when
`-s` is used were not being taken into account when wrapping the lines
for width, causing some lines to be wrapped unnecessarily.

The second is that when lines were wrapped in the middle of a long
message, the `|` characters making up the table were being bolded along
with the message.
  • Loading branch information
anomiex committed Dec 10, 2023
1 parent 7fb9515 commit 0febbfb
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/Reports/Full.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
// The maximum amount of space an error message can use.
$maxErrorSpace = ($width - $paddingLength - 1);

if ($showSources === true) {
$beforeMsg = "\033[1m";
$afterMsg = "\033[0m";
} else {
$beforeMsg = '';
$afterMsg = '';
}

foreach ($report['messages'] as $line => $lineErrors) {
foreach ($lineErrors as $column => $colErrors) {
foreach ($colErrors as $error) {
Expand All @@ -128,23 +136,35 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
$lastLine = (count($msgLines) - 1);
foreach ($msgLines as $k => $msgLine) {
if ($k === 0) {
if ($showSources === true) {
$errorMsg .= "\033[1m";
}
$errorMsg .= $beforeMsg;
} else {
$errorMsg .= PHP_EOL.$paddingLine2;
}

if ($k === $lastLine && $showSources === true) {
$msgLine .= "\033[0m".' ('.$error['source'].')';
$errorMsg .= $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg;
}

$errorMsg .= wordwrap(
$wrappedLines = wordwrap(
$msgLine,
$maxErrorSpace,
PHP_EOL.$paddingLine2
$afterMsg.PHP_EOL.$paddingLine2.$beforeMsg
);
}
$errorMsg .= $wrappedLines;

if ($k === $lastLine) {
$errorMsg .= $afterMsg;
if ($showSources === true) {
$lastLineLength = strlen($wrappedLines);
$lastNewlinePos = strrpos($wrappedLines, PHP_EOL);
if ($lastNewlinePos !== false) {
$lastLineLength -= ($lastNewlinePos + strlen(PHP_EOL.$paddingLine2.$beforeMsg));
}

if (($lastLineLength + strlen($error['source']) + 3) > $maxErrorSpace) {
$errorMsg .= PHP_EOL.$paddingLine2.'('.$error['source'].')';
} else {
$errorMsg .= ' ('.$error['source'].')';
}
}
}
}//end foreach

// The padding that goes on the front of the line.
$padding = ($maxLineNumLength - strlen($line));
Expand Down

0 comments on commit 0febbfb

Please sign in to comment.