123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace Symfony\Component\Console\Output;
- use Symfony\Component\Console\Formatter\OutputFormatterInterface;
- use Symfony\Component\Console\Helper\Helper;
- use Symfony\Component\Console\Terminal;
- class ConsoleSectionOutput extends StreamOutput
- {
- private $content = [];
- private $lines = 0;
- private $sections;
- private $terminal;
-
- public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter)
- {
- parent::__construct($stream, $verbosity, $decorated, $formatter);
- array_unshift($sections, $this);
- $this->sections = &$sections;
- $this->terminal = new Terminal();
- }
-
- public function clear(int $lines = null)
- {
- if (empty($this->content) || !$this->isDecorated()) {
- return;
- }
- if ($lines) {
- array_splice($this->content, -($lines * 2));
- } else {
- $lines = $this->lines;
- $this->content = [];
- }
- $this->lines -= $lines;
- parent::doWrite($this->popStreamContentUntilCurrentSection($lines), false);
- }
-
- public function overwrite($message)
- {
- $this->clear();
- $this->writeln($message);
- }
- public function getContent(): string
- {
- return implode('', $this->content);
- }
-
- public function addContent(string $input)
- {
- foreach (explode(PHP_EOL, $input) as $lineContent) {
- $this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1;
- $this->content[] = $lineContent;
- $this->content[] = PHP_EOL;
- }
- }
-
- protected function doWrite($message, $newline)
- {
- if (!$this->isDecorated()) {
- return parent::doWrite($message, $newline);
- }
- $erasedContent = $this->popStreamContentUntilCurrentSection();
- $this->addContent($message);
- parent::doWrite($message, true);
- parent::doWrite($erasedContent, false);
- }
-
- private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string
- {
- $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection;
- $erasedContent = [];
- foreach ($this->sections as $section) {
- if ($section === $this) {
- break;
- }
- $numberOfLinesToClear += $section->lines;
- $erasedContent[] = $section->getContent();
- }
- if ($numberOfLinesToClear > 0) {
-
- parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false);
-
- parent::doWrite("\x1b[0J", false);
- }
- return implode('', array_reverse($erasedContent));
- }
- private function getDisplayLength(string $text): string
- {
- return Helper::strlenWithoutDecoration($this->getFormatter(), str_replace("\t", ' ', $text));
- }
- }
|