Skip to content

Commit

Permalink
Merge pull request jc21#8 from biggianteye/multi-line-support
Browse files Browse the repository at this point in the history
Multiline support
  • Loading branch information
jc21 authored Oct 8, 2023
2 parents 0398162 + 3922386 commit 6de41c3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions docs/Documentation.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## Table of contents
# Table of contents

- [\jc21\CliTable](#class-jc21clitable)
- [\jc21\CliTableManipulator](#class-jc21clitablemanipulator)

<hr />
### Class: \jc21\CliTable

## Class: \jc21\CliTable

| Visibility | Function |
|:-----------|:---------|
Expand Down Expand Up @@ -34,7 +35,8 @@
| protected | <strong>getTableTop(</strong><em>array</em> <strong>$columnLengths</strong>)</strong> : <em>string</em><br /><em>getTableTop</em> |

<hr />
### Class: \jc21\CliTableManipulator

## Class: \jc21\CliTableManipulator

| Visibility | Function |
|:-----------|:---------|
Expand Down
Binary file modified example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 32 additions & 16 deletions src/jc21/CliTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
array(
'firstName' => 'Jamie',
'lastName' => 'Curnow',
'hobbies' => 'Creative writing',
'dobTime' => 390492000,
'isAdmin' => true,
'lastSeenTime' => time() - rand(500, 1500),
Expand All @@ -13,6 +14,7 @@
array(
'firstName' => 'Kyle',
'lastName' => 'Shinnings',
'hobbies' => '',
'dobTime' => 383061600,
'isAdmin' => true,
'lastSeenTime' => time() - rand(500, 1500),
Expand All @@ -21,6 +23,7 @@
array(
'firstName' => 'Samantha',
'lastName' => 'Collerson',
'hobbies' => "Bouldering\nPhotography\nChess",
'dobTime' => 339256800,
'isAdmin' => false,
'lastSeenTime' => time() - rand(500, 1500),
Expand All @@ -29,6 +32,7 @@
array(
'firstName' => 'Michelle',
'lastName' => 'Kringle',
'hobbies' => '',
'dobTime' => 92844000,
'isAdmin' => false,
'lastSeenTime' => time() - rand(500, 1500),
Expand All @@ -37,6 +41,7 @@
array(
'firstName' => 'Timothy',
'lastName' => 'Samuels',
'hobbies' => '',
'dobTime' => 487778400,
'isAdmin' => false,
'lastSeenTime' => time() - rand(500, 1500),
Expand Down
1 change: 1 addition & 0 deletions tests/test1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions tests/test2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 6de41c3

Please sign in to comment.