Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1e2f0ae

Browse filesBrowse files
committed
[Console] Fix clear line with question in section
1 parent 97f927a commit 1e2f0ae
Copy full SHA for 1e2f0ae

File tree

3 files changed

+50
-0
lines changed
Filter options

3 files changed

+50
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Output/ConsoleSectionOutput.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public function addContent(string $input)
8787
}
8888
}
8989

90+
/**
91+
* @internal
92+
*/
93+
public function incrementLines()
94+
{
95+
$this->lines++;
96+
}
97+
9098
/**
9199
* {@inheritdoc}
92100
*/

‎src/Symfony/Component/Console/Style/SymfonyStyle.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/SymfonyStyle.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Console\Helper\TableSeparator;
2323
use Symfony\Component\Console\Input\InputInterface;
2424
use Symfony\Component\Console\Output\ConsoleOutputInterface;
25+
use Symfony\Component\Console\Output\ConsoleSectionOutput;
2526
use Symfony\Component\Console\Output\OutputInterface;
2627
use Symfony\Component\Console\Output\TrimmedBufferOutput;
2728
use Symfony\Component\Console\Question\ChoiceQuestion;
@@ -350,6 +351,11 @@ public function askQuestion(Question $question): mixed
350351
if ($this->input->isInteractive()) {
351352
$this->newLine();
352353
$this->bufferedOutput->write("\n");
354+
if ($this->output instanceof ConsoleSectionOutput) {
355+
// add one line more to the ConsoleSectionOutput because of the `return` to submit the input
356+
// this is relevant when a `ConsoleSectionOutput::clear` is called.
357+
$this->output->incrementLines();
358+
}
353359
}
354360

355361
return $answer;

‎src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
use Symfony\Component\Console\Exception\RuntimeException;
1717
use Symfony\Component\Console\Formatter\OutputFormatter;
1818
use Symfony\Component\Console\Input\ArrayInput;
19+
use Symfony\Component\Console\Input\Input;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2122
use Symfony\Component\Console\Output\ConsoleSectionOutput;
2223
use Symfony\Component\Console\Output\NullOutput;
2324
use Symfony\Component\Console\Output\OutputInterface;
25+
use Symfony\Component\Console\Output\StreamOutput;
2426
use Symfony\Component\Console\Style\SymfonyStyle;
2527
use Symfony\Component\Console\Tester\CommandTester;
2628

@@ -181,4 +183,38 @@ public function testMemoryConsumption()
181183

182184
$this->assertSame(0, memory_get_usage() - $start);
183185
}
186+
187+
public function testAskAndClearExpectFullSectionCleared()
188+
{
189+
$answer = 'Answer';
190+
$inputStream = fopen('php://memory', 'r+');
191+
fwrite($inputStream, $answer.PHP_EOL);
192+
rewind($inputStream);
193+
$input = $this->createMock(Input::class);
194+
$sections = [];
195+
$output = new ConsoleSectionOutput(fopen('php://memory', 'r+', false), $sections, StreamOutput::VERBOSITY_NORMAL, true, new OutputFormatter());
196+
$input
197+
->method('isInteractive')
198+
->willReturn(true);
199+
$input
200+
->method('getStream')
201+
->willReturn($inputStream);
202+
203+
$style = new SymfonyStyle($input, $output);
204+
205+
$style->write('foo');
206+
$givenAnswer = $style->ask('Dummy question?');
207+
$output->write('bar');
208+
$output->clear();
209+
210+
rewind($output->getStream());
211+
$this->assertEquals($answer,$givenAnswer);
212+
$this->assertEquals(
213+
'foo'.PHP_EOL. // write foo
214+
PHP_EOL.PHP_EOL.PHP_EOL." \033[32mDummy question?\033[39m:".PHP_EOL.' > '.PHP_EOL.PHP_EOL.PHP_EOL. // question
215+
'bar'.PHP_EOL. // write bar
216+
"\033[10A\033[0J", // clear 10 lines (9 output lines and one from the answer input return)
217+
stream_get_contents($output->getStream())
218+
);
219+
}
184220
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.