Skip to content

Commit e3e05ce

Browse files
committed
Fixes report-impact option output
1 parent 6a0b47a commit e3e05ce

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
77

88
## [Unreleased]
99

10+
### Fixed
11+
- The `--report-impact` option output is now formatted correctly.
12+
1013
### Changed
1114
- Removed the `symfony/finder` dependency.
1215

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Would expect the following .gitattributes file content:
207207
bin/release-version export-ignore
208208
```
209209

210-
The `--report-stale-export-ignores` option extends the validation to look for export-ignore statements referencing
210+
The `--report-stale-export-ignores` option extends the validation to look for export-ignore statements referencing
211211
non-existent repository artefacts. In combination with the `--diff` option these will be shown in the output.
212212

213213
The `--report-impact` option displays a distribution impact report that compares the project archive with and without
@@ -218,7 +218,7 @@ savings and CO₂ emission avoided based on a typical download volume.
218218
lean-package-validator validate --report-impact [<directory>]
219219

220220
Distribution impact
221-
────────────────────────────────
221+
───────────────────────────────────────────
222222
Original archive: 1.25 MB
223223
Optimized archive: 0.45 MB
224224
Size reduction: 64.0%

‎src/ImpactReport.php‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ public static function formatCo2(float $grams): string
294294
return \number_format($grams, 2) . ' g CO₂';
295295
}
296296

297+
/**
298+
* Pad a string to a specified display width (handles multi-byte characters).
299+
*
300+
* @param string $string
301+
* @param int $width
302+
* @return string
303+
*/
304+
private static function strPadDisplayWidth(string $string, int $width): string
305+
{
306+
$currentWidth = \mb_strwidth($string);
307+
if ($currentWidth >= $width) {
308+
return $string;
309+
}
310+
311+
return $string . \str_repeat(' ', $width - $currentWidth);
312+
}
313+
297314
/**
298315
* Generate the formatted impact report string.
299316
*
@@ -308,17 +325,19 @@ public function generateReport(): string
308325
$estimatedDataSaved = self::formatBytesToGb($this->getEstimatedDataSavedBytes());
309326
$co2Avoided = self::formatCo2($this->getEstimatedCo2Avoided());
310327

311-
// Label column width for alignment
328+
// Label column display width for alignment
312329
$labelWidth = 32;
330+
// Separator width
331+
$separatorWidth = 43;
313332

314333
$report = "Distribution impact" . PHP_EOL;
315-
$report .= \str_repeat('─', $labelWidth) . PHP_EOL;
316-
$report .= \str_pad('Original archive:', $labelWidth) . ' ' . $originalSize . PHP_EOL;
317-
$report .= \str_pad('Optimized archive:', $labelWidth) . ' ' . $optimizedSize . PHP_EOL;
318-
$report .= \str_pad('Size reduction:', $labelWidth) . ' ' . $reduction . PHP_EOL;
319-
$report .= \str_pad('Data saved/download:', $labelWidth) . ' ' . $dataSaved . PHP_EOL;
320-
$report .= \str_pad('Estimated data saved:', $labelWidth) . ' ' . $estimatedDataSaved . PHP_EOL;
321-
$report .= \str_pad('Estimated CO₂ emission avoided:', $labelWidth) . ' ' . $co2Avoided . PHP_EOL;
334+
$report .= \str_repeat('─', $separatorWidth) . PHP_EOL;
335+
$report .= self::strPadDisplayWidth('Original archive:', $labelWidth) . $originalSize . PHP_EOL;
336+
$report .= self::strPadDisplayWidth('Optimized archive:', $labelWidth) . $optimizedSize . PHP_EOL;
337+
$report .= self::strPadDisplayWidth('Size reduction:', $labelWidth) . $reduction . PHP_EOL;
338+
$report .= self::strPadDisplayWidth('Data saved/download:', $labelWidth) . $dataSaved . PHP_EOL;
339+
$report .= self::strPadDisplayWidth('Estimated data saved:', $labelWidth) . $estimatedDataSaved . PHP_EOL;
340+
$report .= self::strPadDisplayWidth('Estimated CO₂ emission avoided:', $labelWidth) . $co2Avoided . PHP_EOL;
322341

323342
return $report;
324343
}

0 commit comments

Comments
 (0)