Skip to content

[Console] Horizontal table top border is incorrectly rendered #52101

Closed
@alainrinder

Description

@alainrinder

Symfony version(s) affected

6.3.3

Description

Top border of an horizontal table (Symfony Console) won't render correctly : the wrong crossing chars are used.
This is not visible with the default style which uses '+' character for both corners and crossings, but with custom crossing chars like ┼ ┌ ┬ ┐ ┤ ┘ ┴ └ ├ for instance, the bug is visible.

How to reproduce

The following code

$consoleOutput = new ConsoleOutput();

$tableStyle = (new TableStyle())
    ->setHorizontalBorderChars('')
    ->setVerticalBorderChars('')
    ->setCrossingChars('', '', '', '', '', '', '', '', '')
;

$table = (new Table($consoleOutput))
    ->setStyle($tableStyle)
    ->setHeaderTitle('Title')
    ->setHeaders(['Hello', 'World'])
    ->setRows([[1, 2], [3, 4]])
;

$table->setHorizontal(true)->render();
$table->setHorizontal(false)->render();

will render like this :

├──── Title ┼───┤
│ Hello │ 1 │ 3 │
│ World │ 2 │ 4 │
└───────┴───┴───┘
┌──── Title ────┐
│ Hello │ World │
├───────┼───────┤
│ 1     │ 2     │
│ 3     │ 4     │
└───────┴───────┘

instead of what we could expect :

┌──── Title ┬───┐
│ Hello │ 1 │ 3 │
│ World │ 2 │ 4 │
└───────┴───┴───┘
┌──── Title ────┐
│ Hello │ World │
├───────┼───────┤
│ 1     │ 2     │
│ 3     │ 4     │
└───────┴───────┘

Possible Solution

One possible solution i found would be to modify the render method of Symfony\Component\Console\Helper\Table :

The part that renders the first row :

if ($isFirstRow) {
    $this->renderRowSeparator(
        $isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
        $hasTitle ? $this->headerTitle : null,
        $hasTitle ? $this->style->getHeaderTitleFormat() : null
    );
    $isFirstRow = false;
    $hasTitle = false;
}

could be modified like this to fix this issue :

if ($isFirstRow) {
    $this->renderRowSeparator(
        $isHeader || $horizontal ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
        $hasTitle ? $this->headerTitle : null,
        $hasTitle ? $this->style->getHeaderTitleFormat() : null
    );
    $isFirstRow = false;
    $hasTitle = false;
}

Since there is no header row at the top for an horizontal table, the SEPARATOR_TOP_BOTTOM should not be used for horizontal table.

Maybe that is not the right way to fix it, but it seems to work.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions