| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Output; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 15 |
use Symfony\Component\Console\Helper\Helper; |
| 16 |
use Symfony\Component\Console\Terminal; |
| 17 |
|
| 18 |
/** |
| 19 |
* @author Pierre du Plessis <[email protected]> |
| 20 |
* @author Gabriel Ostrolucký <[email protected]> |
| 21 |
*/ |
| 22 |
class ConsoleSectionOutput extends StreamOutput |
| 23 |
{ |
| 24 |
private array $content = []; |
| 25 |
private int $lines = 0; |
| 26 |
private array $sections; |
| 27 |
private Terminal $terminal; |
| 28 |
private int $maxHeight = 0; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param resource $stream |
| 32 |
* @param ConsoleSectionOutput[] $sections |
| 33 |
*/ |
| 34 |
public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) |
| 35 |
{ |
| 36 |
parent::__construct($stream, $verbosity, $decorated, $formatter); |
| 37 |
array_unshift($sections, $this); |
| 38 |
$this->sections = &$sections; |
| 39 |
$this->terminal = new Terminal(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Defines a maximum number of lines for this section. |
| 44 |
* |
| 45 |
* When more lines are added, the section will automatically scroll to the |
| 46 |
* end (i.e. remove the first lines to comply with the max height). |
| 47 |
*/ |
| 48 |
public function setMaxHeight(int $maxHeight): void |
| 49 |
{ |
| 50 |
// when changing max height, clear output of current section and redraw again with the new height |
| 51 |
$previousMaxHeight = $this->maxHeight; |
| 52 |
$this->maxHeight = $maxHeight; |
| 53 |
$existingContent = $this->popStreamContentUntilCurrentSection($previousMaxHeight ? min($previousMaxHeight, $this->lines) : $this->lines); |
| 54 |
|
| 55 |
parent::doWrite($this->getVisibleContent(), false); |
| 56 |
parent::doWrite($existingContent, false); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Clears previous output for this section. |
| 61 |
* |
| 62 |
* @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared |
| 63 |
*/ |
| 64 |
public function clear(?int $lines = null): void |
| 65 |
{ |
| 66 |
if (!$this->content || !$this->isDecorated()) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if ($lines) { |
| 71 |
array_splice($this->content, -$lines); |
| 72 |
} else { |
| 73 |
$lines = $this->lines; |
| 74 |
$this->content = []; |
| 75 |
} |
| 76 |
|
| 77 |
$this->lines -= $lines; |
| 78 |
|
| 79 |
parent::doWrite($this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $lines) : $lines), false); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Overwrites the previous output with a new message. |
| 84 |
*/ |
| 85 |
public function overwrite(string|iterable $message): void |
| 86 |
{ |
| 87 |
if (!$this->content || !$this->isDecorated()) { |
| 88 |
$this->writeln($message); |
| 89 |
|
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Replace own content and write everything in a single cursor-up + erase |
| 94 |
// pass, to avoid the flicker (and the line-eating artifacts on some |
| 95 |
// terminals) caused by calling clear() then writeln() back-to-back. |
| 96 |
$linesCleared = $this->lines; |
| 97 |
$this->content = []; |
| 98 |
$this->lines = 0; |
| 99 |
|
| 100 |
if (!is_iterable($message)) { |
| 101 |
$message = [$message]; |
| 102 |
} |
| 103 |
|
| 104 |
foreach ($message as $line) { |
| 105 |
$this->addContent($this->getFormatter()->format($line) ?? '', true); |
| 106 |
} |
| 107 |
|
| 108 |
$erasedContent = $this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $linesCleared) : $linesCleared); |
| 109 |
|
| 110 |
parent::doWrite($this->getVisibleContent(), false); |
| 111 |
parent::doWrite($erasedContent, false); |
| 112 |
} |
| 113 |
|
| 114 |
public function getContent(): string |
| 115 |
{ |
| 116 |
return implode('', $this->content); |
| 117 |
} |
| 118 |
|
| 119 |
public function getVisibleContent(): string |
| 120 |
{ |
| 121 |
if (0 === $this->maxHeight) { |
| 122 |
return $this->getContent(); |
| 123 |
} |
| 124 |
|
| 125 |
return implode('', \array_slice($this->content, -$this->maxHeight)); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @internal |
| 130 |
*/ |
| 131 |
public function addContent(string $input, bool $newline = true): int |
| 132 |
{ |
| 133 |
$width = $this->terminal->getWidth(); |
| 134 |
$lines = explode(\PHP_EOL, $input); |
| 135 |
$linesAdded = 0; |
| 136 |
$count = \count($lines) - 1; |
| 137 |
foreach ($lines as $i => $lineContent) { |
| 138 |
// re-add the line break (that has been removed in the above `explode()` for |
| 139 |
// - every line that is not the last line |
| 140 |
// - if $newline is required, also add it to the last line |
| 141 |
if ($i < $count || $newline) { |
| 142 |
$lineContent .= \PHP_EOL; |
| 143 |
} |
| 144 |
|
| 145 |
// skip line if there is no text (or newline for that matter) |
| 146 |
if ('' === $lineContent) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
// For the first line, check if the previous line (last entry of `$this->content`) |
| 151 |
// needs to be continued (i.e. does not end with a line break). |
| 152 |
if (0 === $i |
| 153 |
&& (false !== $lastLine = end($this->content)) |
| 154 |
&& !str_ends_with($lastLine, \PHP_EOL) |
| 155 |
) { |
| 156 |
// deduct the line count of the previous line |
| 157 |
$this->lines -= (int) ceil($this->getDisplayLength($lastLine) / $width) ?: 1; |
| 158 |
// concatenate previous and new line |
| 159 |
$lineContent = $lastLine.$lineContent; |
| 160 |
// replace last entry of `$this->content` with the new expanded line |
| 161 |
array_splice($this->content, -1, 1, $lineContent); |
| 162 |
} else { |
| 163 |
// otherwise just add the new content |
| 164 |
$this->content[] = $lineContent; |
| 165 |
} |
| 166 |
|
| 167 |
$linesAdded += (int) ceil($this->getDisplayLength($lineContent) / $width) ?: 1; |
| 168 |
} |
| 169 |
|
| 170 |
$this->lines += $linesAdded; |
| 171 |
|
| 172 |
return $linesAdded; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @internal |
| 177 |
*/ |
| 178 |
public function addNewLineOfInputSubmit(): void |
| 179 |
{ |
| 180 |
$this->content[] = \PHP_EOL; |
| 181 |
++$this->lines; |
| 182 |
} |
| 183 |
|
| 184 |
protected function doWrite(string $message, bool $newline): void |
| 185 |
{ |
| 186 |
// Simulate newline behavior for consistent output formatting, avoiding extra logic |
| 187 |
if (!$newline && str_ends_with($message, \PHP_EOL)) { |
| 188 |
$message = substr($message, 0, -\strlen(\PHP_EOL)); |
| 189 |
$newline = true; |
| 190 |
} |
| 191 |
|
| 192 |
if (!$this->isDecorated()) { |
| 193 |
parent::doWrite($message, $newline); |
| 194 |
|
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
// Check if the previous line (last entry of `$this->content`) needs to be continued |
| 199 |
// (i.e. does not end with a line break). In which case, it needs to be erased first. |
| 200 |
$linesToClear = $deleteLastLine = ($lastLine = end($this->content) ?: '') && !str_ends_with($lastLine, \PHP_EOL) ? 1 : 0; |
| 201 |
|
| 202 |
$linesAdded = $this->addContent($message, $newline); |
| 203 |
|
| 204 |
if ($lineOverflow = $this->maxHeight > 0 && $this->lines > $this->maxHeight) { |
| 205 |
// on overflow, clear the whole section and redraw again (to remove the first lines) |
| 206 |
$linesToClear = $this->maxHeight; |
| 207 |
} |
| 208 |
|
| 209 |
$erasedContent = $this->popStreamContentUntilCurrentSection($linesToClear); |
| 210 |
|
| 211 |
if ($lineOverflow) { |
| 212 |
// redraw existing lines of the section |
| 213 |
$previousLinesOfSection = \array_slice($this->content, $this->lines - $this->maxHeight, $this->maxHeight - $linesAdded); |
| 214 |
parent::doWrite(implode('', $previousLinesOfSection), false); |
| 215 |
} |
| 216 |
|
| 217 |
// if the last line was removed, re-print its content together with the new content. |
| 218 |
// otherwise, just print the new content. |
| 219 |
parent::doWrite($deleteLastLine ? $lastLine.$message : $message, true); |
| 220 |
parent::doWrite($erasedContent, false); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits |
| 225 |
* current section. Then it erases content it crawled through. Optionally, it erases part of current section too. |
| 226 |
*/ |
| 227 |
private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string |
| 228 |
{ |
| 229 |
$numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection; |
| 230 |
$erasedContent = []; |
| 231 |
|
| 232 |
foreach ($this->sections as $section) { |
| 233 |
if ($section === $this) { |
| 234 |
break; |
| 235 |
} |
| 236 |
|
| 237 |
$numberOfLinesToClear += $section->maxHeight ? min($section->lines, $section->maxHeight) : $section->lines; |
| 238 |
if ('' !== $sectionContent = $section->getVisibleContent()) { |
| 239 |
if (!str_ends_with($sectionContent, \PHP_EOL)) { |
| 240 |
$sectionContent .= \PHP_EOL; |
| 241 |
} |
| 242 |
$erasedContent[] = $sectionContent; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if ($numberOfLinesToClear > 0) { |
| 247 |
// move cursor up n lines |
| 248 |
parent::doWrite(\sprintf("\x1b[%dA", $numberOfLinesToClear), false); |
| 249 |
// erase to end of screen |
| 250 |
parent::doWrite("\x1b[0J", false); |
| 251 |
} |
| 252 |
|
| 253 |
return implode('', array_reverse($erasedContent)); |
| 254 |
} |
| 255 |
|
| 256 |
private function getDisplayLength(string $text): int |
| 257 |
{ |
| 258 |
return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text))); |
| 259 |
} |
| 260 |
} |