Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Support max column width in Table #28373

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

Merged
merged 1 commit into from
Sep 11, 2018
Merged

[Console] Support max column width in Table #28373

merged 1 commit into from
Sep 11, 2018

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Sep 5, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #22156, #27832
License MIT
Doc PR symfony/symfony-docs#10300

Continuation of #22225 to better preserve spaces (which preserves background colors), using wordwrap it caused some issues.

Also the wrapping was plain wrong by not taking the current line length into account.

While at it, it comes with Table integration :)

Given

$table = new Table($output);
$table->setColumnMaxWidth(0, 2);
$table->setRow(0, ['pre <error>foo bar baz</error> post']);
$table->render();

$table = new Table($output);
$table->setColumnMaxWidth(0, 3);
$table->setRow(0, ['pre <error>foo bar baz</error> post']);
$table->render();

$table = new Table($output);
$table->setColumnMaxWidth(0, 4);
$table->setRow(0, ['pre <error>foo bar baz</error> post']);
$table->render();

image


$lines = array();
foreach (explode("\n", $text) as $line) {
$lines[] = '' === $line ? $line : $this->styleStack->getCurrent()->apply($line);
Copy link
Contributor Author

@ro0NL ro0NL Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we fix #27832 by formatting each line separate. This only helps when the user wraps a string. If it's wrapped by the terminal it can still occur.

@nicolas-grekas nicolas-grekas added this to the next milestone Sep 6, 2018
@nicolas-grekas
Copy link
Member

(tests are red)

@ro0NL
Copy link
Contributor Author

ro0NL commented Sep 6, 2018

Ah forgot to mention; failures unrelated :)

fabbot.io is a false-positive IMHO

travis

1) Symfony\Component\Lock\Tests\Store\PdoStoreTest::testRefreshLock
Failed asserting that false is true.

appveyor:

KO src\Symfony/Bundle/SecurityBundle

@nicolas-grekas
Copy link
Member

I restarted the builds on Travis and appveyor. Let's fix fabbot.

@ro0NL
Copy link
Contributor Author

ro0NL commented Sep 6, 2018

Done. Consistent with render() anyway and doesnt break formatting: https://api.symfony.com/3.4/Symfony/Component/Console/Helper/Table.html#method_render

if ('' !== $current && "\n" !== substr($current, -1)) {
$text = "\n".$text;
if ($this->isDecorated()) {
foreach ($lines as &$line) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not use a reference when we can do without

*
* @return $this
*/
public function setColumnMaxWidths(array $widths): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this one? Can't we do the same with setColumnMaxWidth and keep a smaller public API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently we have setColumnWidth() and setColumnWidths() to control min. width. We can do without setColumnMaxWidths() but might raise some questions (why cant i set max. widths at once?)

IMHO the ideal api is: setColumnMinWidth + setColumnMaxWidth + setColumnWidth where the latter sets both min. and max. (thus fixed). However that requires to rename the current setColumnWidth to setColumnMinWidth first. Not aiming for that :)

Do you suggest to drop setColumnMaxWidths and add array support in setColumnMaxWidth or just drop it?

@ro0NL
Copy link
Contributor Author

ro0NL commented Sep 7, 2018

Ready for me :) really happy with this feature.

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this! Thanks Roland.

@@ -142,6 +142,7 @@ public function formatAndWrap(string $message, int $width)
$offset = 0;
$output = '';
$tagRegex = '[a-z][a-z0-9,_=;-]*+';
$length = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment: should we rename $length as $maxLength ? "length" is too generic, and in this case is confusing because we are also using "width" --> $this->applyCurrentStyle(..., ..., $width, $length)

Copy link
Contributor Author

@ro0NL ro0NL Sep 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$currentLineLength? It's not a max value, e.g. for $width = 5 and $text = '123456'

12345 // $length = 0 aka "full"
6     // $length = 1

So we know we can add 5-1=4 more chars before the next line break

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for $currentLineLength

chalasr pushed a commit that referenced this pull request Sep 11, 2018
This PR was merged into the 3.4 branch.

Discussion
----------

[Console] Fix typo in tests

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Spotted in #28373

cc @chalasr

Commits
-------

01e491e [Console] Fix typo in tests
@chalasr
Copy link
Member

chalasr commented Sep 11, 2018

Thank you @ro0NL.

@chalasr chalasr merged commit 175f68f into symfony:master Sep 11, 2018
chalasr pushed a commit that referenced this pull request Sep 11, 2018
This PR was squashed before being merged into the 4.2-dev branch (closes #28373).

Discussion
----------

[Console] Support max column width in Table

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #22156, #27832
| License       | MIT
| Doc PR        | symfony/symfony-docs#10300

Continuation of #22225 to better preserve spaces (which preserves background colors), using `wordwrap` it caused some issues.

Also the wrapping was plain wrong by not taking the current line length into account.

While at it, it comes with `Table` integration :)

Given

```php
$table = new Table($output);
$table->setColumnMaxWidth(0, 2);
$table->setRow(0, ['pre <error>foo bar baz</error> post']);
$table->render();

$table = new Table($output);
$table->setColumnMaxWidth(0, 3);
$table->setRow(0, ['pre <error>foo bar baz</error> post']);
$table->render();

$table = new Table($output);
$table->setColumnMaxWidth(0, 4);
$table->setRow(0, ['pre <error>foo bar baz</error> post']);
$table->render();
```

![image](https://user-images.githubusercontent.com/1047696/45101516-f19b5880-b12b-11e8-825f-6a1d84f68f47.png)

Commits
-------

175f68f [Console] Support max column width in Table
@ro0NL ro0NL deleted the console/table-width branch September 11, 2018 17:22
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
@pjcdawkins
Copy link
Contributor

In case you're interested in an older implementation of something that sets column widths according to the terminal width:
https://github.com/platformsh/platformsh-cli/blob/281b998a369339a0e481cf450d986f03f52e2146/src/Console/AdaptiveTable.php#L12

Looks like it can now be significantly simplified based on these Console changes (thanks @ro0NL)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants