Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/cli/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ public function getDisplayLines() {
}

foreach ( $this->_rows as $row ) {
$row = $this->_renderer->row( $row );
$row = explode( PHP_EOL, $row );
$out = array_merge( $out, $row );
// Append line by line; array_merge() in this loop copies $out for every row.
foreach ( explode( PHP_EOL, $this->_renderer->row( $row ) ) as $line ) {
$out[] = $line;
}
}

// Only add final border if there are rows
Expand Down
26 changes: 26 additions & 0 deletions tests/Test_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ public function test_column_value_too_long_ascii() {
}
}

public function test_display_lines_keep_row_order_with_many_rows() {
$table = new cli\Table();
$renderer = new cli\Table\Ascii();
$renderer->setConstraintWidth( 80 );
$table->setRenderer( $renderer );
$table->setHeaders( array( 'ID', 'Value' ) );

$rows = 5000;
for ( $i = 1; $i <= $rows; $i++ ) {
// Every 100th row contains a line break and renders as two lines.
$table->addRow( array( (string) $i, 0 === $i % 100 ? "first\nsecond" : 'value' ) );
}

$out = $table->getDisplayLines();

// Border, header, border, one line per row plus one extra line per multi-line row, border.
$this->assertCount( 3 + $rows + $rows / 100 + 1, $out );
$this->assertSame( '| 1 | value |', $out[3] );
$this->assertSame( '| 100 | first |', $out[102] );
$this->assertSame( '| | second |', $out[103] );
$this->assertSame( '| 101 | value |', $out[104] );
$this->assertSame( '| 5000 | first |', $out[ count( $out ) - 3 ] );
$this->assertSame( '| | second |', $out[ count( $out ) - 2 ] );
$this->assertSame( $out[0], $out[ count( $out ) - 1 ] );
}

public function test_column_value_too_long_with_multibytes() {

$constraint_width = 80;
Expand Down
Loading