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

Replace tabs in tables with 4 spaces #181

Merged
merged 4 commits into from
Mar 3, 2025
Merged

Replace tabs in tables with 4 spaces #181

merged 4 commits into from
Mar 3, 2025

Conversation

swissspidy
Copy link
Member

While testing #179, I realized that tabs (\t) have always slightly messed up table formatting due to tab length.

By replacing them with spaces, the length calculation is correct again.

@swissspidy swissspidy requested a review from a team as a code owner March 3, 2025 14:49
@swissspidy swissspidy added this to the 0.12.2 milestone Mar 3, 2025
@mrsdizzie
Copy link
Member

I tested this but I don't think it works properly because of the existing logic. Right now it only directly modifies $row to use $value if this condition is met :

if ( $col_width && ( $original_val_width > $col_width || strpos( $value, "\n" ) !== false ) ) {

But if it isn't, then it just uses the original $row data to calculate padding:

$row = array_map(array($this, 'padColumn'), $row, array_keys($row));
array_unshift($row, ''); // First border
array_push($row, ''); // Last border
$ret = join($this->_characters['border'], $row);

So it doesn't know about the tab replacement always.

I saw this in two ways when testing with the value apple\tbanana\tmango

When I first added it, output looked like this:

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

Which seemed odd. Then when I added a longer string as another value, the table gets broken by the tab row again:

+---------+----------+------------------------------------------------------------------------+
| post_id | meta_key | meta_value                                                             |
+---------+----------+------------------------------------------------------------------------+
| 1       | fruits   | apple                                                                  |
|         |          | banana                                                                 |
|         |          | mango                                                                  |
| 1       | foo      | bar                                                                    |
| 1       | fruitz   | apple	banana	mango                                                     |
| 1       | fruitzz  | this is a really really really really really really really long string |
+---------+----------+------------------------------------------------------------------------+

Maybe a better place for this fix would be to modify $content here:

private function padColumn($content, $column) {
return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ) ) . $this->_characters['padding'];
}

To make sure it always removes tabs from the content no matter what

@swissspidy
Copy link
Member Author

Yes that makes more sense, thanks!

@swissspidy swissspidy merged commit 8063b4d into master Mar 3, 2025
17 checks passed
@swissspidy swissspidy deleted the replace-tabs branch March 3, 2025 16:22
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