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

Skip to content

Commit caffee8

Browse files
committed
bug #48089 [Console] Fix clear line with question in section (maxbeckers)
This PR was merged into the 6.1 branch. Discussion ---------- [Console] Fix clear line with question in section | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47411 | License | MIT | Doc PR | - In the issue #47411 is the current behavior described (with videos). The problem is in a section using a question and a clear. Then one line is not cleared because of the `return` so submit the input. NOTICE: This bug might be as well in the versions 4.4+, but in the versions < 6.1 it would be more complicated to fix, because the `SymfonyStyle` does not have the property `$output` in this versions. Commits ------- fe1ddd3 [Console] Fix clear line with question in section
2 parents f14901e + fe1ddd3 commit caffee8

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

Lines 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

Lines 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

Lines 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)