Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Allow limiting the height of a console section #47308

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
Sep 29, 2022

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.

@wouterj wouterj requested a review from chalasr as a code owner August 17, 2022 15:57
@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
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

I like it a lot!

@PhilETaylor
Copy link
Contributor

Can something "like this" be applied to "fix this"? #29746

@wouterj wouterj force-pushed the console-section-height branch from 297686b to eb86140 Compare September 28, 2022 22:15
@wouterj wouterj changed the title [Console] [POC] Allow limiting the height of a console section [Console] Allow limiting the height of a console section Sep 28, 2022
@wouterj wouterj added the Ready label Sep 28, 2022
@wouterj wouterj force-pushed the console-section-height branch from eb86140 to 1e46912 Compare September 28, 2022 22:52
@wouterj wouterj force-pushed the console-section-height branch from 1e46912 to a72f98a Compare September 29, 2022 10:02
@wouterj
Copy link
Member Author

wouterj commented Sep 29, 2022

I've added some tests, this is ready now as far as I'm concerned.

@fabpot
Copy link
Member

fabpot commented Sep 29, 2022

Thank you @wouterj.

@fabpot fabpot merged commit b7bc764 into symfony:6.2 Sep 29, 2022
@wouterj wouterj deleted the console-section-height branch September 29, 2022 19:36
@fabpot fabpot mentioned this pull request Oct 24, 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.

4 participants