This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Fabien Potencier a5dbc68cd4 feature #24363 [Console] Modify console output and print multiple modifyable sections (pierredup)
This PR was squashed before being merged into the 4.1-dev branch (closes #24363).

Discussion
----------

[Console] Modify console output and print multiple modifyable sections

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | TBD
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#9304

Add support to create different output sections for the console output.
Each section is it's own 'stream' of output, where the output can be modified (even if there are other output after it). This allows you to modify previous output in the console, either by appending new lines, modifying previous lines or clearing the output. Modifying a sections output doesn't affect the output after that or in other sections.

Some examples of what can be done:

**Overwriting content in a previous section:**

Code:

```php
$section1 = $output->section();
$section2 = $output->section();

$section1->writeln("<comment>Doing something</comment>\n");
usleep(500000);
$section2->writeln('<info>Result of first operation</info>');
usleep(500000);

$section1->overwrite("<comment>Doing something else</comment>\n");
usleep(500000);
$section2->writeln('<info>Result of second operation</info>');
usleep(500000);

$section1->overwrite("<comment>Finishing</comment>\n");
usleep(500000);
$section2->writeln('<info>Last Result</info>');
```

Result:
![overwrite-append](https://user-images.githubusercontent.com/144858/30975030-769f2c46-a471-11e7-819f-c3698b43f0af.gif)

**Multiple Progress Bars:**

Code:

```php
$section1 = $output->section();
$section2 = $output->section();

$progress = new ProgressBar($section1);
$progress2 = new ProgressBar($section2);

$progress->start(100);
$progress2->start(100);

$c = 0;
while (++$c < 100) {
    $progress->advance();

    if ($c % 2 === 0) {
        $progress2->advance(4);
    }

    usleep(500000);
}
```

Result:
![multiple-progress](https://user-images.githubusercontent.com/144858/30975119-b63222be-a471-11e7-89aa-a555cdf3d2e0.gif)

**Modifying content of a table & updating a progress bar:**

Code:

```php
$section1 = $output->section();
$section2 = $output->section();

$progress = new ProgressBar($section1);
$table = new Table($section2);

$table->addRow(['Row 1']);
$table->render();

$progress->start(5);

$c = 0;
while (++$c < 5) {
    $table->appendRow(['Row '.($c + 1)]);

    $progress->advance();

    usleep(500000);
}

$progress->finish();
$section1->clear();
```

Result:
![progress-table](https://user-images.githubusercontent.com/144858/30975176-e332499c-a471-11e7-9d4f-f58b464a53c2.gif)

**Example with Symfony Installer:***

Before:
![sf-installer-old](https://user-images.githubusercontent.com/144858/30975291-40f22106-a472-11e7-8836-bc39139c2d30.gif)

After:
![sf-installer](https://user-images.githubusercontent.com/144858/30975302-4a00acf4-a472-11e7-83ba-88ea9d0f0f3f.gif)

TODO:
- [x] Add unit tests

Commits
-------

9ec51a1797 [Console] Modify console output and print multiple modifyable sections
2018-03-20 15:41:21 +01:00
.composer Drop hirak/prestissimo 2016-05-12 07:44:15 -05:00
.github add hint in Github pull request template 2018-03-18 17:01:38 +01:00
src/Symfony feature #24363 [Console] Modify console output and print multiple modifyable sections (pierredup) 2018-03-20 15:41:21 +01:00
.editorconfig Add EditorConfig File 2012-06-16 14:08:15 +02:00
.gitignore Add appveyor.yml for C.I. on Windows 2015-08-25 23:41:37 +02:00
.php_cs.dist Merge branch '2.8' into 3.3 2018-01-03 18:13:53 +01:00
.travis.yml Merge branch '3.4' into 4.0 2018-02-22 15:57:28 +01:00
CHANGELOG-4.0.md updated CHANGELOG for 4.0.6 2018-03-05 14:26:58 -08:00
CONTRIBUTING.md Mention the community review guide 2016-12-18 22:02:35 +01:00
CONTRIBUTORS.md update CONTRIBUTORS for 2.7.43 2018-03-05 10:31:59 -08:00
LICENSE fixed years in copyright 2018-01-03 08:23:28 +01:00
README.md Prefer composer install instead for using Symfony Installer 2018-01-10 18:56:50 +01:00
UPGRADE-4.0.md Add entry for `container.dumper.inline_class_loader` param at `UPGRADE-3.4.md` and `UPGRADE-4.0.md` 2018-03-01 21:43:47 -03:00
UPGRADE-4.1.md feature #26564 [HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false (fmata) 2018-03-18 20:19:04 +01:00
UPGRADE-5.0.md feature #26564 [HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false (fmata) 2018-03-18 20:19:04 +01:00
appveyor.yml Merge branch '3.4' into 4.0 2018-01-16 19:04:12 +01:00
composer.json Merge branch '2.7' into 2.8 2018-01-21 20:03:25 +01:00
link Update Repository Symlink Helper 2018-02-11 12:15:02 +01:00
phpunit Force phpunit-bridge update (bis) 2017-11-22 10:25:38 +01:00
phpunit.xml.dist [Cache] Rely on mock for Doctrine ArrayCache 2018-03-19 23:12:11 +01:00

README.md

Symfony is a PHP framework for web applications and a set of reusable PHP components. Symfony is used by thousands of web applications (including BlaBlaCar.com and Spotify.com) and most of the popular PHP projects (including Drupal and Magento).

Installation

Documentation

Community

Contributing

Symfony is an Open Source, community-driven project with thousands of contributors. Join them contributing code or contributing documentation.

Security Issues

If you discover a security vulnerability within Symfony, please follow our disclosure procedure.

About Us

Symfony development is sponsored by SensioLabs, led by the Symfony Core Team and supported by Symfony contributors.