From dfef3d4750cc4e3bb1ce5965580c0d1d4bc6efa0 Mon Sep 17 00:00:00 2001 From: schlndh Date: Fri, 30 May 2025 12:20:40 +0200 Subject: [PATCH] remove manual wrapping for error table --- src/Command/ErrorsConsoleStyle.php | 57 +++--------------------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/src/Command/ErrorsConsoleStyle.php b/src/Command/ErrorsConsoleStyle.php index f953d40c49..e7cf2c1b30 100644 --- a/src/Command/ErrorsConsoleStyle.php +++ b/src/Command/ErrorsConsoleStyle.php @@ -3,6 +3,7 @@ namespace PHPStan\Command; use OndraM\CiDetector\CiDetector; +use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Input\InputInterface; @@ -13,9 +14,7 @@ use function explode; use function implode; use function sprintf; -use function str_starts_with; use function strlen; -use function wordwrap; use const DIRECTORY_SEPARATOR; final class ErrorsConsoleStyle extends SymfonyStyle @@ -55,7 +54,8 @@ public function table(array $headers, array $rows): void $terminalWidth = (new Terminal())->getWidth() - 2; $maxHeaderWidth = strlen($headers[0]); foreach ($rows as $row) { - $length = strlen($row[0]); + $length = Helper::width(Helper::removeDecoration($this->getFormatter(), $row[0])); + if ($maxHeaderWidth !== 0 && $length <= $maxHeaderWidth) { continue; } @@ -63,11 +63,6 @@ public function table(array $headers, array $rows): void $maxHeaderWidth = $length; } - // manual wrapping could be replaced with $table->setColumnMaxWidth() - // but it's buggy for lines - // https://github.com/symfony/symfony/issues/45520 - // https://github.com/symfony/symfony/issues/45521 - $headers = $this->wrap($headers, $terminalWidth, $maxHeaderWidth); foreach ($headers as $i => $header) { $newHeader = []; foreach (explode("\n", $header) as $h) { @@ -77,11 +72,9 @@ public function table(array $headers, array $rows): void $headers[$i] = implode("\n", $newHeader); } - foreach ($rows as $i => $row) { - $rows[$i] = $this->wrap($row, $terminalWidth, $maxHeaderWidth); - } - $table = $this->createTable(); + // -5 because there are 5 padding spaces: One on each side of the table, one on each side of a cell and one between columns. + $table->setColumnMaxWidth(1, $terminalWidth - $maxHeaderWidth - 5); array_unshift($rows, $headers, new TableSeparator()); $table->setRows($rows); @@ -89,46 +82,6 @@ public function table(array $headers, array $rows): void $this->newLine(); } - /** - * @param string[] $rows - * @return string[] - */ - private function wrap(array $rows, int $terminalWidth, int $maxHeaderWidth): array - { - foreach ($rows as $i => $column) { - $columnRows = explode("\n", $column); - foreach ($columnRows as $k => $columnRow) { - if (str_starts_with($columnRow, '✏️')) { - continue; - } - $wrapped = wordwrap( - $columnRow, - $terminalWidth - $maxHeaderWidth - 5, - ); - if (str_starts_with($columnRow, '💡 ')) { - $wrappedLines = explode("\n", $wrapped); - $newWrappedLines = []; - foreach ($wrappedLines as $l => $line) { - if ($l === 0) { - $newWrappedLines[] = $line; - continue; - } - - $newWrappedLines[] = ' ' . $line; - } - $columnRows[$k] = implode("\n", $newWrappedLines); - } else { - $columnRows[$k] = $wrapped; - } - - } - - $rows[$i] = implode("\n", $columnRows); - } - - return $rows; - } - public function createProgressBar(int $max = 0): ProgressBar { $this->progressBar = parent::createProgressBar($max);