Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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
118
            if ($i < $count || $newline) {
119
                $lineContent .= \PHP_EOL;
120
            }
121
 
122
            // skip line if there is no text (or newline for that matter)
123
            if ('' === $lineContent) {
124
                continue;
125
            }
126
 
127
            // For the first line, check if the previous line (last entry of `$this->content`)
128
            // needs to be continued (i.e. does not end with a line break).
129
            if (0 === $i
130
                && (false !== $lastLine = end($this->content))
131
                && !str_ends_with($lastLine, \PHP_EOL)
132
            ) {
133
                // deduct the line count of the previous line
134
                $this->lines -= (int) ceil($this->getDisplayLength($lastLine) / $width) ?: 1;
135
                // concatenate previous and new line
136
                $lineContent = $lastLine.$lineContent;
137
                // replace last entry of `$this->content` with the new expanded line
138
                array_splice($this->content, -1, 1, $lineContent);
139
            } else {
140
                // otherwise just add the new content
141
                $this->content[] = $lineContent;
142
            }
143
 
144
            $linesAdded += (int) ceil($this->getDisplayLength($lineContent) / $width) ?: 1;
145
        }
146
 
147
        $this->lines += $linesAdded;
148
 
149
        return $linesAdded;
150
    }
151
 
152
    protected function doWrite(string $message, bool $newline)
153
    {
154
        if (!$this->isDecorated()) {
155
            parent::doWrite($message, $newline);
156
 
157
            return;
158
        }
159
 
160
        // Check if the previous line (last entry of `$this->content`) needs to be continued
161
        // (i.e. does not end with a line break). In which case, it needs to be erased first.
162
        $linesToClear = $deleteLastLine = ($lastLine = end($this->content) ?: '') && !str_ends_with($lastLine, \PHP_EOL) ? 1 : 0;
163
 
164
        $linesAdded = $this->addContent($message, $newline);
165
 
166
        if ($lineOverflow = $this->maxHeight > 0 && $this->lines > $this->maxHeight) {
167
            // on overflow, clear the whole section and redraw again (to remove the first lines)
168
            $linesToClear = $this->maxHeight;
169
        }
170
 
171
        $erasedContent = $this->popStreamContentUntilCurrentSection($linesToClear);
172
 
173
        if ($lineOverflow) {
174
            // redraw existing lines of the section
175
            $previousLinesOfSection = \array_slice($this->content, $this->lines - $this->maxHeight, $this->maxHeight - $linesAdded);
176
            parent::doWrite(implode('', $previousLinesOfSection), false);
177
        }
178
 
179
        // if the last line was removed, re-print its content together with the new content.
180
        // otherwise, just print the new content.
181
        parent::doWrite($deleteLastLine ? $lastLine.$message : $message, true);
182
        parent::doWrite($erasedContent, false);
183
    }
184
 
185
    /**
186
     * At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits
187
     * current section. Then it erases content it crawled through. Optionally, it erases part of current section too.
188
     */
189
    private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string
190
    {
191
        $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection;
192
        $erasedContent = [];
193
 
194
        foreach ($this->sections as $section) {
195
            if ($section === $this) {
196
                break;
197
            }
198
 
199
            $numberOfLinesToClear += $section->lines;
200
            if ('' !== $sectionContent = $section->getVisibleContent()) {
201
                if (!str_ends_with($sectionContent, \PHP_EOL)) {
202
                    $sectionContent .= \PHP_EOL;
203
                }
204
                $erasedContent[] = $sectionContent;
205
            }
206
        }
207
 
208
        if ($numberOfLinesToClear > 0) {
209
            // move cursor up n lines
210
            parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false);
211
            // erase to end of screen
212
            parent::doWrite("\x1b[0J", false);
213
        }
214
 
215
        return implode('', array_reverse($erasedContent));
216
    }
217
 
218
    private function getDisplayLength(string $text): int
219
    {
220
        return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", '        ', $text)));
221
    }
222
}