Skip to content

Commit

Permalink
ErrorsConsoleStyle - improve table wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 23, 2022
1 parent d1c2577 commit bc481b7
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/Command/ErrorsConsoleStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
use function array_map;
use function explode;
use function implode;
use function str_starts_with;
use function strlen;
use function wordwrap;
use const DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -58,20 +60,45 @@ public function table(array $headers, array $rows): void
$maxHeaderWidth = $length;
}

$wrap = static fn ($rows): array => array_map(static fn ($row): array => array_map(static function ($s) use ($terminalWidth, $maxHeaderWidth) {
if ($terminalWidth > $maxHeaderWidth + 5) {
return wordwrap(
$s,
$terminalWidth - $maxHeaderWidth - 5,
"\n",
true,
);
}
// manual wrapping could be replaced with $table->setColumnMaxWidth()
// but it's buggy for <href> lines
$headers = $this->wrap($headers, $terminalWidth, $maxHeaderWidth);
foreach ($rows as $i => $row) {
$rows[$i] = $this->wrap($row, $terminalWidth, $maxHeaderWidth);
}

$table = $this->createTable();
$table->setHeaders($headers);
$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;
}
$columnRows[$k] = wordwrap(
$columnRow,
$terminalWidth - $maxHeaderWidth - 5,
"\n",
true,
);
}

return $s;
}, $row), $rows);
$rows[$i] = implode("\n", $columnRows);
}

parent::table($headers, $wrap($rows));
return $rows;
}

public function createProgressBar(int $max = 0): ProgressBar
Expand Down

0 comments on commit bc481b7

Please sign in to comment.