Skip to content

Commit 18cb0d4

Browse files
anomiexjrfnl
andauthored
Report Full: fix two line wrapping bugs (#125)
* Fix two line wrapping bugs in default report formatter 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. * Report Full: iterate on line wrapping bug fix This commit takes the fix one step further by adding the padding only after the message has been word-wrapped. Includes correct handling of padding for multi-line error messages. It then takes the last line of the resulting message and determines in isolation whether the source code suffix can fit on that line or needs to be placed on a new line. Co-authored-by: Brad Jorsch <anomie@users.sourceforge.net> Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
1 parent 7fb9515 commit 18cb0d4

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

src/Reports/Full.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,36 +115,51 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
115115
// The maximum amount of space an error message can use.
116116
$maxErrorSpace = ($width - $paddingLength - 1);
117117

118+
$beforeMsg = '';
119+
$afterMsg = '';
120+
if ($showSources === true) {
121+
$beforeMsg = "\033[1m";
122+
$afterMsg = "\033[0m";
123+
}
124+
125+
$beforeAfterLength = strlen($beforeMsg.$afterMsg);
126+
118127
foreach ($report['messages'] as $line => $lineErrors) {
119128
foreach ($lineErrors as $column => $colErrors) {
120129
foreach ($colErrors as $error) {
121-
$message = $error['message'];
122-
$msgLines = [$message];
123-
if (strpos($message, "\n") !== false) {
124-
$msgLines = explode("\n", $message);
125-
}
130+
$errorMsg = wordwrap(
131+
$error['message'],
132+
$maxErrorSpace
133+
);
126134

127-
$errorMsg = '';
128-
$lastLine = (count($msgLines) - 1);
129-
foreach ($msgLines as $k => $msgLine) {
130-
if ($k === 0) {
131-
if ($showSources === true) {
132-
$errorMsg .= "\033[1m";
133-
}
134-
} else {
135-
$errorMsg .= PHP_EOL.$paddingLine2;
136-
}
135+
// Add the padding _after_ the wordwrap as the message itself may contain line breaks
136+
// and those lines will also need to receive padding.
137+
$errorMsg = str_replace("\n", $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg, $errorMsg);
138+
$errorMsg = $beforeMsg.$errorMsg.$afterMsg;
137139

138-
if ($k === $lastLine && $showSources === true) {
139-
$msgLine .= "\033[0m".' ('.$error['source'].')';
140+
if ($showSources === true) {
141+
$lastMsg = $errorMsg;
142+
$startPosLastLine = strrpos($errorMsg, PHP_EOL.$paddingLine2.$beforeMsg);
143+
if ($startPosLastLine !== false) {
144+
// Message is multiline. Grab the text of last line of the message, including the color codes.
145+
$lastMsg = substr($errorMsg, ($startPosLastLine + strlen(PHP_EOL.$paddingLine2)));
140146
}
141147

142-
$errorMsg .= wordwrap(
143-
$msgLine,
144-
$maxErrorSpace,
145-
PHP_EOL.$paddingLine2
146-
);
147-
}
148+
// When show sources is used, the message itself will be bolded, so we need to correct the length.
149+
$sourceSuffix = '('.$error['source'].')';
150+
151+
$lastMsgPlusSourceLength = strlen($lastMsg);
152+
// Add space + source suffix length.
153+
$lastMsgPlusSourceLength += (1 + strlen($sourceSuffix));
154+
// Correct for the color codes.
155+
$lastMsgPlusSourceLength -= $beforeAfterLength;
156+
157+
if ($lastMsgPlusSourceLength > $maxErrorSpace) {
158+
$errorMsg .= PHP_EOL.$paddingLine2.$sourceSuffix;
159+
} else {
160+
$errorMsg .= ' '.$sourceSuffix;
161+
}
162+
}//end if
148163

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

0 commit comments

Comments
 (0)