Skip to content

remove manual wrapping for error table #4035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 5 additions & 52 deletions src/Command/ErrorsConsoleStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -55,19 +54,15 @@ 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;
}

$maxHeaderWidth = $length;
}

// manual wrapping could be replaced with $table->setColumnMaxWidth()
// but it's buggy for <href> 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) {
Expand All @@ -77,58 +72,16 @@ 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);

$table->render();
$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);
Expand Down
Loading