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

Skip to content

Commit a107488

Browse files
committed
bug #51355 [Console] fix section output when multiples section with max height (joelwurtz)
This PR was merged into the 6.3 branch. Discussion ---------- [Console] fix section output when multiples section with max height | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Partial fix for #35012 | License | MIT | Doc PR | This pr does not fix #35012 However it fixes the second script wrote by `@lyrixx` (#35012 (comment)) with fiber and parallel output. In short when there was multiple sections with max height, it was not taking into account the max height of other sections when doing a full rewrite, leading to erase more lines than intended. Commits ------- 28e9da6 fix(console): fix section output when multiples section with max height
2 parents a0c1de1 + 28e9da6 commit a107488

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function __construct($stream, array &$sections, int $verbosity, bool $dec
4848
public function setMaxHeight(int $maxHeight): void
4949
{
5050
// when changing max height, clear output of current section and redraw again with the new height
51-
$existingContent = $this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $this->lines) : $this->lines);
52-
51+
$previousMaxHeight = $this->maxHeight;
5352
$this->maxHeight = $maxHeight;
53+
$existingContent = $this->popStreamContentUntilCurrentSection($previousMaxHeight ? min($previousMaxHeight, $this->lines) : $this->lines);
5454

5555
parent::doWrite($this->getVisibleContent(), false);
5656
parent::doWrite($existingContent, false);
@@ -213,7 +213,7 @@ private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFr
213213
break;
214214
}
215215

216-
$numberOfLinesToClear += $section->lines;
216+
$numberOfLinesToClear += $section->maxHeight ? min($section->lines, $section->maxHeight) : $section->lines;
217217
if ('' !== $sectionContent = $section->getVisibleContent()) {
218218
if (!str_ends_with($sectionContent, \PHP_EOL)) {
219219
$sectionContent .= \PHP_EOL;

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,40 @@ public function testMaxHeight()
133133
$this->assertEquals($expected, stream_get_contents($output->getStream()));
134134
}
135135

136+
public function testMaxHeightMultipleSections()
137+
{
138+
$expected = '';
139+
$sections = [];
140+
141+
$firstSection = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
142+
$firstSection->setMaxHeight(3);
143+
144+
$secondSection = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
145+
$secondSection->setMaxHeight(3);
146+
147+
// fill the first section
148+
$firstSection->writeln(['One', 'Two', 'Three']);
149+
$expected .= 'One'.\PHP_EOL.'Two'.\PHP_EOL.'Three'.\PHP_EOL;
150+
151+
// fill the second section
152+
$secondSection->writeln(['One', 'Two', 'Three']);
153+
$expected .= 'One'.\PHP_EOL.'Two'.\PHP_EOL.'Three'.\PHP_EOL;
154+
155+
// cause overflow of second section (redraw whole section, without first line)
156+
$secondSection->writeln('Four');
157+
$expected .= "\x1b[3A\x1b[0J";
158+
$expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL;
159+
160+
// cause overflow of first section (redraw whole section, without first line)
161+
$firstSection->writeln("Four\nFive\nSix");
162+
$expected .= "\x1b[6A\x1b[0J";
163+
$expected .= 'Four'.\PHP_EOL.'Five'.\PHP_EOL.'Six'.\PHP_EOL;
164+
$expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL;
165+
166+
rewind($this->stream);
167+
$this->assertEquals(escapeshellcmd($expected), escapeshellcmd(stream_get_contents($this->stream)));
168+
}
169+
136170
public function testMaxHeightWithoutNewLine()
137171
{
138172
$expected = '';

0 commit comments

Comments
 (0)