Skip to content

Commit c95fbcf

Browse files
Copilotswissspidy
andauthored
Add ability to add row in a loop to existing table (#190)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 443a53c commit c95fbcf

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

lib/cli/Table.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public function resetTable()
8282
return $this;
8383
}
8484

85+
/**
86+
* Resets only the rows in the table, keeping headers, footers, and width information.
87+
*
88+
* @return $this
89+
*/
90+
public function resetRows()
91+
{
92+
$this->_rows = array();
93+
return $this;
94+
}
95+
8596
/**
8697
* Sets the renderer used by this table.
8798
*
@@ -127,6 +138,33 @@ public function display() {
127138
}
128139
}
129140

141+
/**
142+
* Display a single row without headers or top border.
143+
*
144+
* This method is useful for adding rows incrementally to an already-rendered table.
145+
* It will display the row with side borders and a bottom border (if using Ascii renderer).
146+
*
147+
* @param array $row The row data to display.
148+
*/
149+
public function displayRow(array $row) {
150+
// Update widths if this row has wider content
151+
$row = $this->checkRow($row);
152+
153+
// Recalculate widths for the renderer
154+
$this->_renderer->setWidths($this->_width, false);
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+
}
166+
}
167+
130168
/**
131169
* Get the table lines to output.
132170
*
@@ -154,7 +192,8 @@ public function getDisplayLines() {
154192
$out = array_merge( $out, $row );
155193
}
156194

157-
if (isset($border)) {
195+
// Only add final border if there are rows
196+
if (!empty($this->_rows) && isset($border)) {
158197
$out[] = $border;
159198
}
160199

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
/**

tests/Test_Table.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,79 @@ 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+
314+
try {
315+
\cli\Streams::setStream( 'out', $resource );
316+
317+
$table = new cli\Table();
318+
$renderer = new cli\Table\Ascii();
319+
$table->setRenderer( $renderer );
320+
$table->setHeaders( array( 'Name', 'Age' ) );
321+
322+
// Display a single row
323+
$table->displayRow( array( 'Alice', '30' ) );
324+
325+
$output = file_get_contents( $mockFile );
326+
327+
// Should contain the row data
328+
$this->assertStringContainsString( 'Alice', $output );
329+
$this->assertStringContainsString( '30', $output );
330+
331+
// Should contain borders
332+
$this->assertStringContainsString( '|', $output );
333+
$this->assertStringContainsString( '+', $output );
334+
} finally {
335+
if ( $mockFile && file_exists( $mockFile ) ) {
336+
unlink( $mockFile );
337+
}
338+
}
339+
}
340+
341+
public function test_displayRow_tabular() {
342+
$mockFile = tempnam( sys_get_temp_dir(), 'temp' );
343+
$resource = fopen( $mockFile, 'wb' );
344+
345+
try {
346+
\cli\Streams::setStream( 'out', $resource );
347+
348+
$table = new cli\Table();
349+
$renderer = new cli\Table\Tabular();
350+
$table->setRenderer( $renderer );
351+
$table->setHeaders( array( 'Name', 'Age' ) );
352+
353+
// Display a single row
354+
$table->displayRow( array( 'Alice', '30' ) );
355+
356+
$output = file_get_contents( $mockFile );
357+
358+
// Should contain the row data with tabs
359+
$this->assertStringContainsString( 'Alice', $output );
360+
$this->assertStringContainsString( '30', $output );
361+
} finally {
362+
if ( $mockFile && file_exists( $mockFile ) ) {
363+
unlink( $mockFile );
364+
}
365+
}
366+
}
292367
}

tests/Test_Table_Ascii.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ public function testDrawWithHeadersNoData() {
249249
+----------+----------+
250250
| header 1 | header 2 |
251251
+----------+----------+
252-
+----------+----------+
253252

254253
OUT;
255254
$this->assertInOutEquals(array($headers, $rows), $output);

0 commit comments

Comments
 (0)