diff --git a/README.md b/README.md index 084dbc8..31ee4b3 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ CLI Table Output for PHP - Data manipulators for fields, formats raw data to a nice display output - Colors! When specifying a color, choose from these strings: blue, red, green, yellow, black, magenta, cyan, white, grey +- Support for multi-line values. ### Installing via Composer diff --git a/docs/Documentation.md b/docs/Documentation.md index 21d6ca9..3085271 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -1,10 +1,11 @@ -## Table of contents +# Table of contents - [\jc21\CliTable](#class-jc21clitable) - [\jc21\CliTableManipulator](#class-jc21clitablemanipulator)
-### Class: \jc21\CliTable + +## Class: \jc21\CliTable | Visibility | Function | |:-----------|:---------| @@ -34,7 +35,8 @@ | protected | getTableTop(array $columnLengths) : string
getTableTop |
-### Class: \jc21\CliTableManipulator + +## Class: \jc21\CliTableManipulator | Visibility | Function | |:-----------|:---------| diff --git a/example.png b/example.png index 09f3f0b..5de0bf3 100644 Binary files a/example.png and b/example.png differ diff --git a/src/jc21/CliTable.php b/src/jc21/CliTable.php index b071205..90a7e95 100644 --- a/src/jc21/CliTable.php +++ b/src/jc21/CliTable.php @@ -385,7 +385,10 @@ public function get() { $columnLengths[$key] = 0; } $c = chr(27); - $columnLengths[$key] = max($columnLengths[$key], strlen(preg_replace("/({$c}\[(.*?)m)/s", '', $value))); + $lines = explode("\n", preg_replace("/({$c}\[(.*?)m)/s", '', $value)); + foreach ($lines as $line) { + $columnLengths[$key] = max($columnLengths[$key], mb_strlen($line)); + } } $rowCount++; } @@ -439,27 +442,40 @@ public function get() { * @return string */ protected function getFormattedRow($rowData, $columnLengths, $header = false) { - $response = $this->getChar('left'); + $response = ''; - foreach ($rowData as $key => $field) { - if ($header) { - $color = $this->getHeaderColor(); - } else { - $color = $this->fields[$key]['color']; - } + $splitLines = []; + $maxLines = 1; + foreach ($rowData as $key => $line) { + $splitLines[$key] = explode("\n", $line); + $maxLines = max($maxLines, count($splitLines[$key])); + } - $c = chr(27); - $fieldLength = mb_strwidth(preg_replace("/({$c}\[(.*?)m)/", '', $field)) + 1; - $field = ' '.($this->getUseColors() ? $this->getColorFromName($color) : '').$field; - $response .= $field; + for ($i = 0; $i < $maxLines; $i++) { + $response .= $this->getChar('left'); - for ($x = $fieldLength; $x < ($columnLengths[$key] + 2); $x++) { - $response .= ' '; + foreach ($splitLines as $key => $lines) { + if ($header) { + $color = $this->getHeaderColor(); + } else { + $color = $this->fields[$key]['color']; + } + + $line = isset($lines[$i]) ? $lines[$i] : ''; + + $c = chr(27); + $lineLength = mb_strwidth(preg_replace("/({$c}\[(.*?)m)/", '', $line)) + 1; + $line = ' ' . ($this->getUseColors() ? $this->getColorFromName($color) : '') . $line; + $response .= $line; + + for ($x = $lineLength; $x < ($columnLengths[$key] + 2); $x++) { + $response .= ' '; + } + $response .= $this->getChar('middle'); } - $response .= $this->getChar('middle'); + $response = substr($response, 0, strlen($response) - 3) . $this->getChar('right') . PHP_EOL; } - $response = substr($response, 0, strlen($response) - 3) . $this->getChar('right') . PHP_EOL; return $response; } diff --git a/tests/data.php b/tests/data.php index 5bd538d..04953ad 100644 --- a/tests/data.php +++ b/tests/data.php @@ -5,6 +5,7 @@ array( 'firstName' => 'Jamie', 'lastName' => 'Curnow', + 'hobbies' => 'Creative writing', 'dobTime' => 390492000, 'isAdmin' => true, 'lastSeenTime' => time() - rand(500, 1500), @@ -13,6 +14,7 @@ array( 'firstName' => 'Kyle', 'lastName' => 'Shinnings', + 'hobbies' => '', 'dobTime' => 383061600, 'isAdmin' => true, 'lastSeenTime' => time() - rand(500, 1500), @@ -21,6 +23,7 @@ array( 'firstName' => 'Samantha', 'lastName' => 'Collerson', + 'hobbies' => "Bouldering\nPhotography\nChess", 'dobTime' => 339256800, 'isAdmin' => false, 'lastSeenTime' => time() - rand(500, 1500), @@ -29,6 +32,7 @@ array( 'firstName' => 'Michelle', 'lastName' => 'Kringle', + 'hobbies' => '', 'dobTime' => 92844000, 'isAdmin' => false, 'lastSeenTime' => time() - rand(500, 1500), @@ -37,6 +41,7 @@ array( 'firstName' => 'Timothy', 'lastName' => 'Samuels', + 'hobbies' => '', 'dobTime' => 487778400, 'isAdmin' => false, 'lastSeenTime' => time() - rand(500, 1500), diff --git a/tests/test1.php b/tests/test1.php index 87cbb4b..a06aea0 100644 --- a/tests/test1.php +++ b/tests/test1.php @@ -14,6 +14,7 @@ $table->setHeaderColor('cyan'); $table->addField('First Name', 'firstName', false, 'white'); $table->addField('Last Name', 'lastName', false, 'white'); +$table->addField('Hobbies', 'hobbies'); $table->addField('DOB', 'dobTime', new CliTableManipulator('datelong')); $table->addField('Admin', 'isAdmin', new CliTableManipulator('yesno'), 'yellow'); $table->addField('Last Seen', 'lastSeenTime', new CliTableManipulator('nicetime'), 'red'); diff --git a/tests/test2.php b/tests/test2.php index c4dc0a4..948380c 100644 --- a/tests/test2.php +++ b/tests/test2.php @@ -32,6 +32,7 @@ $table->setHeaderColor('yellow'); $table->addField('First Name', 'firstName', false, 'cyan'); $table->addField('Last Name', 'lastName', false, 'cyan'); +$table->addField('Hobbies', 'hobbies'); $table->addField('DOB', 'dobTime', new CliTableManipulator('datelong')); $table->addField('Admin', 'isAdmin', new CliTableManipulator('yesno'), 'yellow'); $table->addField('Last Seen', 'lastSeenTime', new CliTableManipulator('nicetime'), 'red');