Skip to content

Support line breaks and tab replacement in tabular table values #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
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
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 );
}
}