Skip to content

Commit b5c8e39

Browse files
Copilotswissspidy
andcommitted
Fix incremental table row display with proper width handling
- Reset border cache when widths change in Ascii renderer - Update displayRow() to properly handle width updates - Improve example to better demonstrate incremental row display - All tests pass (lint, phpcs, phpstan, phpunit) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 9a9ef08 commit b5c8e39

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

examples/incremental-table.php

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
echo "=========================================================\n\n";
1212

1313
$table = new \cli\Table();
14-
$table->setHeaders(array('Item', 'Status', 'Progress'));
14+
$table->setHeaders(array('File Name', 'Status', 'Progress'));
1515
$table->display();
1616

1717
// Simulate processing items in a loop
1818
$items = array(
19-
array('Processing file 1', 'Done', '100%'),
20-
array('Processing file 2', 'Done', '100%'),
21-
array('Processing file 3', 'Done', '100%'),
22-
array('Processing file 4', 'Done', '100%'),
19+
array('file1.txt', 'Done', '100%'),
20+
array('file2.txt', 'Done', '100%'),
21+
array('file3.txt', 'Done', '100%'),
22+
array('file4.txt', 'Done', '100%'),
2323
);
2424

2525
foreach ($items as $item) {
@@ -28,30 +28,50 @@
2828
$table->displayRow($item);
2929
}
3030

31-
echo "\n\nExample 2: Using resetRows() to clear and update table data\n";
32-
echo "==============================================================\n\n";
31+
echo "\n\nExample 2: Using resetRows() with incremental display\n";
32+
echo "========================================================\n\n";
3333

3434
$table2 = new \cli\Table();
3535
$table2->setHeaders(array('Name', 'Age', 'City'));
36-
$table2->addRow(array('Alice', '30', 'New York'));
37-
$table2->addRow(array('Bob', '25', 'London'));
3836
$table2->display();
3937

40-
echo "\nClearing rows and adding new data...\n\n";
38+
echo "Adding first batch of rows...\n";
39+
$table2->displayRow(array('Alice', '30', 'New York'));
40+
$table2->displayRow(array('Bob', '25', 'London'));
4141

42+
echo "\nClearing rows and adding new batch...\n";
4243
$table2->resetRows();
43-
$table2->addRow(array('Charlie', '35', 'Paris'));
44-
$table2->addRow(array('Diana', '28', 'Tokyo'));
45-
$table2->display();
44+
$table2->displayRow(array('Charlie', '35', 'Paris'));
45+
$table2->displayRow(array('Diana', '28', 'Tokyo'));
4646

47-
echo "\n\nExample 3: Incremental display with Tabular renderer (for piped output)\n";
48-
echo "========================================================================\n\n";
47+
echo "\n\nExample 3: Real-time progress display\n";
48+
echo "========================================\n\n";
4949

5050
$table3 = new \cli\Table();
51-
$table3->setRenderer(new \cli\table\Tabular());
52-
$table3->setHeaders(array('ID', 'Name', 'Email'));
51+
$table3->setHeaders(array('Task', 'Result'));
5352
$table3->display();
5453

54+
$tasks = array(
55+
array('Initialize database', 'OK'),
56+
array('Load configuration', 'OK'),
57+
array('Connect to API', 'OK'),
58+
array('Process data', 'OK'),
59+
array('Generate report', 'OK'),
60+
);
61+
62+
foreach ($tasks as $task) {
63+
usleep(300000); // 0.3 seconds
64+
$table3->displayRow($task);
65+
}
66+
67+
echo "\n\nExample 4: Tabular format (for piped output)\n";
68+
echo "==============================================\n\n";
69+
70+
$table4 = new \cli\Table();
71+
$table4->setRenderer(new \cli\table\Tabular());
72+
$table4->setHeaders(array('ID', 'Name', 'Email'));
73+
$table4->display();
74+
5575
$users = array(
5676
array('1', 'John Doe', 'john@example.com'),
5777
array('2', 'Jane Smith', 'jane@example.com'),
@@ -60,5 +80,5 @@
6080

6181
foreach ($users as $user) {
6282
usleep(100000); // 0.1 seconds
63-
$table3->displayRow($user);
83+
$table4->displayRow($user);
6484
}

lib/cli/Table.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ public function display() {
150150
* @param array $row The row data to display.
151151
*/
152152
public function displayRow(array $row) {
153+
// Update widths if this row has wider content
153154
$row = $this->checkRow($row);
154-
$this->_renderer->setWidths($this->_width, $fallback = true);
155+
156+
// Recalculate widths for the renderer
157+
$this->_renderer->setWidths($this->_width, $fallback = false);
155158

156159
$rendered_row = $this->_renderer->row($row);
157160
$row_lines = explode( PHP_EOL, $rendered_row );

lib/cli/table/Ascii.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function setWidths(array $widths, $fallback = false) {
8383
}
8484

8585
$this->_widths = $widths;
86+
// Reset border cache when widths change
87+
$this->_border = null;
8688
}
8789

8890
/**

0 commit comments

Comments
 (0)