From 0171e03abd97fa6cd7905353493b8ca106a3b082 Mon Sep 17 00:00:00 2001 From: Isla Waters Date: Tue, 4 Mar 2025 10:14:18 -0500 Subject: [PATCH] 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. --- lib/cli/table/Tabular.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/cli/table/Tabular.php b/lib/cli/table/Tabular.php index 6e7c502..c7c2a1f 100644 --- a/lib/cli/table/Tabular.php +++ b/lib/cli/table/Tabular.php @@ -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 ); } }