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

Disable progress bar when not in an interactive terminal #45596

Open
greg0ire opened this issue Mar 1, 2022 · 16 comments
Open

Disable progress bar when not in an interactive terminal #45596

greg0ire opened this issue Mar 1, 2022 · 16 comments

Comments

@greg0ire
Copy link
Contributor

greg0ire commented Mar 1, 2022

Description

Progress bars are a great way to provide feedback to somebody using a Symfony command in a terminal. However, when running the same Symfony command in a cronjob, you will often get logs flooded by the output of the same progress bar. I found that in my case, checking for the TERM env variable seemed like a reliable way to do this, and I'm proposing this is done internally by Symfony, transparently.

Example

Before

if (getenv('TERM') !== false) {
    $progressBar = new ProgressBar($output);
    $callback = function () use ($progressBar): void {
        $progressBar->advance();
    };
} else {
	$callback = null;
}     
($this->import)($callback);
if (isset($progressBar)) {
    $progressBar->finish();
}

After

$progressBar = new ProgressBar($output);
$callback = function () use ($progressBar): void {
    $progressBar->advance(); // does nothing if TERM is not set
};
($this->import)($callback);
$progressBar->finish(); // does nothing if TERM is not set
@ogizanagi
Copy link
Member

ogizanagi commented Mar 1, 2022

Interesting ; I'm used to do the same whenever the input is not interactive (not exactly what it means, agreed) or not decorated, but I recently stumbled upon https://twitter.com/rectorphp/status/1492135257356779528?s=20&t=GZLDX4J0EwZEDnLQtlctrg. So it seems some would like to keep such progress bars in their CI/crons output (with a lower redraw frequency)?

Another possibility might be a --no-progress option.

@greg0ire
Copy link
Contributor Author

greg0ire commented Mar 1, 2022

A lower redraw frequency might be a great strategy, but it's significantly more complex and means you have some idea of how long the process is going to take. Having an explicit option does sound like a better, more backwards-compatible strategy (and is something I have been looking for in bin/console --help today). 👍

@alexander-schranz
Copy link
Contributor

alexander-schranz commented Mar 1, 2022

Maybe instead of a frequency a steps value could be defined which has a default value for none interactive terminal e.g. 4 then only 0% 25% 50% 75% and 100% is outputted.

@ogizanagi
Copy link
Member

Reminds me of the ProgressIndicator as well, which is yet another alternative to the ProgressBar in some cases, but still requires the developer to adapt the logic for CI/crons output.

@greg0ire
Copy link
Contributor Author

greg0ire commented Mar 2, 2022

I think a piece of the puzzle will be https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/Console/Output/NullOutput.php : it will allow avoiding many if statements in Progress{Bar,Indicator}.
One hurdle is that Symfony does not instantiate those itself, unless the user is using OutputStyle::createProgressBar (there is no ProgressIndicator equivalent, by the way). Maybe objects implementing OutputInterface could have a new method indicating if progress should be printed to it, and another method to set the corresponding flag? Or there could be a static flag directly on the Progress{Bar,Indicator} class?

Regarding the definition of the --no-progress option, I think it should happen here:

protected function getDefaultInputDefinition(): InputDefinition
{
return new InputDefinition([
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display help for the given command. When no command is given display help for the <info>'.$this->defaultCommand.'</info> command'),
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', null),
new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
]);

While --progress seems a bit useless, if feels handy to use InputOption::VALUE_NEGATABLE because we would get a boolean on the other side (with InputOption::VALUE_NONE too, but I think it would be flipped).

🤔 note that if we set the default value of that option to getenv('TERM') !== false), then --progress would be useful.

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@greg0ire
Copy link
Contributor Author

Yes.

@carsonbot carsonbot removed the Stalled label Aug 31, 2022
@StefanvdBroek
Copy link

Yes please!

@szepeviktor
Copy link

Yes please.

@tazorax
Copy link

tazorax commented May 11, 2023

Yes, please :)

@Chi-teck
Copy link
Contributor

Chi-teck commented Jun 16, 2023

This seems not a big deal to work around.

$progress_bar = new ProgressBar($output->isDecorated() ? $output : new NullOutput());

Or in case you are using output style.

$io = new SymfonyStyle($input, $output);
$progress_bar = $io->isDecorated() ? $io->createProgressBar() : new ProgressBar(new NullOutput());

And the most weird case when only stderr is redirected to a file.

$is_decorated = $output instanceof ConsoleOutputInterface && $output->getErrorOutput()->isDecorated();
$progress_bar = new ProgressBar($is_decorated ? $output : new NullOutput());

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@greg0ire
Copy link
Contributor Author

Yes

@sebastiaanluca
Copy link

Just chiming in here, as our GitHub action job is failing because there's still progress bar output after the success message (and exit code?). So a --no-progress flag to disable the progress bar would be great!

2024-02-07T02:45:59.8421060Z > vendor/bin/rector --ansi
2024-02-07T02:46:36.1829547Z     0/3472 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░]   0%
2024-02-07T02:47:06.5587344Z   304/3472 [▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░]   8%
2024-02-07T02:47:33.5312665Z   560/3472 [▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░]  16%
2024-02-07T02:48:02.1145345Z   704/3472 [▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░]  20%
2024-02-07T02:48:33.3649256Z  1056/3472 [▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░]  30%
2024-02-07T02:49:07.6572102Z  1344/3472 [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░]  38%
2024-02-07T02:49:39.0313619Z  1600/3472 [▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░]  46%
2024-02-07T02:49:57.6221429Z  1968/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░]  56%
2024-02-07T02:50:28.1378926Z  2096/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░]  60%
2024-02-07T02:51:02.0328770Z  2336/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░]  67%
2024-02-07T02:51:35.4586190Z  2592/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░]  74%
2024-02-07T02:52:10.0303783Z  2736/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░]  78%
2024-02-07T02:52:48.6929054Z  2864/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░]  82%
2024-02-07T02:53:27.1299380Z  2944/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░]  84%
2024-02-07T02:53:58.7016056Z  3040/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░]  87%
2024-02-07T02:53:58.8778480Z 
2024-02-07T02:53:58.8787746Z 
2024-02-07T02:53:58.9036049Z �[30;42m                                                                                �[39;49m
2024-02-07T02:53:58.9037287Z �[30;42m [OK] Rector is done!                                                           �[39;49m
2024-02-07T02:53:58.9038345Z �[30;42m                                                                                �[39;49m
2024-02-07T02:53:58.9106085Z 
2024-02-07T02:54:00.3588258Z  3472/3472 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

@Chi-teck
Copy link
Contributor

Chi-teck commented Feb 7, 2024

So a --no-progress flag to disable the progress bar would be great!

@sebastiaanluca rector has --no-progress-bar option

@sebastiaanluca
Copy link

I don't know why I posted this in symfony/symfony 😆🫣 AND rector has that flag indeed. I think I need some sleep, apologies.

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