-
Notifications
You must be signed in to change notification settings - Fork 115
Add column alignment support for tables #192
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
Merged
+311
−19
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
110db79
Initial plan
Copilot dfd6b61
Add table column alignment feature
Copilot df01866
Add table alignment example demonstrating the feature
Copilot 69bd29f
Optimize alignment lookup and validation
Copilot e842497
Update lib/cli/Table.php
swissspidy cb48e85
Cache valid alignments map to avoid recreating on every call
Copilot c3ff813
Remove extra blank line for consistent spacing
Copilot 22c6f67
Merge branch 'main' into copilot/enhance-table-format
swissspidy d34de56
Fix merge hiccup
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
| /** | ||
| * Table Alignment Examples | ||
| * | ||
| * This example demonstrates the table column alignment feature. | ||
| * You can align columns to the left, right, or center. | ||
| */ | ||
|
|
||
| if (file_exists(__DIR__ . '/../vendor/autoload.php')) { | ||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
| } elseif (file_exists(__DIR__ . '/../../../autoload.php')) { | ||
| require_once __DIR__ . '/../../../autoload.php'; | ||
| } else { | ||
| throw new Exception('Unable to locate autoloader; please run "composer install"'); | ||
| } | ||
|
|
||
| cli\line(); | ||
| cli\line('%G===%n %CTable Column Alignment Examples%n %G===%n'); | ||
| cli\line(); | ||
|
|
||
| // Example 1: Default (Left) Alignment | ||
| cli\line('%Y## Example 1: Default Left Alignment%n'); | ||
| cli\line(); | ||
| $table = new cli\Table(); | ||
| $table->setHeaders(['Product', 'Price', 'Stock']); | ||
| $table->addRow(['Widget', '$19.99', '150']); | ||
| $table->addRow(['Gadget', '$29.99', '75']); | ||
| $table->addRow(['Tool', '$9.99', '200']); | ||
| $table->display(); | ||
| cli\line(); | ||
|
|
||
| // Example 2: Right Alignment for Numeric Columns | ||
| cli\line('%Y## Example 2: Right Alignment for Numeric Columns%n'); | ||
| cli\line('Notice how the numeric values are much easier to compare when right-aligned.'); | ||
| cli\line(); | ||
| $table = new cli\Table(); | ||
| $table->setHeaders(['Product', 'Price', 'Stock']); | ||
| $table->setAlignments([ | ||
| 'Product' => cli\table\Column::ALIGN_LEFT, | ||
| 'Price' => cli\table\Column::ALIGN_RIGHT, | ||
| 'Stock' => cli\table\Column::ALIGN_RIGHT, | ||
| ]); | ||
| $table->addRow(['Widget', '$19.99', '150']); | ||
| $table->addRow(['Gadget', '$29.99', '75']); | ||
| $table->addRow(['Tool', '$9.99', '200']); | ||
| $table->display(); | ||
| cli\line(); | ||
|
|
||
| // Example 3: Center Alignment | ||
| cli\line('%Y## Example 3: Center Alignment%n'); | ||
| cli\line(); | ||
| $table = new cli\Table(); | ||
| $table->setHeaders(['Left', 'Center', 'Right']); | ||
| $table->setAlignments([ | ||
| 'Left' => cli\table\Column::ALIGN_LEFT, | ||
| 'Center' => cli\table\Column::ALIGN_CENTER, | ||
| 'Right' => cli\table\Column::ALIGN_RIGHT, | ||
| ]); | ||
| $table->addRow(['Text', 'Centered', 'More']); | ||
| $table->addRow(['Data', 'Values', 'Here']); | ||
| $table->addRow(['A', 'B', 'C']); | ||
| $table->display(); | ||
| cli\line(); | ||
|
|
||
| // Example 4: Real-world Database Table Sizes | ||
| cli\line('%Y## Example 4: Database Table Sizes (Real-world Use Case)%n'); | ||
| cli\line('This example shows how the alignment feature makes database'); | ||
| cli\line('statistics much more readable and easier to compare.'); | ||
| cli\line(); | ||
| $table = new cli\Table(); | ||
| $table->setHeaders(['Table Name', 'Rows', 'Data Size', 'Index Size']); | ||
| $table->setAlignments([ | ||
| 'Table Name' => cli\table\Column::ALIGN_LEFT, | ||
| 'Rows' => cli\table\Column::ALIGN_RIGHT, | ||
| 'Data Size' => cli\table\Column::ALIGN_RIGHT, | ||
| 'Index Size' => cli\table\Column::ALIGN_RIGHT, | ||
| ]); | ||
| $table->addRow(['wp_posts', '1,234', '5.2 MB', '1.1 MB']); | ||
| $table->addRow(['wp_postmeta', '45,678', '23.4 MB', '8.7 MB']); | ||
| $table->addRow(['wp_comments', '9,012', '2.3 MB', '0.8 MB']); | ||
| $table->addRow(['wp_options', '456', '1.5 MB', '0.2 MB']); | ||
| $table->addRow(['wp_users', '89', '0.1 MB', '0.05 MB']); | ||
| $table->display(); | ||
| cli\line(); | ||
|
|
||
| // Example 5: Alignment Constants | ||
| cli\line('%Y## Alignment Constants%n'); | ||
| cli\line(); | ||
| cli\line('You can use the following constants from %Ccli\table\Column%n:'); | ||
| cli\line(' %G*%n %CALIGN_LEFT%n - Left align (default)'); | ||
| cli\line(' %G*%n %CALIGN_RIGHT%n - Right align (good for numbers)'); | ||
| cli\line(' %G*%n %CALIGN_CENTER%n - Center align'); | ||
| cli\line(); | ||
| cli\line('Example usage:'); | ||
| cli\line(' %c$table->setAlignments([%n'); | ||
| cli\line(' %c\'Column1\' => cli\table\Column::ALIGN_LEFT,%n'); | ||
| cli\line(' %c\'Column2\' => cli\table\Column::ALIGN_RIGHT,%n'); | ||
| cli\line(' %c]);%n'); | ||
| cli\line(); | ||
|
|
||
| cli\line('%GDone!%n'); | ||
| cli\line(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
| /** | ||
| * PHP Command Line Tools | ||
| * | ||
| * This source file is subject to the MIT license that is bundled | ||
| * with this package in the file LICENSE. | ||
| * | ||
| * @author James Logsdon <dwarf@girsbrain.org> | ||
| * @copyright 2010 James Logsdom (http://girsbrain.org) | ||
| * @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
| */ | ||
|
|
||
| namespace cli\table; | ||
|
|
||
| /** | ||
| * Column alignment constants for table rendering. | ||
| */ | ||
| interface Column { | ||
| const ALIGN_LEFT = STR_PAD_RIGHT; | ||
| const ALIGN_RIGHT = STR_PAD_LEFT; | ||
| const ALIGN_CENTER = STR_PAD_BOTH; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.