Skip to content

Commit 0171e03

Browse files
committed
Support line breaks and tab replacement in tabular table values
This adds the same functionality from both #179 and #181 to the tabular table output. In WP CLI behat tests, the 'table containing rows' check can only use tabular output so we need to fix it here in order for tests to work.
1 parent 8063b4d commit 0171e03

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/cli/table/Tabular.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,30 @@ class Tabular extends Renderer {
2222
* @param array $row The table row.
2323
* @return string The formatted table row.
2424
*/
25-
public function row(array $row) {
26-
return implode("\t", array_values($row));
25+
public function row( array $row ) {
26+
$rows = [];
27+
$output = '';
28+
29+
foreach ( $row as $col => $value ) {
30+
$value = str_replace( "\t", ' ', $value );
31+
$split_lines = preg_split( '/\r\n|\n/', $value );
32+
// Keep anything before the first line break on the original line
33+
$row[ $col ] = array_shift( $split_lines );
34+
}
35+
36+
$rows[] = $row;
37+
38+
foreach ( $split_lines as $i => $line ) {
39+
if ( ! isset( $rows[ $i + 1 ] ) ) {
40+
$rows[ $i + 1 ] = array_fill_keys( array_keys( $row ), '' );
41+
}
42+
$rows[ $i + 1 ][ $col ] = $line;
43+
}
44+
45+
foreach ( $rows as $r ) {
46+
$output .= implode( "\t", array_values( $r ) ) . PHP_EOL;
47+
}
48+
49+
return trim( $output );
2750
}
2851
}

0 commit comments

Comments
 (0)