Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alainrinder opened this issue Oct 17, 2023 · 2 comments
Closed

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

alainrinder opened this issue Oct 17, 2023 · 2 comments

Comments

@alainrinder
Copy link

alainrinder commented Oct 17, 2023

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.

@OskarStark
Copy link
Contributor

Could you provide a PR with a test case?

@OskarStark
Copy link
Contributor

OskarStark commented Oct 18, 2023

nicolas-grekas added a commit that referenced this issue Oct 31, 2023
…endered (OskarStark)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[Console] Fix horizontal table top border is incorrectly rendered

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #52101
| License       | MIT

cc `@alainrinder`

Commits
-------

6ef10c1 [Console] Fix horizontal table top border is incorrectly rendered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants