Skip to content
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

Properly handle line breaks in column value #179

Merged
merged 1 commit into from
Mar 3, 2025
Merged

Conversation

mrsdizzie
Copy link
Member

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

Example of new fixed output:

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               |
+-----------------------+-----------------------------------------+

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
@mrsdizzie mrsdizzie requested a review from a team as a code owner March 3, 2025 13:44
@mrsdizzie mrsdizzie added the bug label Mar 3, 2025
@mrsdizzie mrsdizzie added this to the 0.12.2 milestone Mar 3, 2025
@swissspidy
Copy link
Member

Thanks @mrsdizzie!

This looks good!

While testing, I realized that tabs (\t) have always slightly messed up table formatting due to tab length. What do you think about replacing tabs in tables with e.g. 4 spaces? Could be as simple as $value = str_replace( "\t", ' ', $value );

@swissspidy swissspidy merged commit 62f1f00 into master Mar 3, 2025
17 checks passed
@swissspidy swissspidy deleted the linebreak-in-rows branch March 3, 2025 14:47
mrsdizzie added a commit that referenced this pull request Mar 4, 2025
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.
swissspidy pushed a commit that referenced this pull request Mar 4, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants