Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.

Commit d75d532

Browse files
Merge pull request wp-cli#71 from bskrtich/master
Fix 2 small bugs related to passing in empty arrays to the table class
2 parents 5e9fbf1 + b2bd1bb commit d75d532

3 files changed

Lines changed: 46 additions & 26 deletions

File tree

lib/cli/Table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Table {
3333
*
3434
* There are 3 ways to instantiate this class:
3535
*
36-
* 1. Pass an array of strings as the first paramater for the column headers
36+
* 1. Pass an array of strings as the first parameter for the column headers
3737
* and a 2-dimensional array as the second parameter for the data rows.
3838
* 2. Pass an array of hash tables (string indexes instead of numerical)
3939
* where each hash table is a row and the indexes of the *first* hash
@@ -48,7 +48,7 @@ public function __construct(array $headers = null, array $rows = null, array $fo
4848
if (!empty($headers)) {
4949
// If all the rows is given in $headers we use the keys from the
5050
// first row for the header values
51-
if (empty($rows)) {
51+
if ($rows === null) {
5252
$rows = $headers;
5353
$keys = array_keys(array_shift($headers));
5454
$headers = array();

lib/cli/table/Ascii.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function setWidths(array $widths) {
7878
}
7979

8080
/**
81-
* Set the contraint width for the table
81+
* Set the constraint width for the table
8282
*
8383
* @param int $constraintWidth
8484
*/
@@ -123,32 +123,36 @@ public function border() {
123123
*/
124124
public function row( array $row ) {
125125

126-
$extra_rows = array_fill( 0, count( $row ), array() );
127126
$extra_row_count = 0;
128-
foreach( $row as $col => $value ) {
129-
130-
$value = str_replace( PHP_EOL, ' ', $value );
131-
132-
$col_width = $this->_widths[ $col ];
133-
$original_val_width = Colors::length( $value );
134-
if ( $original_val_width > $col_width ) {
135-
$row[ $col ] = \cli\safe_substr( $value, 0, $col_width );
136-
$value = \cli\safe_substr( $value, $col_width, $original_val_width );
137-
$i = 0;
138-
do {
139-
$extra_value = \cli\safe_substr( $value, 0, $col_width );
140-
$val_width = \cli\safe_strlen( $extra_value );
141-
if ( $val_width ) {
142-
$extra_rows[ $col ][] = $extra_value;
143-
$value = \cli\safe_substr( $value, $col_width, $original_val_width );
144-
$i++;
145-
if ( $i > $extra_row_count ) {
146-
$extra_row_count = $i;
127+
128+
if ( count( $row ) > 0 ) {
129+
$extra_rows = array_fill( 0, count( $row ), array() );
130+
131+
foreach( $row as $col => $value ) {
132+
133+
$value = str_replace( PHP_EOL, ' ', $value );
134+
135+
$col_width = $this->_widths[ $col ];
136+
$original_val_width = Colors::length( $value );
137+
if ( $original_val_width > $col_width ) {
138+
$row[ $col ] = \cli\safe_substr( $value, 0, $col_width );
139+
$value = \cli\safe_substr( $value, $col_width, $original_val_width );
140+
$i = 0;
141+
do {
142+
$extra_value = \cli\safe_substr( $value, 0, $col_width );
143+
$val_width = \cli\safe_strlen( $extra_value );
144+
if ( $val_width ) {
145+
$extra_rows[ $col ][] = $extra_value;
146+
$value = \cli\safe_substr( $value, $col_width, $original_val_width );
147+
$i++;
148+
if ( $i > $extra_row_count ) {
149+
$extra_row_count = $i;
150+
}
147151
}
148-
}
149-
} while( $value );
150-
}
152+
} while( $value );
153+
}
151154

155+
}
152156
}
153157

154158
$row = array_map(array($this, 'padColumn'), $row, array_keys($row));

tests/test-table-ascii.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ public function testDrawMultiplicationTable() {
134134
| 16 | 16 | 32 | 48 | 64 | 80 | 96 | 112 | 128 | 144 | 160 | 176 | 192 | 208 | 224 | 240 | 256 |
135135
+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
136136

137+
OUT;
138+
$this->assertInOutEquals(array($headers, $rows), $output);
139+
}
140+
141+
/**
142+
* Draw a table with headers but no data
143+
*/
144+
public function testDrawWithHeadersNoData() {
145+
$headers = array('header 1', 'header 2');
146+
$rows = array();
147+
$output = <<<'OUT'
148+
+----------+----------+
149+
| header 1 | header 2 |
150+
+----------+----------+
151+
+----------+----------+
152+
137153
OUT;
138154
$this->assertInOutEquals(array($headers, $rows), $output);
139155
}

0 commit comments

Comments
 (0)