-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Symfony Console Style tweaks #15964
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
Symfony Console Style tweaks #15964
Conversation
The first change is about adding a new method called |
@@ -120,10 +120,14 @@ public function title($message) | |||
*/ | |||
public function section($message) | |||
{ | |||
// don't take into account the style tags to compute the message length | |||
// otherwise, the underline displayed after the section title is too long | |||
$messageLength = strlen(preg_replace('/<.*>/U', '', $message)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the wrong way to compute it. there is many cases where it will be computed wrong:
<foo>bar<baz>
will get a length of0
here- if your string contains tags which are not registered as applying styles, they will be stripped while they should not (as they are part of the output)
- if your string contains escaped tags, they will be stripped too.
We already have the implementation of computed the length of a string without counting the decoration. Please just reuse it instead of trying to reimplement it in a broken way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, you can actually use Helper::strlenWithoutDecoration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done this change and for the title()
too. Thanks.
Shouldn't the underline length calculation be applied to |
{ | ||
$this->autoPrependText(); | ||
|
||
if (!is_array($message)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be simplified to something like:
$this->autoPrependText();
$messages = is_array($messages) ? array_values($messages) : array($messages);
foreach ($messages as $message) {
$this->writeln(sprintf(' // %s', $message));
}
Unless you really need the recursion to do something like:
$sfStyle->comment([
array('lorem', array('ipsum', 'dolor')),
'Sit',
'amet',
])
Which would be kind of confusing and should be reflected to other similar methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this proposal. If @kbond (the original author of this code) doesn't say anything against it, I'll do these changes. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ! Actually I didn't see it was the same implementation as text
... It would be a minor BC break again if someone rely on this particular behavior.
Would string[]|string
be more accurate in the docblock ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the recursion is necessary. Simplify away :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes made. Thanks.
* | ||
* @param string|array $message | ||
*/ | ||
public function comment($message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it a BC break ? I think the StyleInterface
is already implemented by some third-party libraries. (But I like the comment method over the text one, it makes much more sense !) Anyway it's a really minor one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, StyleInterface
already was present on 2.7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I've removed the new method from the interface.
Tests are broken |
@@ -110,7 +110,7 @@ public function title($message) | |||
$this->autoPrependBlock(); | |||
$this->writeln(array( | |||
sprintf('<comment>%s</>', $message), | |||
sprintf('<comment>%s</>', str_repeat('=', strlen($message))), | |||
sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($message))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should pass the return value of $this->getFormatter()
as the first argument to Helper::strlenWithoutDecoration()
(same below).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thanks.
This PR is no longer WIP. When tests are green again, this can be merged. Thanks. |
Still one broken test for me:
|
The failing test should be fixed now. |
Thank you @javiereguiluz. |
…aviereguiluz) This PR was squashed before being merged into the 2.8 branch (closes #15972). Discussion ---------- [Console] Updated the styles of the server commands | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR uses `comment()` which hasn't been merged yet. WIP PR at #15964   Commits ------- 4e2cc0f [Console] Updated the styles of the server commands
This PR was merged into the 2.8 branch. Discussion ---------- Updated the styles of the cache commands | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR uses comment() which hasn't been merged yet. WIP PR at #15964    Commits ------- 44c5416 Updated the styles for the "cache:warmup" command 08b2959 Updated the style for the "cache:clear" command
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function comment($message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is not defined in the parent class. So inheritdoc makes no sense. Also I guess this method should be part of StyleInterface. But I don't really see the point of this interface either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very glad that I'm not the only one to wonder about why one insists on having interfaces for everything. Here again, I'm 👍 for removing this interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, the interface is useless as we always create concrete instances instead of injecting them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can image a user-case: Having a centralized place where the style is created. And all commands would just work with StyleInterface. Thus one can easily replace the style evewhere. But that is not how commands currently work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am +1 for removing the interface as well. There was a reason for it that was discussed during the initial PR but I can't find it. Laravel is using this feature but they extend SymfonyStyle
.
This PR was squashed before being merged into the 2.8 branch (closes #15980). Discussion ---------- Updated the styles of the config parameters | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR uses comment() which hasn't been merged yet. WIP PR at #15964 **I have a question**: is it mandatory that the output of this command is a valid YAML or XML file? If not, we can keep the changes in the title of the command. Otherwise, I'll revert those changes.   Commits ------- fdaa513 Updated the styles of the config parameters
…luz) This PR was squashed before being merged into the 2.8 branch (closes #15983). Discussion ---------- Updated the styles of the container commands | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR uses comment() which hasn't been merged yet. WIP PR at #15964 This command defines a lot of options, so I don't know if I've updated all the possible outputs:       Commits ------- d209a4e Updated the styles of the container commands
This PR will contain some minor tweaks found while updating all the Symfony commands. Don't merge yet. Thanks!