Subversion-Projekte lars-tiefland.laravel_shop

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;
13
 
14
use Symfony\Component\Console\Output\OutputInterface;
15
 
16
/**
17
 * @author Pierre du Plessis <pdples@gmail.com>
18
 */
19
final class Cursor
20
{
21
    private OutputInterface $output;
22
    private $input;
23
 
24
    /**
25
     * @param resource|null $input
26
     */
27
    public function __construct(OutputInterface $output, $input = null)
28
    {
29
        $this->output = $output;
30
        $this->input = $input ?? (\defined('STDIN') ? \STDIN : fopen('php://input', 'r+'));
31
    }
32
 
33
    /**
34
     * @return $this
35
     */
36
    public function moveUp(int $lines = 1): static
37
    {
38
        $this->output->write(sprintf("\x1b[%dA", $lines));
39
 
40
        return $this;
41
    }
42
 
43
    /**
44
     * @return $this
45
     */
46
    public function moveDown(int $lines = 1): static
47
    {
48
        $this->output->write(sprintf("\x1b[%dB", $lines));
49
 
50
        return $this;
51
    }
52
 
53
    /**
54
     * @return $this
55
     */
56
    public function moveRight(int $columns = 1): static
57
    {
58
        $this->output->write(sprintf("\x1b[%dC", $columns));
59
 
60
        return $this;
61
    }
62
 
63
    /**
64
     * @return $this
65
     */
66
    public function moveLeft(int $columns = 1): static
67
    {
68
        $this->output->write(sprintf("\x1b[%dD", $columns));
69
 
70
        return $this;
71
    }
72
 
73
    /**
74
     * @return $this
75
     */
76
    public function moveToColumn(int $column): static
77
    {
78
        $this->output->write(sprintf("\x1b[%dG", $column));
79
 
80
        return $this;
81
    }
82
 
83
    /**
84
     * @return $this
85
     */
86
    public function moveToPosition(int $column, int $row): static
87
    {
88
        $this->output->write(sprintf("\x1b[%d;%dH", $row + 1, $column));
89
 
90
        return $this;
91
    }
92
 
93
    /**
94
     * @return $this
95
     */
96
    public function savePosition(): static
97
    {
98
        $this->output->write("\x1b7");
99
 
100
        return $this;
101
    }
102
 
103
    /**
104
     * @return $this
105
     */
106
    public function restorePosition(): static
107
    {
108
        $this->output->write("\x1b8");
109
 
110
        return $this;
111
    }
112
 
113
    /**
114
     * @return $this
115
     */
116
    public function hide(): static
117
    {
118
        $this->output->write("\x1b[?25l");
119
 
120
        return $this;
121
    }
122
 
123
    /**
124
     * @return $this
125
     */
126
    public function show(): static
127
    {
128
        $this->output->write("\x1b[?25h\x1b[?0c");
129
 
130
        return $this;
131
    }
132
 
133
    /**
134
     * Clears all the output from the current line.
135
     *
136
     * @return $this
137
     */
138
    public function clearLine(): static
139
    {
140
        $this->output->write("\x1b[2K");
141
 
142
        return $this;
143
    }
144
 
145
    /**
146
     * Clears all the output from the current line after the current position.
147
     */
148
    public function clearLineAfter(): self
149
    {
150
        $this->output->write("\x1b[K");
151
 
152
        return $this;
153
    }
154
 
155
    /**
156
     * Clears all the output from the cursors' current position to the end of the screen.
157
     *
158
     * @return $this
159
     */
160
    public function clearOutput(): static
161
    {
162
        $this->output->write("\x1b[0J");
163
 
164
        return $this;
165
    }
166
 
167
    /**
168
     * Clears the entire screen.
169
     *
170
     * @return $this
171
     */
172
    public function clearScreen(): static
173
    {
174
        $this->output->write("\x1b[2J");
175
 
176
        return $this;
177
    }
178
 
179
    /**
180
     * Returns the current cursor position as x,y coordinates.
181
     */
182
    public function getCurrentPosition(): array
183
    {
184
        static $isTtySupported;
185
 
186
        if (!$isTtySupported ??= '/' === \DIRECTORY_SEPARATOR && stream_isatty(\STDOUT)) {
187
            return [1, 1];
188
        }
189
 
190
        $sttyMode = shell_exec('stty -g');
191
        shell_exec('stty -icanon -echo');
192
 
193
        @fwrite($this->input, "\033[6n");
194
 
195
        $code = trim(fread($this->input, 1024));
196
 
197
        shell_exec(sprintf('stty %s', $sttyMode));
198
 
199
        sscanf($code, "\033[%d;%dR", $row, $col);
200
 
201
        return [$col, $row];
202
    }
203
}