Skip to content

Commit 9a9ef08

Browse files
Copilotswissspidy
andcommitted
Changes before error encountered
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 73f97c1 commit 9a9ef08

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

examples/incremental-table.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
require_once 'common.php';
4+
5+
/**
6+
* Example demonstrating the incremental table row display feature.
7+
* This shows how to display a table header once and then add rows incrementally in a loop.
8+
*/
9+
10+
echo "Example 1: Using displayRow() to add rows incrementally\n";
11+
echo "=========================================================\n\n";
12+
13+
$table = new \cli\Table();
14+
$table->setHeaders(array('Item', 'Status', 'Progress'));
15+
$table->display();
16+
17+
// Simulate processing items in a loop
18+
$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%'),
23+
);
24+
25+
foreach ($items as $item) {
26+
// Add some delay to simulate processing
27+
usleep(200000); // 0.2 seconds
28+
$table->displayRow($item);
29+
}
30+
31+
echo "\n\nExample 2: Using resetRows() to clear and update table data\n";
32+
echo "==============================================================\n\n";
33+
34+
$table2 = new \cli\Table();
35+
$table2->setHeaders(array('Name', 'Age', 'City'));
36+
$table2->addRow(array('Alice', '30', 'New York'));
37+
$table2->addRow(array('Bob', '25', 'London'));
38+
$table2->display();
39+
40+
echo "\nClearing rows and adding new data...\n\n";
41+
42+
$table2->resetRows();
43+
$table2->addRow(array('Charlie', '35', 'Paris'));
44+
$table2->addRow(array('Diana', '28', 'Tokyo'));
45+
$table2->display();
46+
47+
echo "\n\nExample 3: Incremental display with Tabular renderer (for piped output)\n";
48+
echo "========================================================================\n\n";
49+
50+
$table3 = new \cli\Table();
51+
$table3->setRenderer(new \cli\table\Tabular());
52+
$table3->setHeaders(array('ID', 'Name', 'Email'));
53+
$table3->display();
54+
55+
$users = array(
56+
array('1', 'John Doe', 'john@example.com'),
57+
array('2', 'Jane Smith', 'jane@example.com'),
58+
array('3', 'Bob Johnson', 'bob@example.com'),
59+
);
60+
61+
foreach ($users as $user) {
62+
usleep(100000); // 0.1 seconds
63+
$table3->displayRow($user);
64+
}

lib/cli/Table.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Table {
2727
protected $_footers = array();
2828
protected $_width = array();
2929
protected $_rows = array();
30+
protected $_displayed = false;
3031

3132
/**
3233
* Initializes the `Table` class.
@@ -79,6 +80,18 @@ public function resetTable()
7980
$this->_width = array();
8081
$this->_rows = array();
8182
$this->_footers = array();
83+
$this->_displayed = false;
84+
return $this;
85+
}
86+
87+
/**
88+
* Resets only the rows in the table, keeping headers, footers, and width information.
89+
*
90+
* @return $this
91+
*/
92+
public function resetRows()
93+
{
94+
$this->_rows = array();
8295
return $this;
8396
}
8497

@@ -125,6 +138,31 @@ public function display() {
125138
foreach( $this->getDisplayLines() as $line ) {
126139
Streams::line( $line );
127140
}
141+
$this->_displayed = true;
142+
}
143+
144+
/**
145+
* Display a single row without headers or top border.
146+
*
147+
* This method is useful for adding rows incrementally to an already-rendered table.
148+
* It will display the row with side borders and a bottom border (if using Ascii renderer).
149+
*
150+
* @param array $row The row data to display.
151+
*/
152+
public function displayRow(array $row) {
153+
$row = $this->checkRow($row);
154+
$this->_renderer->setWidths($this->_width, $fallback = true);
155+
156+
$rendered_row = $this->_renderer->row($row);
157+
$row_lines = explode( PHP_EOL, $rendered_row );
158+
foreach ( $row_lines as $line ) {
159+
Streams::line( $line );
160+
}
161+
162+
$border = $this->_renderer->border();
163+
if (isset($border)) {
164+
Streams::line( $border );
165+
}
128166
}
129167

130168
/**

tests/Test_Table.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,67 @@ public function test_null_values_are_handled() {
289289
];
290290
$this->assertSame( $expected, $out, 'Null values should be safely converted to empty strings in table output.' );
291291
}
292+
293+
public function test_resetRows() {
294+
$table = new cli\Table();
295+
$table->setHeaders( array( 'Name', 'Age' ) );
296+
$table->addRow( array( 'Alice', '30' ) );
297+
$table->addRow( array( 'Bob', '25' ) );
298+
299+
$this->assertEquals( 2, $table->countRows() );
300+
301+
$table->resetRows();
302+
303+
$this->assertEquals( 0, $table->countRows() );
304+
305+
// Headers should still be intact
306+
$out = $table->getDisplayLines();
307+
$this->assertGreaterThan( 0, count( $out ) );
308+
}
309+
310+
public function test_displayRow_ascii() {
311+
$mockFile = tempnam(sys_get_temp_dir(), 'temp');
312+
$resource = fopen($mockFile, 'wb');
313+
\cli\Streams::setStream('out', $resource);
314+
315+
$table = new cli\Table();
316+
$renderer = new cli\Table\Ascii();
317+
$table->setRenderer( $renderer );
318+
$table->setHeaders( array( 'Name', 'Age' ) );
319+
320+
// Display a single row
321+
$table->displayRow( array( 'Alice', '30' ) );
322+
323+
$output = file_get_contents($mockFile);
324+
unlink($mockFile);
325+
326+
// Should contain the row data
327+
$this->assertStringContainsString( 'Alice', $output );
328+
$this->assertStringContainsString( '30', $output );
329+
330+
// Should contain borders
331+
$this->assertStringContainsString( '|', $output );
332+
$this->assertStringContainsString( '+', $output );
333+
}
334+
335+
public function test_displayRow_tabular() {
336+
$mockFile = tempnam(sys_get_temp_dir(), 'temp');
337+
$resource = fopen($mockFile, 'wb');
338+
\cli\Streams::setStream('out', $resource);
339+
340+
$table = new cli\Table();
341+
$renderer = new cli\Table\Tabular();
342+
$table->setRenderer( $renderer );
343+
$table->setHeaders( array( 'Name', 'Age' ) );
344+
345+
// Display a single row
346+
$table->displayRow( array( 'Alice', '30' ) );
347+
348+
$output = file_get_contents($mockFile);
349+
unlink($mockFile);
350+
351+
// Should contain the row data with tabs
352+
$this->assertStringContainsString( 'Alice', $output );
353+
$this->assertStringContainsString( '30', $output );
354+
}
292355
}

0 commit comments

Comments
 (0)