Skip to content

Commit dd59ec9

Browse files
committed
Fix: Pad lines in code coverage report only when colors are shown
1 parent fb2c09c commit dd59ec9

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/Report/Text.php

+23-14
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

1212
use const PHP_EOL;
13+
use function array_map;
1314
use function date;
1415
use function ksort;
16+
use function max;
1517
use function sprintf;
18+
use function str_pad;
19+
use function strlen;
1620
use SebastianBergmann\CodeCoverage\CodeCoverage;
1721
use SebastianBergmann\CodeCoverage\Node\File;
1822
use SebastianBergmann\CodeCoverage\Util\Percentage;
@@ -156,28 +160,31 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
156160
$report->numberOfExecutableLines(),
157161
);
158162

163+
$padding = max(array_map('strlen', [$classes, $methods, $lines]));
164+
159165
if ($this->showOnlySummary) {
160-
$title = 'Code Coverage Report Summary:';
166+
$title = 'Code Coverage Report Summary:';
167+
$padding = max($padding, strlen($title));
161168

162-
$output .= $this->format($colors['header'], $title);
169+
$output .= $this->format($colors['header'], $padding, $title);
163170
} else {
164171
$date = date(' Y-m-d H:i:s');
165172
$title = 'Code Coverage Report:';
166173

167-
$output .= $this->format($colors['header'], $title);
168-
$output .= $this->format($colors['header'], $date);
169-
$output .= $this->format($colors['header'], '');
170-
$output .= $this->format($colors['header'], ' Summary:');
174+
$output .= $this->format($colors['header'], $padding, $title);
175+
$output .= $this->format($colors['header'], $padding, $date);
176+
$output .= $this->format($colors['header'], $padding, '');
177+
$output .= $this->format($colors['header'], $padding, ' Summary:');
171178
}
172179

173-
$output .= $this->format($colors['classes'], $classes);
174-
$output .= $this->format($colors['methods'], $methods);
180+
$output .= $this->format($colors['classes'], $padding, $classes);
181+
$output .= $this->format($colors['methods'], $padding, $methods);
175182

176183
if ($hasBranchCoverage) {
177-
$output .= $this->format($colors['paths'], $paths);
178-
$output .= $this->format($colors['branches'], $branches);
184+
$output .= $this->format($colors['paths'], $padding, $paths);
185+
$output .= $this->format($colors['branches'], $padding, $branches);
179186
}
180-
$output .= $this->format($colors['lines'], $lines);
187+
$output .= $this->format($colors['lines'], $padding, $lines);
181188

182189
if ($this->showOnlySummary) {
183190
return $output . PHP_EOL;
@@ -297,10 +304,12 @@ private function printCoverageCounts(int $numberOfCoveredElements, int $totalNum
297304
sprintf($format, $totalNumberOfElements) . ')';
298305
}
299306

300-
private function format(string $color, false|string $string): string
307+
private function format(string $color, int $padding, false|string $string): string
301308
{
302-
$reset = $color ? self::COLOR_RESET : '';
309+
if ($color === '') {
310+
return (string) $string . PHP_EOL;
311+
}
303312

304-
return $color . (string) $string . $reset . PHP_EOL;
313+
return $color . str_pad((string) $string, $padding) . self::COLOR_RESET . PHP_EOL;
305314
}
306315
}

0 commit comments

Comments
 (0)