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

Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __construct($stream, array &$sections, int $verbosity, bool $dec
public function setMaxHeight(int $maxHeight): void
{
// when changing max height, clear output of current section and redraw again with the new height
$existingContent = $this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $this->lines) : $this->lines);

$previousMaxHeight = $this->maxHeight;
$this->maxHeight = $maxHeight;
$existingContent = $this->popStreamContentUntilCurrentSection($previousMaxHeight ? min($previousMaxHeight, $this->lines) : $this->lines);

parent::doWrite($this->getVisibleContent(), false);
parent::doWrite($existingContent, false);
Expand Down Expand Up @@ -213,7 +213,7 @@ private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFr
break;
}

$numberOfLinesToClear += $section->lines;
$numberOfLinesToClear += $section->maxHeight ? min($section->lines, $section->maxHeight) : $section->lines;
if ('' !== $sectionContent = $section->getVisibleContent()) {
if (!str_ends_with($sectionContent, \PHP_EOL)) {
$sectionContent .= \PHP_EOL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,40 @@ public function testMaxHeight()
$this->assertEquals($expected, stream_get_contents($output->getStream()));
}

public function testMaxHeightMultipleSections()
{
$expected = '';
$sections = [];

$firstSection = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
$firstSection->setMaxHeight(3);

$secondSection = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
$secondSection->setMaxHeight(3);

// fill the first section
$firstSection->writeln(['One', 'Two', 'Three']);
$expected .= 'One'.\PHP_EOL.'Two'.\PHP_EOL.'Three'.\PHP_EOL;

// fill the second section
$secondSection->writeln(['One', 'Two', 'Three']);
$expected .= 'One'.\PHP_EOL.'Two'.\PHP_EOL.'Three'.\PHP_EOL;

// cause overflow of second section (redraw whole section, without first line)
$secondSection->writeln('Four');
$expected .= "\x1b[3A\x1b[0J";
$expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL;

// cause overflow of first section (redraw whole section, without first line)
$firstSection->writeln("Four\nFive\nSix");
$expected .= "\x1b[6A\x1b[0J";
$expected .= 'Four'.\PHP_EOL.'Five'.\PHP_EOL.'Six'.\PHP_EOL;
$expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL;

rewind($this->stream);
$this->assertEquals(escapeshellcmd($expected), escapeshellcmd(stream_get_contents($this->stream)));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

escallesshellcmd is intended here, it's way better to debug output with this, as we can properly see escaped character when there is a failure :

image

}

public function testMaxHeightWithoutNewLine()
{
$expected = '';
Expand Down