|
14 | 14 | use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration; |
15 | 15 | use ReflectionObject; |
16 | 16 | use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Helper\TableCell; |
| 18 | +use Symfony\Component\Console\Helper\TableSeparator; |
17 | 19 | use Symfony\Component\Console\Input\InputInterface; |
18 | 20 | use Symfony\Component\Console\Output\OutputInterface; |
19 | 21 | use Symfony\Component\Console\Style\SymfonyStyle; |
@@ -58,10 +60,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
58 | 60 |
|
59 | 61 | if ($configurationTableRows !== []) { |
60 | 62 | $this->symfonyStyle->title('Composer package bound rule configuration'); |
61 | | - $this->symfonyStyle->table( |
62 | | - ['Rule', 'Package', 'Requires', 'Installed', 'Active', 'Configuration'], |
63 | | - $configurationTableRows |
64 | | - ); |
| 63 | + |
| 64 | + $headers = ['Rule', 'Package', 'Requires', 'Installed', 'Active']; |
| 65 | + |
| 66 | + $table = $this->symfonyStyle->createTable(); |
| 67 | + $table->setHeaders($headers); |
| 68 | + $table->setRows($this->createConfigurationDisplayRows($configurationTableRows)); |
| 69 | + |
| 70 | + // keep columns as narrow as their content, so the full-width configuration line does not stretch them |
| 71 | + foreach ($headers as $columnPosition => $header) { |
| 72 | + $columnWidth = max(array_map( |
| 73 | + static fn (array $configurationTableRow): int => strlen($configurationTableRow[$columnPosition]), |
| 74 | + $configurationTableRows |
| 75 | + )); |
| 76 | + |
| 77 | + $table->setColumnMaxWidth($columnPosition, max($columnWidth, strlen($header))); |
| 78 | + } |
| 79 | + |
| 80 | + $table->render(); |
65 | 81 | } |
66 | 82 |
|
67 | 83 | $allTableRows = [...$tableRows, ...$configurationTableRows]; |
@@ -143,6 +159,30 @@ private function createConfigurationTableRows(): array |
143 | 159 | return $tableRows; |
144 | 160 | } |
145 | 161 |
|
| 162 | + /** |
| 163 | + * The configuration is printed on a separated full-width line, to avoid breaking the table layout |
| 164 | + * |
| 165 | + * @param array<array{string, string, string, string, string, string}> $configurationTableRows |
| 166 | + * @return array<array<TableCell|string>|TableSeparator> |
| 167 | + */ |
| 168 | + private function createConfigurationDisplayRows(array $configurationTableRows): array |
| 169 | + { |
| 170 | + $displayRows = []; |
| 171 | + |
| 172 | + foreach ($configurationTableRows as $configurationTableRow) { |
| 173 | + if ($displayRows !== []) { |
| 174 | + $displayRows[] = new TableSeparator(); |
| 175 | + } |
| 176 | + |
| 177 | + $displayRows[] = array_slice($configurationTableRow, 0, 5); |
| 178 | + $displayRows[] = [new TableCell($configurationTableRow[5], [ |
| 179 | + 'colspan' => 5, |
| 180 | + ])]; |
| 181 | + } |
| 182 | + |
| 183 | + return $displayRows; |
| 184 | + } |
| 185 | + |
146 | 186 | /** |
147 | 187 | * @param mixed[] $configuration |
148 | 188 | */ |
|
0 commit comments