Skip to content

Commit 62f1f00

Browse files
authored
Properly handle line breaks in column value (#179)
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
1 parent d1fe500 commit 62f1f00

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

lib/cli/table/Ascii.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,32 @@ public function row( array $row ) {
136136
if ( count( $row ) > 0 ) {
137137
$extra_rows = array_fill( 0, count( $row ), array() );
138138

139-
foreach( $row as $col => $value ) {
140-
$value = $value ?: '';
141-
$value = str_replace( array( "\r\n", "\n" ), ' ', $value );
142-
143-
$col_width = $this->_widths[ $col ];
144-
$encoding = function_exists( 'mb_detect_encoding' ) ? mb_detect_encoding( $value, null, true /*strict*/ ) : false;
139+
foreach ( $row as $col => $value ) {
140+
$value = $value ?: '';
141+
$col_width = $this->_widths[ $col ];
142+
$encoding = function_exists( 'mb_detect_encoding' ) ? mb_detect_encoding( $value, null, true /*strict*/ ) : false;
145143
$original_val_width = Colors::width( $value, self::isPreColorized( $col ), $encoding );
146-
if ( $col_width && $original_val_width > $col_width ) {
147-
$row[ $col ] = \cli\safe_substr( $value, 0, $col_width, true /*is_width*/, $encoding );
148-
$value = \cli\safe_substr( $value, \cli\safe_strlen( $row[ $col ], $encoding ), null /*length*/, false /*is_width*/, $encoding );
149-
$i = 0;
150-
do {
151-
$extra_value = \cli\safe_substr( $value, 0, $col_width, true /*is_width*/, $encoding );
152-
$val_width = Colors::width( $extra_value, self::isPreColorized( $col ), $encoding );
153-
if ( $val_width ) {
154-
$extra_rows[ $col ][] = $extra_value;
155-
$value = \cli\safe_substr( $value, \cli\safe_strlen( $extra_value, $encoding ), null /*length*/, false /*is_width*/, $encoding );
156-
$i++;
157-
if ( $i > $extra_row_count ) {
158-
$extra_row_count = $i;
144+
if ( $col_width && ( $original_val_width > $col_width || strpos( $value, "\n" ) !== false ) ) {
145+
$split_lines = preg_split( '/\r\n|\n/', $value );
146+
147+
$wrapped_lines = [];
148+
foreach ( $split_lines as $line ) {
149+
do {
150+
$wrapped_value = \cli\safe_substr( $line, 0, $col_width, true /*is_width*/, $encoding );
151+
$val_width = Colors::width( $wrapped_value, self::isPreColorized( $col ), $encoding );
152+
if ( $val_width ) {
153+
$wrapped_lines[] = $wrapped_value;
154+
$line = \cli\safe_substr( $line, \cli\safe_strlen( $wrapped_value, $encoding ), null /*length*/, false /*is_width*/, $encoding );
159155
}
160-
}
161-
} while( $value );
162-
}
156+
} while ( $line );
157+
}
163158

159+
$row[ $col ] = array_shift( $wrapped_lines );
160+
foreach ( $wrapped_lines as $wrapped_line ) {
161+
$extra_rows[ $col ][] = $wrapped_line;
162+
++$extra_row_count;
163+
}
164+
}
164165
}
165166
}
166167

0 commit comments

Comments
 (0)