Skip to content

Commit

Permalink
Support multi line cell values in \yii\console\widgets\Table
Browse files Browse the repository at this point in the history
  • Loading branch information
rhertogh committed Jul 21, 2023
1 parent 6036621 commit 5b25698
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
18 changes: 17 additions & 1 deletion framework/console/widgets/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,21 @@ protected function renderRow(array $row, $spanLeft, $spanMiddle, $spanRight)
if ($index !== 0) {
$buffer .= $spanMiddle . ' ';
}

$arrayFromMultilineString = false;
if (is_string($cell)) {
$cellLines = explode(PHP_EOL, $cell);
if (count($cellLines) > 1) {
$cell = $cellLines;
$arrayFromMultilineString = true;
}
}

Check warning on line 263 in framework/console/widgets/Table.php

View check run for this annotation

Codecov / codecov/patch

framework/console/widgets/Table.php#L256-L263

Added lines #L256 - L263 were not covered by tests

if (is_array($cell)) {
if (empty($renderedChunkTexts[$index])) {
$renderedChunkTexts[$index] = '';
$start = 0;
$prefix = $this->listPrefix;
$prefix = $arrayFromMultilineString ? '' : $this->listPrefix;

Check warning on line 269 in framework/console/widgets/Table.php

View check run for this annotation

Codecov / codecov/patch

framework/console/widgets/Table.php#L269

Added line #L269 was not covered by tests
if (!isset($arrayPointer[$index])) {
$arrayPointer[$index] = 0;
}
Expand Down Expand Up @@ -339,6 +349,9 @@ protected function calculateRowsSize()
if (is_array($val)) {
return max(array_map('yii\helpers\Console::ansiStrwidth', $val)) + Console::ansiStrwidth($this->listPrefix);
}
if (is_string($val)) {
return max(array_map('yii\helpers\Console::ansiStrwidth', explode(PHP_EOL, $val)));
}

Check warning on line 354 in framework/console/widgets/Table.php

View check run for this annotation

Codecov / codecov/patch

framework/console/widgets/Table.php#L352-L354

Added lines #L352 - L354 were not covered by tests
return Console::ansiStrwidth($val);
}, $column)) + 2;
$this->columnWidths[] = $columnWidth;
Expand Down Expand Up @@ -388,6 +401,9 @@ protected function calculateRowHeight($row)
if (is_array($val)) {
return array_map('yii\helpers\Console::ansiStrwidth', $val);
}
if (is_string($val)) {
return array_map('yii\helpers\Console::ansiStrwidth', explode(PHP_EOL, $val));
}

Check warning on line 406 in framework/console/widgets/Table.php

View check run for this annotation

Codecov / codecov/patch

framework/console/widgets/Table.php#L404-L406

Added lines #L404 - L406 were not covered by tests
return Console::ansiStrwidth($val);
}, $row));
return max($rowsPerCell);
Expand Down
49 changes: 49 additions & 0 deletions tests/framework/console/widgets/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,55 @@ public function testTable($headers, $rows)
║ testcontent21 │ testcontent22 │ testcontent23 ║
╚═══════════════╧═══════════════╧═══════════════╝

EXPECTED;

$tableContent = $table
->setHeaders($headers)
->setRows($rows)
->setScreenWidth(200)
->run();
$this->assertEqualsWithoutLE($expected, $tableContent);
}

public function getMultiLineTableData()
{
return [
[
['test1', 'test2', 'test3' . PHP_EOL . 'multiline'],
[
['test' . PHP_EOL . 'content1', 'testcontent2', 'test' . PHP_EOL . 'content3'],
['testcontent21', 'test' . PHP_EOL . 'content22', 'testcontent23'],
]
],
[
['key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3' . PHP_EOL . 'multiline'],
[
['key1' => 'test' . PHP_EOL . 'content1', 'key2' => 'testcontent2', 'key3' => 'test' . PHP_EOL . 'content3'],
['key1' => 'testcontent21', 'key2' => 'test' . PHP_EOL . 'content22', 'key3' => 'testcontent23'],
]
]
];
}

/**
* @dataProvider getMultiLineTableData
*/
public function testMultiLineTable($headers, $rows)
{
$table = new Table();

$expected = <<<'EXPECTED'
╔═══════════════╤══════════════╤═══════════════╗
║ test1 │ test2 │ test3 ║
║ │ │ multiline ║
╟───────────────┼──────────────┼───────────────╢
║ test │ testcontent2 │ test ║
║ content1 │ │ content3 ║
╟───────────────┼──────────────┼───────────────╢
║ testcontent21 │ test │ testcontent23 ║
║ │ content22 │ ║
╚═══════════════╧══════════════╧═══════════════╝

EXPECTED;

$tableContent = $table
Expand Down

0 comments on commit 5b25698

Please sign in to comment.