8000 bug #28638 [Console] Fix clearing sections containing questions (chal… · symfony/symfony@b1f462f · GitHub
[go: up one dir, main page]

Skip to content

Commit b1f462f

Browse files
committed
bug #28638 [Console] Fix clearing sections containing questions (chalasr)
This PR was merged into the 4.1 branch. Discussion ---------- [Console] Fix clearing sections containing questions | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28615 | License | MIT | Doc PR | n/a Given: ```php $section = $output->section(); $question = "What's your name?"; $name = (new QuestionHelper())->ask($input, $section, new Question($question)); $section->clear(); $section->writeln("$question (<info>$name</info>)"); ``` Before: ![](https://i.imgur.com/nJdKXAo.gif) After: ![](https://i.imgur.com/WdMtKvV.gif) Commits ------- 81ec57d [Console] Fix clearing sections containing questions
2 parents 3ae327c + 81ec57d commit b1f462f

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Input\StreamableInputInterface;
1919
use Symfony\Component\Console\Output\ConsoleOutputInterface;
20+
use Symfony\Component\Console\Output\ConsoleSectionOutput;
2021
use Symfony\Component\Console\Output\OutputInterface;
2122
use Symfony\Component\Console\Question\ChoiceQuestion;
2223
use Symfony\Component\Console\Question\Question;
@@ -123,6 +124,10 @@ private function doAsk(OutputInterface $output, Question $question)
123124
$ret = trim($this->autocomplete($output, $question, $< 10000 /span>inputStream, \is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
124125
}
125126

127+
if ($output instanceof ConsoleSectionOutput) {
128+
$output->addContent($ret);
129+
}
130+
126131
$ret = \strlen($ret) > 0 ? $ret : $question->getDefault();
127132

128133
if ($normalizer = $question->getNormalizer()) {

src/Symfony/Component/Console/Output/ConsoleSectionOutput.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ public function getContent(): string
7777
return implode('', $this->content);
7878
}
7979

80+
/**
81+
* @internal
82+
*/
83+
public function addContent(string $input)
84+
{
85+
foreach (explode(PHP_EOL, $input) as $lineContent) {
86+
$this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1;
87+
$this->content[] = $lineContent;
88+
$this->content[] = PHP_EOL;
89+
}
90+
}
91+
8092
/**
8193
* {@inheritdoc}
8294
*/
@@ -88,11 +100,7 @@ protected function doWrite($message, $newline)
88100

89101
$erasedContent = $this->popStreamContentUntilCurrentSection();
90102

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

97105
parent::doWrite($message, true);
98106
parent::doWrite($erasedContent, false);

src/Symfony/Component/Console/Tests/Output/ConsoleSectionOutputTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Formatter\OutputFormatter;
16+
use Symfony\Component\Console\Helper\QuestionHelper;
17+
use Symfony\Component\Console\Input\StreamableInputInterface;
1618
use Symfony\Component\Console\Output\ConsoleSectionOutput;
1719
use Symfony\Component\Console\Output\OutputInterface;
1820
use Symfony\Component\Console\Output\StreamOutput;
21+
use Symfony\Component\Console\Question\Question;
1922

2023
class ConsoleSectionOutputTest extends TestCase
2124
{
2225
private $stream;
2326

2427
protected function setUp()
2528
{
26-
$this->stream = fopen('php://memory', 'r+', false);
29+
$this->stream = fopen('php://memory', 'r+b', false);
2730
}
2831

2932
protected function tearDown()
@@ -137,4 +140,24 @@ public function testMultipleSectionsOutput()
137140
rewind($output->getStream());
138141
$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()));
139142
}
143+
144+
public function testClearSectionContainingQuestion()
145+
{
146+
$inputStream = fopen('php://memory', 'r+b', false);
147+
fwrite($inputStream, "Batman & Robin\n");
148+
rewind($inputStream);
149+
150+
$input = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
151+
$input->expects($this->once())->method('isInteractive')->willReturn(true);
152+
$input->expects($this->once())->method('getStream')->willReturn($inputStream);
153+
154+
$sections = array();
155+
$output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
156+
157+
(new QuestionHelper())->ask($input, $output, new Question('What\'s your favorite super hero?'));
158+
$output->clear();
159+
160+
rewind($output->getStream());
161+
$this->assertSame('What\'s your favorite super hero?'.PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream()));
162+
}
140163
}

0 commit comments

Comments
 (0)
0