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

Skip to content

[Console] Upgrade word wrapping in SymfonyStyle #30519

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

Closed
wants to merge 6 commits into from

Conversation

fchris82
Copy link
Contributor

@fchris82 fchris82 commented Mar 11, 2019

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

The original word wrapping method got sometimes ugly outputs because of formatter tags: <comment>, <info>, etc. The new solution skips these tags from the line lengths, and uses mb_*() functions to handle strings/words.

The original word wrapping method got sometimes ugly outputs because of formatter tags: <comment>, <info>, etc. The new solution skips these tags from the line lengths.
@ro0NL
Copy link
Contributor

ro0NL commented Mar 11, 2019

can you provide some code / console output that lead to this fix? I tend to believe it's a new feature instead, where we would preserve raw formatting.

Not sure createBlock() should try that, as it's not really supported to use nested markup here (given the block itself uses markup); or at least is interpreted as raw text.

cc @chalasr

@fchris82
Copy link
Contributor Author

Hello @ro0NL ,

can you provide some code / console output that lead to this fix? I tend to believe it's a new feature instead, where we would preserve raw formatting.

I have a command with longer and detailed help texts with many colored words or signs to be readable, eg: <fg=yellow;options=bold>[!]</>, You can see information about all available wizards with <info>h</info>. If the name is <comment>yellow</comment> the program is unavailable here. The reason would be there are missing requires OR it has already built/run. Use the <comment>--force</comment> option to disable this check. You can read more information with <comment>wizard --help</comment> command.
And if I remember well - I made this fix 4 months ago -, sometimes there is a help link that were broken in the oroginal solution, and they weren't clickable in the terminal.

The output was really ugly sometimes because of many "hidden" formatter tags, the length of lines were very changing.

Not sure createBlock() should try that, as it's not really supported to use nested markup here (given the block itself uses markup); or at least is interpreted as raw text.

I changed only the original wordwrap() command to a better "wordwrap function". I didn't change the createBlock() logic, in fact I fixed only the wordwrap() not the createBlock().

@chalasr chalasr added this to the 3.4 milestone Mar 11, 2019
@ro0NL
Copy link
Contributor

ro0NL commented Mar 11, 2019

createBlock() is private and has an escape flag, i assume you either use e.g. comment() (no escape) and success() (escape) and only without escaping it breaks, isnt it?

ive added formatted text wrapping in #22225, perhaps we can use it here? and render the result as-is (OUTPUT_RAW), sounds more simple :D

for the case with escaping, e.g. <tag\nbreaks> im not sure we care. Apparently createBlock() can do both :) didnt recall that.

@fchris82
Copy link
Contributor Author

fchris82 commented Mar 12, 2019

Is there anybody who knows what the problem could be here? https://travis-ci.org/symfony/symfony/jobs/504759370 I can see that one test is failed, but it should be good, it works fine at me and on 7.2 also. It looks like that mb_strlen() works wrong on PHP5.5. Is it possible?


@ro0NL

createBlock() is private and has an escape flag, i assume you either use e.g. comment() (no escape) and success() (escape) and only without escaping it breaks, isnt it?

  1. There is a public block() function what I want to use to create custom blocks like comment() or success()
  2. It breaks the URLs as well. It could be annoying.
  3. It could handle UTF-8 characters wrong also. See below.

ive added formatted text wrapping in #22225, perhaps we can use it here? and render the result as-is (OUTPUT_RAW), sounds more simple :D

I looked at this, and yes, it could be good. As I can see I should refactor everywhere the strlen() and other string functions to their mb_* version.

<?php
// test.php
$string = 'őúáű';
$simple = strlen($string);
$mb = mb_strlen($string);

printf("String: %s\nSimple: %d\nMB: %d", $string, $simple, $mb);
# PHP 5.6
$ docker run --rm --volume $PWD:/app --workdir /app php:5.6-cli php test.php
String: őúáű
Simple: 8
MB: 4
# PHP 7.2
$ docker run --rm --volume $PWD:/app --workdir /app php:7.2-cli php test.php
String: őúáű
Simple: 8
MB: 4

@ro0NL
Copy link
Contributor

ro0NL commented Mar 12, 2019

for the UTF related issue, would it be better to create a separate PR?

ive added formatted text wrapping in #22225, perhaps we can use it here? and render the result as-is (OUTPUT_RAW), sounds more simple :D

I looked at this, and yes, it could be good.

based on the amount of code here (and a new feature (breaking tags) of which im not sure we need it) i would prefer that route.

@fchris82
Copy link
Contributor Author

fchris82 commented Mar 12, 2019

@ro0NL

for the UTF related issue, would it be better to create a separate PR?

Yes, I agree with this.

based on the amount of code here (and a new feature (breaking tags) of which im not sure we need it) i would prefer that route.

It isn't clear exactly for me. Which route do you prefer?

@ro0NL
Copy link
Contributor

ro0NL commented Mar 12, 2019

Which route do you prefer?

using formatAndWrap($txtWithTags, $maxWidth). The formatted result is wrapped in the "unformatted block content", this combination is then rendered by writeln()

I think even we dont need to change the OUTPUT_ type, using OUTPUT_NORMAL will preserve existing formatted text i expect.

@fchris82
Copy link
Contributor Author

@ro0NL

OK, but it is only available from 4.2... I close it and I will make an other PR.

@fchris82 fchris82 closed this Mar 12, 2019
@fchris82
Copy link
Contributor Author

@ro0NL Oh, I have just seen that the formatAndWrap() solution is "too simple":

In: "This is a lorem ipsum test message."
Out (width=7):
This is
a lorem
ipsum t   <--????
est mes
sage.

Instead of:
This
is a lorem
ipsum
test
message
.

It could be OK in shorter place (<30 chars, eg: table cell), but it isn't same what I made.

@ro0NL
Copy link
Contributor

ro0NL commented Mar 12, 2019

is a lorem is 10 chars :D

i think we should first focus on correct rendering (not breaking visual output). Then later on we can try to patch formatAndWrap to be more clever concerning 'word breaks' (but never exceed the max width; so worst case we would still chop up words, or more clever with a dash -).

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.

4 participants