Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
mrsdizzie committed Mar 4, 2025
commit 0171e03abd97fa6cd7905353493b8ca106a3b082
27 changes: 25 additions & 2 deletions lib/cli/table/Tabular.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,30 @@ class Tabular extends Renderer {
* @param array $row The table row.
* @return string The formatted table row.
*/
public function row(array $row) {
return implode("\t", array_values($row));
public function row( array $row ) {
$rows = [];
$output = '';

foreach ( $row as $col => $value ) {
$value = str_replace( "\t", ' ', $value );
$split_lines = preg_split( '/\r\n|\n/', $value );
// Keep anything before the first line break on the original line
$row[ $col ] = array_shift( $split_lines );
}

$rows[] = $row;

foreach ( $split_lines as $i => $line ) {
if ( ! isset( $rows[ $i + 1 ] ) ) {
$rows[ $i + 1 ] = array_fill_keys( array_keys( $row ), '' );
}
$rows[ $i + 1 ][ $col ] = $line;
}

foreach ( $rows as $r ) {
$output .= implode( "\t", array_values( $r ) ) . PHP_EOL;
}

return trim( $output );
}
}