Skip to content
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

Fix multi line cell and longer $listPrefix in Table widget #19906

Merged
merged 24 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
83fda4b
Merge pull request #1 from yiisoft/master
rhertogh Jul 30, 2015
5231894
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jan 24, 2020
e388dfc
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jul 16, 2020
6b56e2c
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Aug 20, 2020
1676da7
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Oct 31, 2020
accf01e
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh May 23, 2021
8856e22
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Jun 2, 2021
22f4a41
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Jun 23, 2021
6548c45
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Aug 15, 2021
315bda2
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Sep 13, 2021
3db5c78
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Sep 15, 2021
c164bea
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jan 17, 2022
1b5c680
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh May 16, 2022
91a8582
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jun 13, 2022
c1b3877
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Oct 27, 2022
43b4425
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Apr 10, 2023
bedfa1f
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh May 17, 2023
412399b
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh May 28, 2023
acad979
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jun 26, 2023
6036621
Merge remote-tracking branch 'origin/master'
rhertogh Jul 21, 2023
5b25698
Support multi line cell values in `\yii\console\widgets\Table`
rhertogh Jul 21, 2023
cdd1866
Updated changelog for #19906 (Fixed multiline strings in the `\yii\c…
rhertogh Jul 21, 2023
92d50cf
Added `\yiiunit\framework\console\widgets\TableTest::testNumericTable()`
rhertogh Jul 21, 2023
1d8be31
Fixed rendering of long multiline strings and longer $listPrefix in `…
rhertogh Jul 22, 2023
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
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Yii Framework 2 Change Log
- Enh #19853: Added support for default value for `\yii\helpers\Console::select()` (rhertogh)
- Bug #19868: Added whitespace sanitation for tests, due to updates in ICU 72 (schmunk42)
- Enh #19884: Added support Enums in Query Builder (sk1t0n)
- Bug #19906: Fixed multiline strings in the `\yii\console\widgets\Table` widget (rhertogh)


2.0.48.1 May 24, 2023
Expand Down
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 @@
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#L262-L263

Added lines #L262 - 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;
if (!isset($arrayPointer[$index])) {
$arrayPointer[$index] = 0;
}
Expand Down Expand Up @@ -339,6 +349,9 @@
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#L354

Added line #L354 was not covered by tests
return Console::ansiStrwidth($val);
}, $column)) + 2;
$this->columnWidths[] = $columnWidth;
Expand Down Expand Up @@ -388,6 +401,9 @@
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#L406

Added line #L406 was not covered by tests
return Console::ansiStrwidth($val);
}, $row));
return max($rowsPerCell);
Expand Down
95 changes: 95 additions & 0 deletions tests/framework/console/widgets/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,101 @@ 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
->setHeaders($headers)
->setRows($rows)
->setScreenWidth(200)
->run();
$this->assertEqualsWithoutLE($expected, $tableContent);
}

public function getNumericTableData()
{
return [
[
[1, 2, 3],
[
[1, 1.2, -1.3],
[-2, 2.2, 2.3],
]
],
[
['key1' => 1, 'key2' => 2, 'key3' => 3],
[
['key1' => 1, 'key2' => 1.2, 'key3' => -1.3],
['key1' => -2, 'key2' => 2.2, 'key3' => 2.3],
]
]
];
}

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

$expected = <<<'EXPECTED'
╔════╤═════╤══════╗
║ 1 │ 2 │ 3 ║
╟────┼─────┼──────╢
║ 1 │ 1.2 │ -1.3 ║
╟────┼─────┼──────╢
║ -2 │ 2.2 │ 2.3 ║
╚════╧═════╧══════╝

EXPECTED;

$tableContent = $table
Expand Down
Loading