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

[Console] ProgressBar progressChar wider than one throws "PHP Fatal error: Uncaught ValueError: str_repeat(): Argument #2 ($times) must be greater than or equal to 0" when displaying at the end of the bar #49111

Open
Sarke opened this issue Jan 26, 2023 · 3 comments

Comments

@Sarke
Copy link

Sarke commented Jan 26, 2023

Symfony version(s) affected

6.2.5

Description

When the progressChar is more than one character wide, it causes an error when updating the progress bar at almost the end.

PHP Fatal error:  Uncaught ValueError: str_repeat(): Argument #2 ($times) must be greater than or equal to 0 in /app/vendor/symfony/console/Helper/ProgressBar.php:511
Stack trace:
#0 /app/vendor/symfony/console/Helper/ProgressBar.php(511): str_repeat()
#1 /app/vendor/symfony/console/Helper/ProgressBar.php(572): Symfony\Component\Console\Helper\ProgressBar::Symfony\Component\Console\Helper\{closure}()
#2 [internal function]: Symfony\Component\Console\Helper\ProgressBar->Symfony\Component\Console\Helper\{closure}()
#3 /app/vendor/symfony/console/Helper/ProgressBar.php(585): preg_replace_callback()
#4 /app/vendor/symfony/console/Helper/ProgressBar.php(411): Symfony\Component\Console\Helper\ProgressBar->buildLine()
#5 /app/progress_error.php(10): Symfony\Component\Console\Helper\ProgressBar->display()
#6 {main}
  thrown in /app/vendor/symfony/console/Helper/ProgressBar.php on line 511

This doesn't always trigger, depending on redraw frequency and number of bar steps.

It's caused by this line returning a negative number in certain situations:

$emptyBars = $bar->getBarWidth() - $completeBars - Helper::length(Helper::removeDecoration($output->getFormatter(), $bar->getProgressCharacter()));

How to reproduce

<?php

require 'vendor/autoload.php';

$output = new \Symfony\Component\Console\Output\ConsoleOutput();
$bar = new \Symfony\Component\Console\Helper\ProgressBar($output);

$bar->setProgressCharacter("> ");
$bar->setMaxSteps(100);
$bar->setProgress(99);

Possible Solution

No response

Additional Context

I encountered this error when reproducing the format from the ProgressBarTest->testAnsiColorsAndEmojis() test, and it has a progress character longer than one as well.

$bar->setProgressCharacter($progress = "\033[32m➤ \033[0m");

This test's progress bar is only o to 15, so I believe it doesn't trigger the error simply because the progress character doesn't get close enough to the end of the bar.

Additionally, what lead me here was because the documentation has this cool progress bar format but no code on how to produce it.

Cool progress bar

I believe the above mentioned test is an earlier version of this (from 5.3):

same from 5.3

@alexislefebvre
Copy link
Contributor

I find it strange to pass several chars to setProgressCharacter(), should it be forbidden?

@Sarke
Copy link
Author

Sarke commented Feb 11, 2023

Yeah, one would think it would only be one with how the functions are named. But like I said I was trying to replicate the demo, and the test has more that one.

It would definitely be the simpler solution.

@alexislefebvre
Copy link
Contributor

I see now, allowing only one character would prevent using colours like in your example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants