8000 [Console] Fix clearing sections containing questions by chalasr · Pull Request #28638 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Fix clearing sections containing questions #28638

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
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
Expand Down Expand Up @@ -123,6 +124,10 @@ private function doAsk(OutputInterface $output, Question $question)
$ret = trim($this->autocomplete($output, $question, $inputStream, \is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
}

if ($output instanceof ConsoleSectionOutput) {
$output->addContent($ret);
Copy link
Contributor

Choose a reason for hiding this comment

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

How will this work with hidden input? There might not be any additional lines to clear in that case

Copy link
Member Author

Choose a reason for hiding this comment

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

Answering questions still implies to hit enter, empty and hidden inputs result in a blank line on the screen. Just tried it, works as expected with hidden questions

}

$ret = \strlen($ret) > 0 ? $ret : $question->getDefault();

if ($normalizer = $question->getNormalizer()) {
Expand Down
18 changes: 13 additions & 5 deletions src/Symfony/Component/Console/Output/ConsoleSectionOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public function getContent(): string
return implode('', $this->content);
}

/**
* @internal
*/
public function addContent(string $input)
{
foreach (explode(PHP_EOL, $input) as $lineContent) {
$this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1;
$this->content[] = $lineContent;
$this->content[] = PHP_EOL;
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -88,11 +100,7 @@ protected function doWrite($message, $newline)

$erasedContent = $this->popStreamContentUntilCurrentSection();

foreach (explode(PHP_EOL, $message) as $lineContent) {
$this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1;
$this->content[] = $lineContent;
$this->content[] = PHP_EOL;
}
$this->addContent($message);

parent::doWrite($message, true);
parent::doWrite($erasedContent, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Question\Question;

class ConsoleSectionOutputTest extends TestCase
{
private $stream;

protected function setUp()
{
$this->stream = fopen('php://memory', 'r+', false);
$this->stream = fopen('php://memory', 'r+b', false);
Copy link
Member

Choose a reason for hiding this comment

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

should be reverted /cc @nicolas-grekas

}

protected function tearDown()
Expand Down Expand Up @@ -137,4 +140,24 @@ public function testMultipleSectionsOutput()
rewind($output->getStream());
$this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL."\x1b[2A\x1b[0JBar".PHP_EOL."\x1b[1A\x1b[0JBaz".PHP_EOL.'Bar'.PHP_EOL."\x1b[1A\x1b[0JFoobar".PHP_EOL, stream_get_contents($output->getStream()));
}

public function testClearSectionContainingQuestion()
{
$inputStream = fopen('php://memory', 'r+b', false);
Copy link
Member

Choose a reason for hiding this comment

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

same here, no b needed

fwrite($inputStream, "Batman & Robin\n");
rewind($inputStream);

$input = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
$input->expects($this->once())->method('isInteractive')->willReturn(true);
$input->expects($this->once())->method('getStream')->willReturn($inputStream);

$sections = array();
$output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());

(new QuestionHelper())->ask($input, $output, new Question('What\'s your favorite super hero?'));
$output->clear();

rewind($output->getStream());
$this->assertSame('What\'s your favorite super hero?'.PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream()));
}
}
0