Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add test for line breaks in table view#6055

Merged
mrsdizzie merged 11 commits into
wp-cli:mainwp-cli/wp-cli:mainfrom
BhargavBhandari90:line-break-improvementBhargavBhandari90/wp-cli:line-break-improvementCopy head branch name to clipboard
Mar 4, 2025
Merged

Add test for line breaks in table view#6055
mrsdizzie merged 11 commits into
wp-cli:mainwp-cli/wp-cli:mainfrom
BhargavBhandari90:line-break-improvementBhargavBhandari90/wp-cli:line-break-improvementCopy head branch name to clipboard

Conversation

@BhargavBhandari90

@BhargavBhandari90 BhargavBhandari90 commented Mar 1, 2025

Copy link
Copy Markdown
Contributor

Edit: This ended up being fixed in wp-cli/php-cli-tools#182 so now this PR just includes a behat test to verify that change works

Fixes: wp-cli/entity-command#262

Before:

Image

After I modified Formatter.php

Image

This is I am marking as WIP as I need some help to improve this.
I've addes some static keys ( post_id 317 , meta_key 318 , meta_value 319 ) for the array manipulation in the loop.
This should be coming from $fields I guess.

@codecov-commenter

codecov-commenter commented Mar 1, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@swissspidy

Copy link
Copy Markdown
Member

Thanks for your contribution, this looks like a great start!

Yes, the fields need to be handled dynamically and not hardcoded, but then it should work nicely. So basically whenever there's one or more fields with a newline, all other fields without one need a blank line added.

Tests could be added to features/formatter.feature

@BhargavBhandari90

Copy link
Copy Markdown
Contributor Author

Thank you @swissspidy for your feedback. I'll improve the code.

Tests could be added to features/formatter.feature

  • Noted

@BhargavBhandari90 BhargavBhandari90 changed the title WIP: Fix line break issue for table view Fix line break issue for table view Mar 2, 2025
@BhargavBhandari90

BhargavBhandari90 commented Mar 2, 2025

Copy link
Copy Markdown
Contributor Author

Hi @swissspidy

I've tried to improve code and added a test.
I see 3 failing checks in github which I am not sure. Is it something wrong in my code?

Aslo, I think this line break thing currently we should limit to meta list commands only. I see there are lot of listing command we have and covering all of them needs proper observations.

Let me know what you think.

@swissspidy

swissspidy commented Mar 2, 2025

Copy link
Copy Markdown
Member

Thanks for adding the test, that's very useful for further iterations. The 3 remaining failing ones are currently known and can be ignored.

I understand your concern, but the formatter code needs to be command-agnostic and should not have any hard coded field references. I think that should be possible. Let me know if you'd like any help in making that last step happen.

@mrsdizzie

Copy link
Copy Markdown
Member

Wouldn't this need to be fixed in the function that generates table rows so that it does work for different types of output:

https://github.com/wp-cli/php-cli-tools/blob/d1fe500378f53fb5ae1072c0daa77095c384a082/lib/cli/table/Ascii.php#L126-L132

(I attempted a quick fix, but I wasn't familiar enough with the code to get it to work 100% when the table wraps because of a size constraint). I think the entire logic would need to be reworked since right now it attempts to remove newlines rather than understand them properly as additional rows.

@mrsdizzie

Copy link
Copy Markdown
Member

OK actually I think this might do it:

diff --git a/lib/cli/table/Ascii.php b/lib/cli/table/Ascii.php
index ff5c3d9..b2505e6 100644
--- a/lib/cli/table/Ascii.php
+++ b/lib/cli/table/Ascii.php
@@ -136,31 +136,32 @@ class Ascii extends Renderer {
 		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;
+					}
+				}
 			}
 		}
 
isla@Islas-MacBook-Pro ~/test $ wp post meta list 1
+---------+----------+--------------------+
| post_id | meta_key | meta_value         |
+---------+----------+--------------------+
| 1       | fruits   | apple              |
|         |          | banana             |
|         |          | mango              |
| 1       | foo      | bar                |
+---------+----------+--------------------+

isla@Islas-MacBook-Pro ~/test $ wp post get 1
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Field                 | Value                                                                                                                                     |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| ID                    | 1                                                                                                                                         |
| post_author           | 1                                                                                                                                         |
| post_date             | 2024-12-05 18:11:32                                                                                                                       |
| post_date_gmt         | 2024-12-05 18:11:32                                                                                                                       |
| post_content          | <!-- wp:paragraph -->                                                                                                                     |
|                       | <p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>                                              |
|                       | <!-- /wp:paragraph -->                                                                                                                    |
| post_title            | Hello world!                                                                                                                              |
| post_excerpt          |                                                                                                                                           |
| post_status           | publish                                                                                                                                   |
| comment_status        | open                                                                                                                                      |
| ping_status           | open                                                                                                                                      |
| post_password         |                                                                                                                                           |
| post_name             | hello-world                                                                                                                               |
| to_ping               |                                                                                                                                           |
| pinged                |                                                                                                                                           |
| post_modified         | 2024-12-05 18:11:32                                                                                                                       |
| post_modified_gmt     | 2024-12-05 18:11:32                                                                                                                       |
| post_content_filtered |                                                                                                                                           |
| post_parent           | 0                                                                                                                                         |
| guid                  | http://macbook.local/?p=1                                                                                                                 |
| menu_order            | 0                                                                                                                                         |
| post_type             | post                                                                                                                                      |
| post_mime_type        |                                                                                                                                           |
| comment_count         | 1                                                                                                                                         |
| url                   | http://macbook.local/?p=1                                                                                                                 |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------+


# Smaller term
isla@Islas-MacBook-Pro ~/test $ wp post get 1
+-----------------------+-----------------------------------------+
| Field                 | Value                                   |
+-----------------------+-----------------------------------------+
| ID                    | 1                                       |
| post_author           | 1                                       |
| post_date             | 2024-12-05 18:11:32                     |
| post_date_gmt         | 2024-12-05 18:11:32                     |
| post_content          | <!-- wp:paragraph -->                   |
|                       | <p>Welcome to WordPress. This is your f |
|                       | irst post. Edit or delete it, then star |
|                       | t writing!</p>                          |
|                       | <!-- /wp:paragraph -->                  |
| post_title            | Hello world!                            |
| post_excerpt          |                                         |
| post_status           | publish                                 |
| comment_status        | open                                    |
| ping_status           | open                                    |
| post_password         |                                         |
| post_name             | hello-world                             |
| to_ping               |                                         |
| pinged                |                                         |
| post_modified         | 2024-12-05 18:11:32                     |
| post_modified_gmt     | 2024-12-05 18:11:32                     |
| post_content_filtered |                                         |
| post_parent           | 0                                       |
| guid                  | http://macbook.local/?p=1               |
| menu_order            | 0                                       |
| post_type             | post                                    |
| post_mime_type        |                                         |
| comment_count         | 1                                       |
| url                   | http://macbook.local/?p=1               |
+-----------------------+-----------------------------------------+

What do you think about that? the row function already had logic for appending additional rows but it was only used for wrapping based on width. Now it takes newlines into account as well (phpunit tests still pass too)

@BhargavBhandari90

Copy link
Copy Markdown
Contributor Author

Wow @mrsdizzie This looks great. Lets see what @swissspidy say about this.

@swissspidy

Copy link
Copy Markdown
Member

Let‘s ship it! Thanks @mrsdizzie!

@mrsdizzie

Copy link
Copy Markdown
Member

OK I've created wp-cli/php-cli-tools#179 for this. Maybe we can keep this PR open for now and still merge the new test afterwards, since that seems useful and there aren't behat tests for php-cli-tools

@swissspidy

Copy link
Copy Markdown
Member

Hmm now the output in the test is like this:

+---------+----------+--------------------+
| post_id | meta_key | meta_value         |
+---------+----------+--------------------+
| 1       | foo      | foo                |
| 1       | fruits   | apple
banana
mango |
| 1       | bar      | br                 |
+---------+----------+--------------------+

@mrsdizzie what am I missing?

@mrsdizzie

mrsdizzie commented Mar 3, 2025

Copy link
Copy Markdown
Member

This must have something to do with there being no tty and it behaving differently -- you can see similar behavior locally:

isla@Islas-MacBook-Pro ~/test $ wp eval-file test.php --skip-wordpress
   +---------+----------+--------------------+
| post_id | meta_key | meta_value         |
+---------+----------+--------------------+
| 1       | foo      | foo                |
| 1       | fruits   | apple              |
|         |          | banana             |
|         |          | mango              |
| 1       | bar      | br                 |
+---------+----------+--------------------+

isla@Islas-MacBook-Pro ~/test $ wp eval-file test.php --skip-wordpress | cat
   post_id	meta_key	meta_value
1	foo	foo
1	fruits	apple
banana
mango
1	bar	br

Maybe there is a different code path for no tty that the earlier changes don't run on? Or maybe it is collapsing empty columns when no tty? will have to investigate that 🕵️‍♀️

Edit:
Yes, in this case it uses this separate implementation of row that wouldn't have either of our recent changes:
https://github.com/wp-cli/php-cli-tools/blob/8063b4da01942d286efaab29a1e9da764e7a8438/lib/cli/table/Tabular.php#L25-L27

All it does is turn a row array into a string spaced by tabs. I guess that one needs to be changed as well. Or alternatively we can use SHELL_PIPE=0 in the test to force the ascii table, which I see we do in several other tests across projects

@mrsdizzie mrsdizzie changed the title Fix line break issue for table view Add test for line breaks in table view Mar 4, 2025
@mrsdizzie mrsdizzie added this to the 2.12.0 milestone Mar 4, 2025

@mrsdizzie mrsdizzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing to get this fixed and including the test

@mrsdizzie mrsdizzie merged commit b077aed into wp-cli:main Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line breaks in meta values break wp post meta list

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.