| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 20 |
> |
| 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 |
|
| 32 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function setMaxHeight(int $maxHeight): void |
| 49 |
{ |
| 50 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 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 |
|
| 139 |
|
| 140 |
|
| 141 |
if ($i < $count || $newline) { |
| 142 |
$lineContent .= \PHP_EOL; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
if ('' === $lineContent) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if (0 === $i |
| 153 |
&& (false !== $lastLine = end($this->content)) |
| 154 |
&& !str_ends_with($lastLine, \PHP_EOL) |
| 155 |
) { |
| 156 |
|
| 157 |
$this->lines -= (int) ceil($this->getDisplayLength($lastLine) / $width) ?: 1; |
| 158 |
|
| 159 |
$lineContent = $lastLine.$lineContent; |
| 160 |
|
| 161 |
array_splice($this->content, -1, 1, $lineContent); |
| 162 |
} else { |
| 163 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 199 |
|
| 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 |
|
| 206 |
$linesToClear = $this->maxHeight; |
| 207 |
} |
| 208 |
|
| 209 |
$erasedContent = $this->popStreamContentUntilCurrentSection($linesToClear); |
| 210 |
|
| 211 |
if ($lineOverflow) { |
| 212 |
|
| 213 |
$previousLinesOfSection = \array_slice($this->content, $this->lines - $this->maxHeight, $this->maxHeight - $linesAdded); |
| 214 |
parent::doWrite(implode('', $previousLinesOfSection), false); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
parent::doWrite($deleteLastLine ? $lastLine.$message : $message, true); |
| 220 |
parent::doWrite($erasedContent, false); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 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 |
|
| 248 |
parent::doWrite(\sprintf("\x1b[%dA", $numberOfLinesToClear), false); |
| 249 |
|
| 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 |
} |