The Wayback Machine - https://web.archive.org/web/20220819121336/https://github.com/symfony/symfony/pull/47308
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] [POC] Allow limiting the height of a console section #47308

Open
wants to merge 1 commit into
base: 6.2
Choose a base branch
from

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Aug 17, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR tbd

This extends the Section feature of the Symfony console with the possibility to define a maximum height. The content will automatically "scroll up" when the content exceeds this height.

code used in this example
<?php

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;

require_once 'vendor/autoload.php';

(new SingleCommandApplication())
  ->setCode(function (InputInterface $input, OutputInterface $output): int {
    $output->writeln(['<comment>Testing sections', '================</>', '']);

    // section for log messages, set the max height to 3 lines
    $messageSection = $output->section();
    $messageSection->setMaxHeight(3);

    // section for the progress bar
    $progressSection = $output->section();
    $progressSection->writeln('');

    // run some heavy logic in 100 steps
    $progress = new ProgressBar($progressSection);
    $progress->start(100);
    for ($i = 0; $i < 100; $i++) {
      if (0 === $i % 10) {
        $messageSection->write('   Message from step '.$i);
      } elseif (0 === $i % 5) {
        $messageSection->writeln(' and '.$i);
      }

      $progress->advance();
      usleep(50000);
    }

    $progress->finish();

    // process finished, show all messages (i.e. remove height limitation)
    $messageSection->setMaxHeight(0);

    $progressSection->overwrite(['', '<bg=green>  </> All steps completed successfully!', '']);

    return SingleCommandApplication::SUCCESS;
  })
  ->run();

sf-overflow-sections

This terminal ui can be found in real world applications like the new Docker buildkit and Docker Compose V2. It's very useful to show verbose output in long running process, without taking over the complete terminal buffer.

This is mostly a POC for now. The code is a bit messy and lacking tests.

@wouterj wouterj requested a review from chalasr as a code owner Aug 17, 2022
@carsonbot carsonbot added this to the 6.2 milestone Aug 17, 2022
@carsonbot carsonbot changed the title [POC][Console] Add possibility to define maximum section height [Console] [POC] Add possibility to define maximum section height Aug 17, 2022
@wouterj wouterj changed the title [Console] [POC] Add possibility to define maximum section height [Console] [POC] Allow limiting the height of a console section Aug 17, 2022
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.

None yet

2 participants