Skip to content

Commit

Permalink
Merge a4a657f into 6b53a28
Browse files Browse the repository at this point in the history
  • Loading branch information
torohill committed Jun 26, 2020
2 parents 6b53a28 + a4a657f commit e09ab07
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 6 deletions.
61 changes: 55 additions & 6 deletions src/TerminalObject/Basic/Table.php
Expand Up @@ -14,6 +14,7 @@ class Table extends BasicTerminalObject

/**
* The data for the table, an array of (arrays|objects)
* Where each row is an array of arrays, with one value per line in the column.
*
* @var array $data
*/
Expand Down Expand Up @@ -88,7 +89,48 @@ private function getData(array $input)
$output[] = $item;
}

return $output;
return $this->splitRows($output);
}

/**
* @param string $string
*
* @return array
*/
private function stringToLines(string $string)
{
return preg_split('/(\r\n|\r|\n)/u', $string);
}

/**
* Split each row in $data into an array of arrays
* Where each value represents a line in the column
*
* @param array $data
*
* @return array
*/
private function splitRows($data)
{
foreach ($data as $row_key => $row) {
$height = 1;
$lines = [];
foreach ($row as $key => $column) {
$lines[$key] = $this->stringToLines($column);
$height = max($height, count($lines[$key]));
}
$keys = array_keys($row);
$new_rows = [];
for ($i = 0; $i < $height; $i++) {
$new_row = [];
foreach ($keys as $key) {
$new_row[$key] = $lines[$key][$i] ?? '';
}
$new_rows[] = $new_row;
}
$data[$row_key] = $new_rows;
}
return $data;
}


Expand All @@ -105,8 +147,10 @@ public function result()

$this->buildHeaderRow();

foreach ($this->data as $columns) {
$this->addLine($this->buildRow($columns));
foreach ($this->data as $row_columns) {
foreach ($row_columns as $columns) {
$this->addLine($this->buildRow($columns));
}
$this->addLine($this->border);
}

Expand Down Expand Up @@ -134,6 +178,7 @@ private function addLine($line)
protected function getWidth()
{
$first_row = reset($this->data);
$first_row = reset($first_row);
$first_row = $this->buildRow($first_row);

return $this->lengthWithoutTags($first_row);
Expand Down Expand Up @@ -203,6 +248,7 @@ protected function buildCell($key, $column)
protected function getHeaderRow()
{
$first_item = reset($this->data);
$first_item = reset($first_item);

$keys = array_keys($first_item);
$first_key = reset($keys);
Expand All @@ -223,13 +269,16 @@ protected function getHeaderRow()
protected function getColumnWidths()
{
$first_row = reset($this->data);
$first_row = reset($first_row);

// Create an array with the columns as keys and values of zero
$column_widths = $this->getDefaultColumnWidths($first_row);

foreach ($this->data as $columns) {
foreach ($columns as $key => $column) {
$column_widths[$key] = $this->getCellWidth($column_widths[$key], $column);
foreach ($this->data as $row_columns) {
foreach ($row_columns as $columns) {
foreach ($columns as $key => $column) {
$column_widths[$key] = $this->getCellWidth($column_widths[$key], $column);
}
}
}

Expand Down
61 changes: 61 additions & 0 deletions tests/TableTest.php
Expand Up @@ -180,4 +180,65 @@ public function testInvalidData1()
"whoops",
]);
}

public function testTableWithNewline()
{
$this->shouldWrite("\e[m-----------------------------------\e[0m");
$this->shouldWrite("\e[m| Cell 1 | Cell | Cell 3 | Cell 4 |\e[0m");
$this->shouldWrite("\e[m| | 2 | | |\e[0m");
$this->shouldWrite("\e[m-----------------------------------\e[0m");

$this->shouldHavePersisted();

$this->cli->table([
[
'Cell 1',
"Cell\n2",
'Cell 3',
'Cell 4',
],
]);
}

public function testTableWithNewlineAndObjects()
{
$this->shouldWrite("\e[m------------------------------------\e[0m");
$this->shouldWrite("\e[m| cell1 | cell2 | cell3 | cell4 |\e[0m");
$this->shouldWrite("\e[m====================================\e[0m");
$this->shouldWrite("\e[m| Cell 1 | Cell | Cell 3 | Cell 4 |\e[0m");
$this->shouldWrite("\e[m| | 2 | | |\e[0m");
$this->shouldWrite("\e[m------------------------------------\e[0m");

$this->shouldHavePersisted();

$this->cli->table([
(object) [
'cell1' => 'Cell 1',
'cell2' => "Cell\n2",
'cell3' => 'Cell 3',
'cell4' => 'Cell 4',
],
]);
}

public function testTableWithMultipleNewlines()
{
$this->shouldWrite("\e[m---------------------------------\e[0m");
$this->shouldWrite("\e[m| Cell 1 | Cell | Cell | Cell |\e[0m");
$this->shouldWrite("\e[m| | 2 | 3 | 4 |\e[0m");
$this->shouldWrite("\e[m| | | Cell 3 | Cell |\e[0m");
$this->shouldWrite("\e[m| | | | 4 |\e[0m");
$this->shouldWrite("\e[m---------------------------------\e[0m");

$this->shouldHavePersisted();

$this->cli->table([
[
'Cell 1',
"Cell\n2",
"Cell\n3\nCell 3",
"Cell\n4\nCell\n4",
],
]);
}
}

0 comments on commit e09ab07

Please sign in to comment.