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

Skip to content

[Console] Support formatted text cutting #22225

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 4, 2018
Merged

[Console] Support formatted text cutting #22225

merged 1 commit into from
Sep 4, 2018

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Mar 30, 2017

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

allows cutting a formatted text to a certain width. Actually needed if we want to support max. column widths in tables (see #22156)

$text = 'pre <error>foo bar baz</error> post';
dump('BEFORE');
$output->writeln(wordwrap($output->getFormatter()->format($text), 3, "\n", true), OutputInterface::OUTPUT_RAW);
dump('AFTER');
$output->writeln($output->getFormatter()->format($text, 3), OutputInterface::OUTPUT_RAW);

image

* Sets the decorated flag.
*
* @param bool $decorated Whether to decorate the messages or not
* {@inheritdoc}
Copy link
Member

Choose a reason for hiding this comment

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

does this patch apply to lower branches? if yes, should be done there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will look into that.

* @param string $message The message to style
*
* @return string The styled message
* {@inheritdoc}
Copy link
Member

Choose a reason for hiding this comment

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

that one is not possible: $width is not in the interface, thus should be documented here

*
* @return string The styled message
*/
public function format($message);
public function format($message/*, $width = 0*/);
Copy link
Member

@nicolas-grekas nicolas-grekas Apr 3, 2017

Choose a reason for hiding this comment

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

Changing an interface like that is a hard BC break. Anyone implementing it without extending OutputFormatter will get no deprecation warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any suggestions? Took this approach from LogoutUrlGenerator::registerListener ;) which is not bound to an interface.

Note that this way is the easiest implementation and avoids double processing the formatted text, which is nice.

@ro0NL
Copy link
Contributor Author

ro0NL commented Apr 7, 2017

Approach updated.. not sure this makes sense either :P but im concerning users extending from OutputFormatter as well as implementing OutputFormatterInterface.

Guess i like the format($message, $width = 0) api.. but maybe the new interface should simply define wrapAndFormat($message, $width) or so.. and let format() forward.

fabpot added a commit that referenced this pull request Apr 10, 2017
…ro0NL)

This PR was merged into the 2.7 branch.

Discussion
----------

[Console] Inherit phpdoc from OutputFormatterInterface

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22225 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Commits
-------

ff3cb9c [Console] Inherit phpdoc from OutputFormatterInterface
@nicolas-grekas nicolas-grekas self-assigned this Aug 31, 2017
@nicolas-grekas nicolas-grekas removed their assignment Sep 16, 2017
@nicolas-grekas
Copy link
Member

Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw.

@nicolas-grekas nicolas-grekas modified the milestones: 3.4, 4.1 Oct 8, 2017
@fabpot
Copy link
Member

fabpot commented Mar 31, 2018

@ro0NL Can you tell us the status of this PR? Looks like it should be rebased.

@ro0NL
Copy link
Contributor Author

ro0NL commented Mar 31, 2018

@fabpot is the current upgrade path OK? If so, suggestions for naming WrappableOutputFormatterInterface welcome :)

but i was hoping we eventually dont need this interface and have OutputInterface::format($message, $width = 0) API... but the current BC policy doesnt allow that right? Hence we're stuck with this interface.

status: needs work

if (__CLASS__ !== get_class($this)) {
$r = new \ReflectionMethod($this, __FUNCTION__);
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method "%s()" will have a 2nd `$width = 0` argument in version 5.0. Not defining it is deprecated since 4.1.', __METHOD__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

... in version 5.0, not defining it is deprecated since Symfony 4.1

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why you want to force people to define the $width. Not setting it explicitly seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. I should check for defined no. of arguments on the signature.

@ro0NL
Copy link
Contributor Author

ro0NL commented Apr 4, 2018

I think the upgrade path is OK now. But i need to have a 2nd look at the fix.. cause im getting different results now as before.

@ro0NL
Copy link
Contributor Author

ro0NL commented Apr 4, 2018

OK something spiffy is going on

image

without any src changes etc. in between.. in PHPstorm it seems to consistently format wrong.

Curious if this can be confirmed on some other terminal also.

@ro0NL
Copy link
Contributor Author

ro0NL commented Apr 4, 2018

It seems to break as soon as it breaks out the window, thus creates a scrollbar. WTF :)

@fabpot
Copy link
Member

fabpot commented Sep 4, 2018

@ro0NL What do we do here? Any ideas how to move forward?

@ro0NL
Copy link
Contributor Author

ro0NL commented Sep 4, 2018

@fabpot rebased. The breaking format is unrelated (#27832), so if tests are still passing im 👍 to merge. But let me add some rendering tests tomorrow.

@ro0NL
Copy link
Contributor Author

ro0NL commented Sep 4, 2018

And perhaps rethink WrappableOutputFormatterInterface, overriding the definition of OutputFormatterInterface::format().

Perhaps more explicit WrappableOutputFormatterInterface::formatAndWrap(string $message, int $width) and let the implementation forward to format($message, $width), where OutputFormatter::format() still supports the 2nd arg, but not as part of the interface..

Then again, currently it allows to update OutputFormatterInterface::format() with the 2nd arg in 5.0 IIUC

@ro0NL
Copy link
Contributor Author

ro0NL commented Sep 4, 2018

Updated. Much cleaner IMHO.

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

Looks much better to me

@chalasr
Copy link
Member

chalasr commented Sep 4, 2018

Nice one! Thanks Roland.

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

Discussion
----------

[Console] Support formatted text cutting

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes/no
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

allows cutting a formatted text to a certain width. Actually needed if we want to support max. column widths in tables (see #22156)

```php
$text = 'pre <error>foo bar baz</error> post';
dump('BEFORE');
$output->writeln(wordwrap($output->getFormatter()->format($text), 3, "\n", true), OutputInterface::OUTPUT_RAW);
dump('AFTER');
$output->writeln($output->getFormatter()->format($text, 3), OutputInterface::OUTPUT_RAW);
```
![image](https://cloud.githubusercontent.com/assets/1047696/24519346/19c9b0ca-1585-11e7-8437-0bcfb6fab63e.png)

Commits
-------

09f8ad9 [Console] Support formatted text cutting
@ro0NL ro0NL deleted the console/cut-format branch September 5, 2018 05:58
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
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
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.

5 participants