Skip to content

Commit

Permalink
Properly handle line breaks in column value (#179)
Browse files Browse the repository at this point in the history
Before this code tried to replace newlines with empty spaces, but it
didn't always work because the new value would only use the replaced
string if additional wrapping needed to be done due to size constraints.
This updates the code to properly handle new lines in all content by
creating an extra row for each one, using the same logic that already
existed for wrapping longer content.

See wp-cli/entity-command#262 for an example
of the currently broken behavior that this PR fixes
  • Loading branch information
mrsdizzie authored Mar 3, 2025
1 parent d1fe500 commit 62f1f00
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions lib/cli/table/Ascii.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,32 @@ public function row( array $row ) {
if ( count( $row ) > 0 ) {
$extra_rows = array_fill( 0, count( $row ), array() );

foreach( $row as $col => $value ) {
$value = $value ?: '';
$value = str_replace( array( "\r\n", "\n" ), ' ', $value );

$col_width = $this->_widths[ $col ];
$encoding = function_exists( 'mb_detect_encoding' ) ? mb_detect_encoding( $value, null, true /*strict*/ ) : false;
foreach ( $row as $col => $value ) {
$value = $value ?: '';
$col_width = $this->_widths[ $col ];
$encoding = function_exists( 'mb_detect_encoding' ) ? mb_detect_encoding( $value, null, true /*strict*/ ) : false;
$original_val_width = Colors::width( $value, self::isPreColorized( $col ), $encoding );
if ( $col_width && $original_val_width > $col_width ) {
$row[ $col ] = \cli\safe_substr( $value, 0, $col_width, true /*is_width*/, $encoding );
$value = \cli\safe_substr( $value, \cli\safe_strlen( $row[ $col ], $encoding ), null /*length*/, false /*is_width*/, $encoding );
$i = 0;
do {
$extra_value = \cli\safe_substr( $value, 0, $col_width, true /*is_width*/, $encoding );
$val_width = Colors::width( $extra_value, self::isPreColorized( $col ), $encoding );
if ( $val_width ) {
$extra_rows[ $col ][] = $extra_value;
$value = \cli\safe_substr( $value, \cli\safe_strlen( $extra_value, $encoding ), null /*length*/, false /*is_width*/, $encoding );
$i++;
if ( $i > $extra_row_count ) {
$extra_row_count = $i;
if ( $col_width && ( $original_val_width > $col_width || strpos( $value, "\n" ) !== false ) ) {
$split_lines = preg_split( '/\r\n|\n/', $value );

$wrapped_lines = [];
foreach ( $split_lines as $line ) {
do {
$wrapped_value = \cli\safe_substr( $line, 0, $col_width, true /*is_width*/, $encoding );
$val_width = Colors::width( $wrapped_value, self::isPreColorized( $col ), $encoding );
if ( $val_width ) {
$wrapped_lines[] = $wrapped_value;
$line = \cli\safe_substr( $line, \cli\safe_strlen( $wrapped_value, $encoding ), null /*length*/, false /*is_width*/, $encoding );
}
}
} while( $value );
}
} while ( $line );
}

$row[ $col ] = array_shift( $wrapped_lines );
foreach ( $wrapped_lines as $wrapped_line ) {
$extra_rows[ $col ][] = $wrapped_line;
++$extra_row_count;
}
}
}
}

Expand Down

0 comments on commit 62f1f00

Please sign in to comment.