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] setColumnMaxWidth does not wrap header cells nicely #45520

Closed
ondrejmirtes opened this issue Feb 23, 2022 · 1 comment · Fixed by #45565
Closed

[Console] setColumnMaxWidth does not wrap header cells nicely #45520

ondrejmirtes opened this issue Feb 23, 2022 · 1 comment · Fixed by #45565

Comments

@ondrejmirtes
Copy link
Contributor

Symfony version(s) affected

5.4.3, 6.x possibly too

Description

I'd like setColumnMaxWidth to also wrap headers nicely. Currently it adds an extra border between the header lines.

This is how the output currently looks like:

------ ------------------------------------------------------------
  Line   src/Type/JustNullableTypeTrait.php (in context of class
 ------ ------------------------------------------------------------
         PHPStan\Type\StringType)
 ------ ------------------------------------------------------------
  81     Method PHPStan\Type\StringType::doFoo() has no return type
         specified.
 ------ ------------------------------------------------------------

This is how I'd like it to look like:

------ ------------------------------------------------------------
  Line   src/Type/JustNullableTypeTrait.php (in context of class
         PHPStan\Type\StringType)
 ------ ------------------------------------------------------------
  81     Method PHPStan\Type\StringType::doFoo() has no return type
         specified.
 ------ ------------------------------------------------------------

How to reproduce

In context of SymfonyStyle child class:

		$table = $this->createTable();
		$table->setHeaders($headers);
		$table->setRows($rows);
		$table->setColumnMaxWidth(1, 60);

		$table->render();

Possible Solution

No response

Additional Context

No response

@verfriemelt-dot-org
Copy link
Contributor

verfriemelt-dot-org commented Feb 24, 2022

hey, i can atleast help you with the borders:

<?php

    use Symfony\Component\Console\Helper\TableCell;
    use Symfony\Component\Console\Helper\TableSeparator;
    use Symfony\Component\Console\Output\ConsoleOutput;
    use Symfony\Component\Console\Helper\Table;
    use Symfony\Component\Console\Helper\TableCellStyle;

    include "vendor/autoload.php";

    $table = new Table( new ConsoleOutput() );

    $headerCell = fn( string $content ): TableCell => new TableCell( $content, [ "style" => new TableCellStyle( [ 'fg' => 'green' ] ) ] );

    $table->setRows( [
        [ $headerCell( 'line' ), $headerCell( "src/Type/JustNullableTypeTrait.php (in context of class PHPStan\Type\StringType)" ) ],
        new TableSeparator(),
        [ '81', 'Method PHPStan\Type\StringType::doFoo() has no return type specified.' ],
        [ '81', 'Method PHPStan\Type\StringType::doFoo() has no return type specified.' ],
    ] );

    $table->setColumnMaxWidth( 1, 60 );
    $table->render();
+------+--------------------------------------------------------------+
| line | src/Type/JustNullableTypeTrait.php (in context of class PHPS |
|      | tan\Type\StringType)                                         |
+------+--------------------------------------------------------------+
| 81   | Method PHPStan\Type\StringType::doFoo() has no return type s |
|      | pecified.                                                    |
| 81   | Method PHPStan\Type\StringType::doFoo() has no return type s |
|      | pecified.                                                    |
+------+--------------------------------------------------------------+

the easiest way is to work around the multi-line-headers bug from symfony alltogether, as this would seem to require a lot of refactoring. i had a go at it, but that will get really messy really fast. there seems to be another a bug during the wrapping or somewhere here:

$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));

which breaks the styling on multiline headers :|
see here:

image

maybe there is an easy way to fix this, but i dont have enough experience with the console component ¯\_(ツ)_/¯

and cheers to all your effort with phpstan, maybe this helps a little.

@fabpot fabpot closed this as completed Apr 9, 2022
fabpot added a commit that referenced this issue Apr 9, 2022
This PR was merged into the 4.4 branch.

Discussion
----------

Fix table header seperator wrapping

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #45520
| License       | MIT
| Doc PR        |

Before PR, a new seperator is added foreach split line in header

```php
        $table = new Table();
        $table
            ->setHeaders([
                [
                    'Publication',
                    'Very long header with a lot of information',
                ],
            ])
            ->setRows([
                [
                    '1954',
                    'The Lord of the Rings, by J.R.R. Tolkien',
                ],
            ])
            ->setColumnMaxWidth(1, 30);
```
Before PR:

![image](https://user-images.githubusercontent.com/9253091/155845175-94482e50-8507-4762-9da1-dbc88967bd47.png)

After PR
![image](https://user-images.githubusercontent.com/9253091/155845154-3182ec3a-eaa8-423f-a1bc-8744c2a95a88.png)

(Fabbot cannot be green (break unit tests))

Commits
-------

ebf38b1 [Console] Header with column max width is now well wrap with separator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants