Skip to content

Commit 5cc6ef2

Browse files
CopilotswissspidyCopilot
authored
Add column alignment support for tables (#192)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent f3963aa commit 5cc6ef2

File tree

6 files changed

+311
-19
lines changed

6 files changed

+311
-19
lines changed

examples/table-alignment.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Table Alignment Examples
5+
*
6+
* This example demonstrates the table column alignment feature.
7+
* You can align columns to the left, right, or center.
8+
*/
9+
10+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
11+
require_once __DIR__ . '/../vendor/autoload.php';
12+
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
13+
require_once __DIR__ . '/../../../autoload.php';
14+
} else {
15+
throw new Exception('Unable to locate autoloader; please run "composer install"');
16+
}
17+
18+
cli\line();
19+
cli\line('%G===%n %CTable Column Alignment Examples%n %G===%n');
20+
cli\line();
21+
22+
// Example 1: Default (Left) Alignment
23+
cli\line('%Y## Example 1: Default Left Alignment%n');
24+
cli\line();
25+
$table = new cli\Table();
26+
$table->setHeaders(['Product', 'Price', 'Stock']);
27+
$table->addRow(['Widget', '$19.99', '150']);
28+
$table->addRow(['Gadget', '$29.99', '75']);
29+
$table->addRow(['Tool', '$9.99', '200']);
30+
$table->display();
31+
cli\line();
32+
33+
// Example 2: Right Alignment for Numeric Columns
34+
cli\line('%Y## Example 2: Right Alignment for Numeric Columns%n');
35+
cli\line('Notice how the numeric values are much easier to compare when right-aligned.');
36+
cli\line();
37+
$table = new cli\Table();
38+
$table->setHeaders(['Product', 'Price', 'Stock']);
39+
$table->setAlignments([
40+
'Product' => cli\table\Column::ALIGN_LEFT,
41+
'Price' => cli\table\Column::ALIGN_RIGHT,
42+
'Stock' => cli\table\Column::ALIGN_RIGHT,
43+
]);
44+
$table->addRow(['Widget', '$19.99', '150']);
45+
$table->addRow(['Gadget', '$29.99', '75']);
46+
$table->addRow(['Tool', '$9.99', '200']);
47+
$table->display();
48+
cli\line();
49+
50+
// Example 3: Center Alignment
51+
cli\line('%Y## Example 3: Center Alignment%n');
52+
cli\line();
53+
$table = new cli\Table();
54+
$table->setHeaders(['Left', 'Center', 'Right']);
55+
$table->setAlignments([
56+
'Left' => cli\table\Column::ALIGN_LEFT,
57+
'Center' => cli\table\Column::ALIGN_CENTER,
58+
'Right' => cli\table\Column::ALIGN_RIGHT,
59+
]);
60+
$table->addRow(['Text', 'Centered', 'More']);
61+
$table->addRow(['Data', 'Values', 'Here']);
62+
$table->addRow(['A', 'B', 'C']);
63+
$table->display();
64+
cli\line();
65+
66+
// Example 4: Real-world Database Table Sizes
67+
cli\line('%Y## Example 4: Database Table Sizes (Real-world Use Case)%n');
68+
cli\line('This example shows how the alignment feature makes database');
69+
cli\line('statistics much more readable and easier to compare.');
70+
cli\line();
71+
$table = new cli\Table();
72+
$table->setHeaders(['Table Name', 'Rows', 'Data Size', 'Index Size']);
73+
$table->setAlignments([
74+
'Table Name' => cli\table\Column::ALIGN_LEFT,
75+
'Rows' => cli\table\Column::ALIGN_RIGHT,
76+
'Data Size' => cli\table\Column::ALIGN_RIGHT,
77+
'Index Size' => cli\table\Column::ALIGN_RIGHT,
78+
]);
79+
$table->addRow(['wp_posts', '1,234', '5.2 MB', '1.1 MB']);
80+
$table->addRow(['wp_postmeta', '45,678', '23.4 MB', '8.7 MB']);
81+
$table->addRow(['wp_comments', '9,012', '2.3 MB', '0.8 MB']);
82+
$table->addRow(['wp_options', '456', '1.5 MB', '0.2 MB']);
83+
$table->addRow(['wp_users', '89', '0.1 MB', '0.05 MB']);
84+
$table->display();
85+
cli\line();
86+
87+
// Example 5: Alignment Constants
88+
cli\line('%Y## Alignment Constants%n');
89+
cli\line();
90+
cli\line('You can use the following constants from %Ccli\table\Column%n:');
91+
cli\line(' %G*%n %CALIGN_LEFT%n - Left align (default)');
92+
cli\line(' %G*%n %CALIGN_RIGHT%n - Right align (good for numbers)');
93+
cli\line(' %G*%n %CALIGN_CENTER%n - Center align');
94+
cli\line();
95+
cli\line('Example usage:');
96+
cli\line(' %c$table->setAlignments([%n');
97+
cli\line(' %c\'Column1\' => cli\table\Column::ALIGN_LEFT,%n');
98+
cli\line(' %c\'Column2\' => cli\table\Column::ALIGN_RIGHT,%n');
99+
cli\line(' %c]);%n');
100+
cli\line();
101+
102+
cli\line('%GDone!%n');
103+
cli\line();

lib/cli/Table.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use cli\Shell;
1616
use cli\Streams;
1717
use cli\table\Ascii;
18+
use cli\table\Column;
1819
use cli\table\Renderer;
1920
use cli\table\Tabular;
2021

@@ -27,6 +28,14 @@ class Table {
2728
protected $_footers = array();
2829
protected $_width = array();
2930
protected $_rows = array();
31+
protected $_alignments = array();
32+
33+
/**
34+
* Cached map of valid alignment constants.
35+
*
36+
* @var array|null
37+
*/
38+
private static $_valid_alignments_map = null;
3039

3140
/**
3241
* Initializes the `Table` class.
@@ -40,11 +49,12 @@ class Table {
4049
* table are used as the header values.
4150
* 3. Pass nothing and use `setHeaders()` and `addRow()` or `setRows()`.
4251
*
43-
* @param array $headers Headers used in this table. Optional.
44-
* @param array $rows The rows of data for this table. Optional.
45-
* @param array $footers Footers used in this table. Optional.
52+
* @param array $headers Headers used in this table. Optional.
53+
* @param array $rows The rows of data for this table. Optional.
54+
* @param array $footers Footers used in this table. Optional.
55+
* @param array $alignments Column alignments. Optional.
4656
*/
47-
public function __construct(array $headers = array(), array $rows = array(), array $footers = array()) {
57+
public function __construct(array $headers = array(), array $rows = array(), array $footers = array(), array $alignments = array()) {
4858
if (!empty($headers)) {
4959
// If all the rows is given in $headers we use the keys from the
5060
// first row for the header values
@@ -66,6 +76,10 @@ public function __construct(array $headers = array(), array $rows = array(), arr
6676
$this->setFooters($footers);
6777
}
6878

79+
if (!empty($alignments)) {
80+
$this->setAlignments($alignments);
81+
}
82+
6983
if (Shell::isPiped()) {
7084
$this->setRenderer(new Tabular());
7185
} else {
@@ -79,6 +93,7 @@ public function resetTable()
7993
$this->_width = array();
8094
$this->_rows = array();
8195
$this->_footers = array();
96+
$this->_alignments = array();
8297
return $this;
8398
}
8499

@@ -175,6 +190,8 @@ public function displayRow(array $row) {
175190
*/
176191
public function getDisplayLines() {
177192
$this->_renderer->setWidths($this->_width, $fallback = true);
193+
$this->_renderer->setHeaders($this->_headers);
194+
$this->_renderer->setAlignments($this->_alignments);
178195
$border = $this->_renderer->border();
179196

180197
$out = array();
@@ -240,6 +257,29 @@ public function setFooters(array $footers) {
240257
$this->_footers = $this->checkRow($footers);
241258
}
242259

260+
/**
261+
* Set the alignments of the table.
262+
*
263+
* @param array $alignments An array of alignment constants keyed by column name.
264+
*/
265+
public function setAlignments(array $alignments) {
266+
// Initialize the cached valid alignments map on first use
267+
if ( null === self::$_valid_alignments_map ) {
268+
self::$_valid_alignments_map = array_flip( array( Column::ALIGN_LEFT, Column::ALIGN_RIGHT, Column::ALIGN_CENTER ) );
269+
}
270+
271+
$headers_map = ! empty( $this->_headers ) ? array_flip( $this->_headers ) : null;
272+
foreach ( $alignments as $column => $alignment ) {
273+
if ( ! isset( self::$_valid_alignments_map[ $alignment ] ) ) {
274+
throw new \InvalidArgumentException( "Invalid alignment value '$alignment' for column '$column'." );
275+
}
276+
// Only validate column names if headers are already set
277+
if ( $headers_map !== null && ! isset( $headers_map[ $column ] ) ) {
278+
throw new \InvalidArgumentException( "Column '$column' does not exist in table headers." );
279+
}
280+
}
281+
$this->_alignments = $alignments;
282+
}
243283

244284
/**
245285
* Add a row to the table.

lib/cli/table/Ascii.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,24 @@ public function row( array $row ) {
206206
return $ret;
207207
}
208208

209+
/**
210+
* Get the alignment for a column.
211+
*
212+
* @param int $column Column index.
213+
* @return int Alignment constant (STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH).
214+
*/
215+
private function getColumnAlignment( $column ) {
216+
$column_name = isset( $this->_headers[ $column ] ) ? $this->_headers[ $column ] : '';
217+
if ( $column_name !== '' && array_key_exists( $column_name, $this->_alignments ) ) {
218+
return $this->_alignments[ $column_name ];
219+
}
220+
return Column::ALIGN_LEFT;
221+
}
222+
209223
private function padColumn($content, $column) {
224+
$alignment = $this->getColumnAlignment( $column );
210225
$content = str_replace( "\t", ' ', (string) $content );
211-
return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ) ) . $this->_characters['padding'];
226+
return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ), false, $alignment ) . $this->_characters['padding'];
212227
}
213228

214229
/**

lib/cli/table/Column.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* PHP Command Line Tools
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this package in the file LICENSE.
7+
*
8+
* @author James Logsdon <dwarf@girsbrain.org>
9+
* @copyright 2010 James Logsdom (http://girsbrain.org)
10+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
11+
*/
12+
13+
namespace cli\table;
14+
15+
/**
16+
* Column alignment constants for table rendering.
17+
*/
18+
interface Column {
19+
const ALIGN_LEFT = STR_PAD_RIGHT;
20+
const ALIGN_RIGHT = STR_PAD_LEFT;
21+
const ALIGN_CENTER = STR_PAD_BOTH;
22+
}

lib/cli/table/Renderer.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,30 @@
1717
*/
1818
abstract class Renderer {
1919
protected $_widths = array();
20+
protected $_alignments = array();
21+
protected $_headers = array();
2022

21-
public function __construct(array $widths = array()) {
23+
public function __construct(array $widths = array(), array $alignments = array()) {
2224
$this->setWidths($widths);
25+
$this->setAlignments($alignments);
26+
}
27+
28+
/**
29+
* Set the alignments of each column in the table.
30+
*
31+
* @param array $alignments The alignments of the columns.
32+
*/
33+
public function setAlignments(array $alignments) {
34+
$this->_alignments = $alignments;
35+
}
36+
37+
/**
38+
* Set the headers of the table.
39+
*
40+
* @param array $headers The headers of the table.
41+
*/
42+
public function setHeaders(array $headers) {
43+
$this->_headers = $headers;
2344
}
2445

2546
/**

0 commit comments

Comments
 (0)