Skip to content

Commit fbb9c6e

Browse files
authored
Fix removal of trailing tab / whitespace in tabular table (#184)
Previously the final table output was trimmed with the intention of removing the last newline. This had an unintended side effect of removing the tab characters from empty columns in the specific case where the last row had one or more empty columns at the end of the row. This makes sure we are only removing the newline character as intended. Add a new unit test to verify this behavior as well.
1 parent 1556134 commit fbb9c6e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/cli/table/Tabular.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function row( array $row ) {
4545
foreach ( $rows as $r ) {
4646
$output .= implode( "\t", array_values( $r ) ) . PHP_EOL;
4747
}
48-
49-
return trim( $output );
48+
return rtrim( $output, PHP_EOL );
5049
}
5150
}

tests/Test_Table.php

+22
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,26 @@ public function test_ascii_pre_colorized_widths() {
245245
$this->assertSame( 56, strlen( $out[6] ) );
246246
}
247247

248+
public function test_preserve_trailing_tabs() {
249+
$table = new cli\Table();
250+
$renderer = new cli\Table\Tabular();
251+
$table->setRenderer( $renderer );
252+
253+
$table->setHeaders( array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' ) );
254+
255+
// Add row with missing values at the end
256+
$table->addRow( array( 'date', 'date', 'NO', 'PRI', '', '' ) );
257+
$table->addRow( array( 'awesome_stuff', 'text', 'YES', '', '', '' ) );
258+
259+
$out = $table->getDisplayLines();
260+
261+
$expected = [
262+
"Field\tType\tNull\tKey\tDefault\tExtra",
263+
"date\tdate\tNO\tPRI\t\t",
264+
"awesome_stuff\ttext\tYES\t\t\t",
265+
];
266+
267+
$this->assertSame( $expected, $out, 'Trailing tabs should be preserved in table output.' );
268+
}
269+
248270
}

0 commit comments

Comments
 (0)