Skip to content

Add alignment functionality to tables#171

Closed
rolf-yoast wants to merge 10 commits into
wp-cli:mainfrom
rolf-yoast:add-alignments
Closed

Add alignment functionality to tables#171
rolf-yoast wants to merge 10 commits into
wp-cli:mainfrom
rolf-yoast:add-alignments

Conversation

@rolf-yoast

@rolf-yoast rolf-yoast commented Apr 19, 2024

Copy link
Copy Markdown

Fixes #239

@rolf-yoast rolf-yoast requested a review from a team as a code owner April 19, 2024 06:23
@rolf-yoast rolf-yoast changed the title Introduce Column interface for the right alignment names Add alignment functionality to tables Apr 19, 2024

@danielbachhuber danielbachhuber 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, @rolf-yoast !

Could you add some tests for this PR?

Also, it would be nice to address your TODO comment.

@swissspidy swissspidy requested a review from Copilot November 2, 2025 14:07
@codecov

codecov Bot commented Nov 2, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.19048% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lib/cli/Table.php 58.33% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for column alignment in CLI tables. The implementation allows users to specify left, right, or center alignment for table columns using alignment constants defined in a new Column interface.

Key changes:

  • Introduced a new Column interface with alignment constants mapped to PHP's STR_PAD_* constants
  • Added alignment tracking and configuration throughout the table rendering pipeline
  • Modified the Ascii renderer to apply column-specific alignments when padding cell content

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
lib/cli/table/Column.php Defines new interface with alignment constants (ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER)
lib/cli/table/Renderer.php Adds $_alignments property and setAlignments() method to base renderer class
lib/cli/table/Ascii.php Implements alignment logic by storing headers and applying column-specific alignment in padColumn()
lib/cli/Table.php Adds alignment parameter to constructor and propagates alignments to renderer

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/cli/table/Ascii.php Outdated
Comment thread lib/cli/table/Ascii.php Outdated
Comment thread lib/cli/Table.php Outdated
Comment thread lib/cli/Table.php Outdated
Comment thread lib/cli/Table.php Outdated
swissspidy and others added 6 commits November 2, 2025 15:21
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy swissspidy requested a review from Copilot November 2, 2025 16:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/cli/Table.php
Comment on lines +216 to +227
public function setAlignments(array $alignments) {
$validAlignments = array(Column::ALIGN_LEFT, Column::ALIGN_RIGHT, Column::ALIGN_CENTER);
foreach ($alignments as $column => $alignment) {
if (!in_array($alignment, $validAlignments, true)) {
throw new \InvalidArgumentException("Invalid alignment value '$alignment' for column '$column'.");
}
if (!in_array($column, $this->_headers, true)) {
throw new \InvalidArgumentException("Column '$column' does not exist in table headers.");
}
}
$this->_alignments = $alignments;
}

Copilot AI Nov 2, 2025

Copy link

Choose a reason for hiding this comment

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

The validation on line 222 checks if headers exist before alignments are set, but setAlignments() can be called in the constructor at line 68 before setHeaders() is called at line 64 when the constructor is invoked with an empty headers array and a non-empty alignments array. This will cause the validation to always fail in that case because $this->_headers will be empty. Consider either validating alignments lazily when they're used, or reordering the constructor calls, or skipping validation when headers are empty.

Copilot uses AI. Check for mistakes.
Comment thread lib/cli/table/Ascii.php
Comment on lines +135 to +137
if ($this->_headers === null) {
$this->_headers = array_values($row);
}

Copilot AI Nov 2, 2025

Copy link

Choose a reason for hiding this comment

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

The header tracking logic assumes the first row rendered is always the headers, but this may not be reliable if the renderer is reused or if rows are rendered in an unexpected order. Consider passing headers explicitly to the renderer via a dedicated method (e.g., setHeaders()) rather than inferring them from the first row call to row().

Copilot uses AI. Check for mistakes.
Comment thread lib/cli/table/Ascii.php Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Jan 20, 2026
- Create Column interface with ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER constants
- Update Renderer base class to support alignments and headers
- Update Ascii renderer to apply alignment when padding columns
- Update Table class to support alignment in constructor and setAlignments()
- Add comprehensive tests for all alignment types
- Address PR #171 feedback: headers passed explicitly to renderer, validation allows setting alignments before headers

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy

Copy link
Copy Markdown
Member

Opened #192 to bring this over the finish line. Thanks for your contribution!

@swissspidy swissspidy closed this Jan 20, 2026
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.

Is it possible to enhance table format with right-align numeric values easily?

4 participants