8000 Correctly clear lines for multi-line progress bar messages by grasmash · Pull Request #40460 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Correctly clear lines for multi-line progress bar messages #40460

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
Mar 17, 2021

Conversation

grasmash
Copy link
Contributor
@grasmash grasmash commented Mar 14, 2021

Measure the length of individual lines when calculating progress bar message length.

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
License MIT

When a multiline message is added to a progress bar, the progress bar measures the length of all lines combined in a single string without considering whether that string contains new line characters. It is not accurately measuring the number of lines that are displayed on the terminal.

On the terminal, the actual number of new lines is dictated by a combination of new line characters and text extending beyond the terminal width. Due to this bug, the progress bar sometimes clears too many lines when the progress bar is advanced.

The progress bar should instead consider the overlap of $this->formatLineCount and the actual number of lines in the message and then add any remainder.

8000
@grasmash grasmash requested a review from chalasr as a code owner March 14, 2021 01:27
@carsonbot carsonbot added this to the 5.2 milestone Mar 14, 2021
@grasmash grasmash changed the title Update ProgressBar.php Measure the length of individual lines when calculating progress bar message length. Mar 14, 2021
@chalasr
Copy link
Member
chalasr commented Mar 14, 2021

Thanks for this. Should it be applied to 4.4 as well?
Also, can you add a test?

@grasmash
Copy link
Contributor Author

Yes, I’m working on a test and also trying to diagnose why the current tests are failing. But, I find that running tests while Xdebug is enableed is extremely slow with Symfony. Any tips on speeding that up?

@grasmash
Copy link
Contributor Author
grasmash commented Mar 14, 2021

So, the test is failing because it expects:

  0/50 [>---------------------------]   0%
\x1b[1A\x1b[0J  1/50 [>---------------------------]   2%
\x1b[1A\x1b[0J  2/50 [=>--------------------------]   4%

But the actual is:

  0/50 [>---------------------------]   0%
\x1b[2A\x1b[0J  1/50 [>---------------------------]   2%
\x1b[2A\x1b[0J  2/50 [=>--------------------------]   4%

I don't really understand the difference between the \x1b[1A and \x1b[2A strings.

@grasmash
Copy link
Contributor Author
grasmash commented Mar 14, 2021

Ok, so I did so research. This is a great article: https://notes.burke.libbey.me/ansi-escape-codes/. The 2A indicates that 2 lines should be cleared rather than 1. That seems right looking at the 3 line output in the example.

The rather unfun part of testing this is that you can't actually see the diff for the failing tests on the command line because they're rendered ANSI escape codes and not actually visible. Apart from using xDebug to inspect the variables (which is terribly slow) I'm not sure how one is supposed to know what's being printed.

$message_lines = explode("\n", $message);
$line_count = \count($message_lines) - $this->formatLineCount + 1;
foreach ($message_lines as $message_line) {
if (Helper::strlen($message_line) / $this->terminal->getWidth() > 1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a second bug that's being fixed. If the Terminal width exactly matches the line length, we should not count an extra line. We should only count an extra line if it exceeds the line length.

@grasmash
Copy link
Contributor Author
grasmash commented Mar 14, 2021

I don't believe new tests are needed. The existing test fixtures have been updated to assert that lines are emitted correctly.

Admittedly just looking at the test fixtures doesn't grant much confidence. I performed a number of successful manual tests that used combinations of short content, long content, and content with new line characters. They all displayed as expected.

However, I'd like a functional review by someone else who is able to more readily identify issues.

@grasmash
Copy link
Contributor Author

@chalasr I believe this is ready to be merged. The failing tests seem unrelated to the changes made here.

@chalasr
Copy link
Member
chalasr commented Mar 14, 2021

Thank you for the detailed explanations, it makes everyone more comfortable with reviewing/merging such a patch (low-level + changes in existing tests).

Still, I'd like to checkout your branch to ensure we don't have any regression on the modified test cases (as it happened a lot in the past). I will look into this by the end of the week.

@grasmash
Copy link
Contributor Author

Still, I'd like to checkout your branch to ensure we don't have any regression on the modified test cases (as it happened a lot in the past). I will look into this by the end of the week.

Completely agree. 👍

@grasmash
Copy link
Contributor Author
grasmash commented Mar 14, 2021

Separately, it looks like I should merge my approach with this proposed change: https://github.com/symfony/symfony/pull/40450/files

Done :)

@chalasr
Copy link
Member
chalasr commented Mar 14, 2021

Thanks, I was about to ask you if you could have a look at #40450 :)
Also can you rebase your PR on the 4.4 branch (+ change the base branch by editing your PR)?

@grasmash
Copy link
Contributor Author

I can rebase it, but I would prefer to have that in a separate pull request. Would that be OK?

@grasmash
Copy link
Contributor Author

Need to do some more work here to correctly integrate changes from #40450, seems to have caused a problem.

@grasmash
Copy link
Contributor Author

@chalasr Ok. Lot's of changes, but much better now:

  1. I've added a new test and left existing fixtures as-is.
  2. I've rebased on to 4.4 and changed the base branch for the PR.
  3. I've cherry-picked @danepowell's new test from [Console] ProgressBar clears too many lines on update #40450.

@grasmash
Copy link
Contributor Author

I think that all test failures are unrelated to the changes. You can see the relevant tests passing here:
https://travis-ci.com/github/symfony/symfony/jobs/491226919#L8401

@danepowell
Copy link
Contributor
danepowell commented Mar 15, 2021 8000

As the owner of #40450, I think this approach makes sense.

@danepowell
Copy link
Contributor
danepowell commented Mar 15, 2021

I guess I should say, I'd prefer for #40450 to be merged first since it addresses a separate bug and I'd love to get credit for the fix 😄 . But as long the bugs get fixed (this PR now fixes both), that's what matters most.

@grasmash
Copy link
Contributor Author

@danepowell I'm fine with that! Though I should say, I intentionally cherry picked your commit so you'd get commit credit if this PR is merged as is.

@nicolas-grekas nicolas-grekas changed the title Measure the length of individual lines when calculating progress bar message length. [Console] Measure the length of individual lines when calculating progress bar message length Mar 16, 2021
@nicolas-grekas
Copy link
Member

I merged #40450 to ease with squashing this PR.

@grasmash
Copy link
Contributor Author

Rebased.

@chalasr
Copy link
Member
chalasr commented Mar 16, 2021

@grasmash Can you squash your 7 commits into 1 so that we can merge?

@grasmash grasmash changed the title [Console] Measure the length of individual lines when calculating progress bar message length [Console] Correctly clear lines for multi-line progress bar messages Mar 17, 2021
@grasmash
Copy link
Contributor Author

@chalasr Done!

@carsonbot carsonbot changed the title [Console] Correctly clear lines for multi-line progress bar messages Correctly clear lines for multi-line progress bar messages Mar 17, 2021
@fabpot
Copy link
Member
fabpot commented Mar 17, 2021

Thank you @grasmash.

@fabpot fabpot merged commit cf79189 into symfony:4.4 Mar 17, 2021
This was referenced Mar 29, 2021
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
0