| 148 |
lars |
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 |
|
|
|
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 <pdples@gmail.com>
|
|
|
20 |
* @author Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
|
|
|
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 |
$existingContent = $this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $this->lines) : $this->lines);
|
|
|
52 |
|
|
|
53 |
$this->maxHeight = $maxHeight;
|
|
|
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)
|
|
|
65 |
{
|
|
|
66 |
if (empty($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)
|
|
|
86 |
{
|
|
|
87 |
$this->clear();
|
|
|
88 |
$this->writeln($message);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function getContent(): string
|
|
|
92 |
{
|
|
|
93 |
return implode('', $this->content);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function getVisibleContent(): string
|
|
|
97 |
{
|
|
|
98 |
if (0 === $this->maxHeight) {
|
|
|
99 |
return $this->getContent();
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return implode('', \array_slice($this->content, -$this->maxHeight));
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* @internal
|
|
|
107 |
*/
|
|
|
108 |
public function addContent(string $input, bool $newline = true): int
|
|
|
109 |
{
|
|
|
110 |
$width = $this->terminal->getWidth();
|
|
|
111 |
$lines = explode(\PHP_EOL, $input);
|
|
|
112 |
$linesAdded = 0;
|
|
|
113 |
$count = \count($lines) - 1;
|
|
|
114 |
foreach ($lines as $i => $lineContent) {
|
|
|
115 |
// re-add the line break (that has been removed in the above `explode()` for
|
|
|
116 |
// - every line that is not the last line
|
|
|
117 |
// - if $newline is required, also add it to the last line
|
| 688 |
lars |
118 |
// - if it's not new line, but input ending with `\PHP_EOL`
|
|
|
119 |
if ($i < $count || $newline || str_ends_with($input, \PHP_EOL)) {
|
| 148 |
lars |
120 |
$lineContent .= \PHP_EOL;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// skip line if there is no text (or newline for that matter)
|
|
|
124 |
if ('' === $lineContent) {
|
|
|
125 |
continue;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// For the first line, check if the previous line (last entry of `$this->content`)
|
|
|
129 |
// needs to be continued (i.e. does not end with a line break).
|
|
|
130 |
if (0 === $i
|
|
|
131 |
&& (false !== $lastLine = end($this->content))
|
|
|
132 |
&& !str_ends_with($lastLine, \PHP_EOL)
|
|
|
133 |
) {
|
|
|
134 |
// deduct the line count of the previous line
|
|
|
135 |
$this->lines -= (int) ceil($this->getDisplayLength($lastLine) / $width) ?: 1;
|
|
|
136 |
// concatenate previous and new line
|
|
|
137 |
$lineContent = $lastLine.$lineContent;
|
|
|
138 |
// replace last entry of `$this->content` with the new expanded line
|
|
|
139 |
array_splice($this->content, -1, 1, $lineContent);
|
|
|
140 |
} else {
|
|
|
141 |
// otherwise just add the new content
|
|
|
142 |
$this->content[] = $lineContent;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
$linesAdded += (int) ceil($this->getDisplayLength($lineContent) / $width) ?: 1;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
$this->lines += $linesAdded;
|
|
|
149 |
|
|
|
150 |
return $linesAdded;
|
|
|
151 |
}
|
|
|
152 |
|
| 688 |
lars |
153 |
/**
|
|
|
154 |
* @internal
|
|
|
155 |
*/
|
|
|
156 |
public function addNewLineOfInputSubmit()
|
|
|
157 |
{
|
|
|
158 |
$this->content[] = \PHP_EOL;
|
|
|
159 |
++$this->lines;
|
|
|
160 |
}
|
|
|
161 |
|
| 148 |
lars |
162 |
protected function doWrite(string $message, bool $newline)
|
|
|
163 |
{
|
|
|
164 |
if (!$this->isDecorated()) {
|
|
|
165 |
parent::doWrite($message, $newline);
|
|
|
166 |
|
|
|
167 |
return;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// Check if the previous line (last entry of `$this->content`) needs to be continued
|
|
|
171 |
// (i.e. does not end with a line break). In which case, it needs to be erased first.
|
|
|
172 |
$linesToClear = $deleteLastLine = ($lastLine = end($this->content) ?: '') && !str_ends_with($lastLine, \PHP_EOL) ? 1 : 0;
|
|
|
173 |
|
|
|
174 |
$linesAdded = $this->addContent($message, $newline);
|
|
|
175 |
|
|
|
176 |
if ($lineOverflow = $this->maxHeight > 0 && $this->lines > $this->maxHeight) {
|
|
|
177 |
// on overflow, clear the whole section and redraw again (to remove the first lines)
|
|
|
178 |
$linesToClear = $this->maxHeight;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$erasedContent = $this->popStreamContentUntilCurrentSection($linesToClear);
|
|
|
182 |
|
|
|
183 |
if ($lineOverflow) {
|
|
|
184 |
// redraw existing lines of the section
|
|
|
185 |
$previousLinesOfSection = \array_slice($this->content, $this->lines - $this->maxHeight, $this->maxHeight - $linesAdded);
|
|
|
186 |
parent::doWrite(implode('', $previousLinesOfSection), false);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
// if the last line was removed, re-print its content together with the new content.
|
|
|
190 |
// otherwise, just print the new content.
|
|
|
191 |
parent::doWrite($deleteLastLine ? $lastLine.$message : $message, true);
|
|
|
192 |
parent::doWrite($erasedContent, false);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits
|
|
|
197 |
* current section. Then it erases content it crawled through. Optionally, it erases part of current section too.
|
|
|
198 |
*/
|
|
|
199 |
private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string
|
|
|
200 |
{
|
|
|
201 |
$numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection;
|
|
|
202 |
$erasedContent = [];
|
|
|
203 |
|
|
|
204 |
foreach ($this->sections as $section) {
|
|
|
205 |
if ($section === $this) {
|
|
|
206 |
break;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
$numberOfLinesToClear += $section->lines;
|
|
|
210 |
if ('' !== $sectionContent = $section->getVisibleContent()) {
|
|
|
211 |
if (!str_ends_with($sectionContent, \PHP_EOL)) {
|
|
|
212 |
$sectionContent .= \PHP_EOL;
|
|
|
213 |
}
|
|
|
214 |
$erasedContent[] = $sectionContent;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
if ($numberOfLinesToClear > 0) {
|
|
|
219 |
// move cursor up n lines
|
|
|
220 |
parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false);
|
|
|
221 |
// erase to end of screen
|
|
|
222 |
parent::doWrite("\x1b[0J", false);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
return implode('', array_reverse($erasedContent));
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
private function getDisplayLength(string $text): int
|
|
|
229 |
{
|
|
|
230 |
return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text)));
|
|
|
231 |
}
|
|
|
232 |
}
|