BufferedOutput.php
2 years ago
ConsoleOutput.php
1 year ago
ConsoleOutputInterface.php
2 years ago
ConsoleSectionOutput.php
1 year ago
NullOutput.php
1 year ago
Output.php
1 year ago
OutputInterface.php
1 year ago
StreamOutput.php
1 year ago
TrimmedBufferOutput.php
1 year ago
ConsoleSectionOutput.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\Console\Output; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 14 | use Matomo\Dependencies\Symfony\Component\Console\Helper\Helper; |
| 15 | use Matomo\Dependencies\Symfony\Component\Console\Terminal; |
| 16 | /** |
| 17 | * @author Pierre du Plessis <pdples@gmail.com> |
| 18 | * @author Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com> |
| 19 | */ |
| 20 | class ConsoleSectionOutput extends StreamOutput |
| 21 | { |
| 22 | private $content = []; |
| 23 | private $lines = 0; |
| 24 | private $sections; |
| 25 | private $terminal; |
| 26 | /** |
| 27 | * @param resource $stream |
| 28 | * @param ConsoleSectionOutput[] $sections |
| 29 | */ |
| 30 | public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) |
| 31 | { |
| 32 | parent::__construct($stream, $verbosity, $decorated, $formatter); |
| 33 | array_unshift($sections, $this); |
| 34 | $this->sections =& $sections; |
| 35 | $this->terminal = new Terminal(); |
| 36 | } |
| 37 | /** |
| 38 | * Clears previous output for this section. |
| 39 | * |
| 40 | * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared |
| 41 | */ |
| 42 | public function clear(?int $lines = null) |
| 43 | { |
| 44 | if (empty($this->content) || !$this->isDecorated()) { |
| 45 | return; |
| 46 | } |
| 47 | if ($lines) { |
| 48 | array_splice($this->content, -($lines * 2)); |
| 49 | // Multiply lines by 2 to cater for each new line added between content |
| 50 | } else { |
| 51 | $lines = $this->lines; |
| 52 | $this->content = []; |
| 53 | } |
| 54 | $this->lines -= $lines; |
| 55 | parent::doWrite($this->popStreamContentUntilCurrentSection($lines), \false); |
| 56 | } |
| 57 | /** |
| 58 | * Overwrites the previous output with a new message. |
| 59 | * |
| 60 | * @param array|string $message |
| 61 | */ |
| 62 | public function overwrite($message) |
| 63 | { |
| 64 | $this->clear(); |
| 65 | $this->writeln($message); |
| 66 | } |
| 67 | public function getContent() : string |
| 68 | { |
| 69 | return implode('', $this->content); |
| 70 | } |
| 71 | /** |
| 72 | * @internal |
| 73 | */ |
| 74 | public function addContent(string $input) |
| 75 | { |
| 76 | foreach (explode(\PHP_EOL, $input) as $lineContent) { |
| 77 | $this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1; |
| 78 | $this->content[] = $lineContent; |
| 79 | $this->content[] = \PHP_EOL; |
| 80 | } |
| 81 | } |
| 82 | /** |
| 83 | * {@inheritdoc} |
| 84 | */ |
| 85 | protected function doWrite(string $message, bool $newline) |
| 86 | { |
| 87 | if (!$this->isDecorated()) { |
| 88 | parent::doWrite($message, $newline); |
| 89 | return; |
| 90 | } |
| 91 | $erasedContent = $this->popStreamContentUntilCurrentSection(); |
| 92 | $this->addContent($message); |
| 93 | parent::doWrite($message, \true); |
| 94 | parent::doWrite($erasedContent, \false); |
| 95 | } |
| 96 | /** |
| 97 | * At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits |
| 98 | * current section. Then it erases content it crawled through. Optionally, it erases part of current section too. |
| 99 | */ |
| 100 | private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0) : string |
| 101 | { |
| 102 | $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection; |
| 103 | $erasedContent = []; |
| 104 | foreach ($this->sections as $section) { |
| 105 | if ($section === $this) { |
| 106 | break; |
| 107 | } |
| 108 | $numberOfLinesToClear += $section->lines; |
| 109 | $erasedContent[] = $section->getContent(); |
| 110 | } |
| 111 | if ($numberOfLinesToClear > 0) { |
| 112 | // move cursor up n lines |
| 113 | parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), \false); |
| 114 | // erase to end of screen |
| 115 | parent::doWrite("\x1b[0J", \false); |
| 116 | } |
| 117 | return implode('', array_reverse($erasedContent)); |
| 118 | } |
| 119 | private function getDisplayLength(string $text) : int |
| 120 | { |
| 121 | return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text))); |
| 122 | } |
| 123 | } |
| 124 |