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

Stop goofy formatting when newlines are present already. #5582

Merged
merged 1 commit into from Jan 6, 2015

Conversation

dereuromark
Copy link
Member

Currently the result would be

This is a line that is almost the 55 chars long.
This
is a new sentence which is manually newlined, but is so
long it needs two lines.
...

Now, with this hotfix patch, it is (as expected):

This is a line that is almost the 55 chars long.
This is a new sentence which is manually newlined, but
is so long it needs two lines.

Resolves #5580

@dereuromark dereuromark added this to the 2.6.1 milestone Jan 6, 2015
@lorenzo
Copy link
Member

lorenzo commented Jan 6, 2015

Is this fix required in 3.x?

@@ -355,6 +355,23 @@ public static function wrap($text, $options = array()) {
* @return string Formatted text.
*/
public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
$paragraphs = explode($break, $text);
foreach ($paragraphs as &$paragraph) {
$paragraph = String::_wordWrap($paragraph, $width, $break, $cut);
Copy link
Member

Choose a reason for hiding this comment

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

I would make me more confortable if you do $paragraph[$i] = String::...

Copy link
Member Author

Choose a reason for hiding this comment

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

Why? :) As long as you don't reuse the variables inside that method scope, string references should be rather harmless in PHP.
But I can sure refactor that part.

Copy link
Member

Choose a reason for hiding this comment

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

Makes me imagine that you are passing a string reference into the function. It is just cosmetic.

Copy link
Member

Choose a reason for hiding this comment

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

Dumb question, couldn't you just remove all the newlines and then wrap?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because the newlines are intentional in those cases. The text case shows a semantically relevant example.

If newlines are not relevant, I agree, you should remove those first prior to calling the method.
In CLI context people here usually use

$text = 'very long line'
    . 'another very long line'
    . ...;

Copy link
Member

Choose a reason for hiding this comment

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

But the user has requested the text to be wrapped to a specific length. I guess it is debatable whether the result should be a block of fully justified text though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, but it can easily be user-adjusted to be fully justified by removing the newline characters before and STILL allow the custom newlines for other use cases. But as of now it can't do the latter at all. And for the first you would need the manual removal anyway either way.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough.

@lorenzo
Copy link
Member

lorenzo commented Jan 6, 2015

Good job! 👍

@dereuromark
Copy link
Member Author

Yes, it would also need to be fixed in 3.x.

markstory added a commit that referenced this pull request Jan 6, 2015
Stop goofy formatting when newlines are present already.
@markstory markstory merged commit d21b046 into master Jan 6, 2015
@markstory markstory deleted the master-hotfix-string-wrap branch January 6, 2015 17:04
markstory added a commit that referenced this pull request Jan 8, 2015
Forward port fix for wordwrap and newlines (#5582).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Console text formatting issue with newlines present
3 participants