Repository navigation
Expand file tree
/
Copy pathInteractiveComponentRenderer.php
More file actions
349 lines (270 loc) · 10.6 KB
/
Copy pathInteractiveComponentRenderer.php
File metadata and controls
349 lines (270 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
declare(strict_types=1);
namespace Tempest\Console\Components;
use Fiber;
use Tempest\Console\Console;
use Tempest\Console\Exceptions\InterruptException;
use Tempest\Console\HandlesKey;
use Tempest\Console\InteractiveConsoleComponent;
use Tempest\Console\Key;
use Tempest\Console\Terminal\Terminal;
use Tempest\Reflection\ClassReflector;
use Tempest\Reflection\MethodReflector;
use Tempest\Support\Arr;
use Tempest\Validation\Validator;
use function Tempest\Support\arr;
final class InteractiveComponentRenderer
{
private array $afterRenderCallbacks = [];
private array $validationErrors = [];
private bool $shouldRerender = true;
/** @var list<string> */
private array $pendingKeys = [];
private string $pendingInput = '';
public function __construct(
private readonly Validator $validator,
) {}
public function render(Console $console, InteractiveConsoleComponent $component, array $validation = []): mixed
{
$clone = clone $this;
return $clone->renderComponent($console, $component, $validation);
}
private function renderComponent(Console $console, InteractiveConsoleComponent $component, array $validation = []): mixed
{
$terminal = $this->createTerminal($console);
$fibers = [
new Fiber(fn () => $this->applyKey($component, $console, $validation)),
new Fiber(fn () => $this->renderFrames($component, $terminal)),
];
try {
while ($fibers !== []) {
foreach ($fibers as $key => $fiber) {
if (! $fiber->isStarted()) {
$fiber->start();
}
$fiber->resume();
if ($fiber->isTerminated()) {
unset($fibers[$key]);
if (! is_null($return = $fiber->getReturn())) {
return $return;
}
}
}
// If we're running within a fiber, we'll suspend here as well so that the parent can continue
// This is needed for our testing helper
if (Fiber::getCurrent() instanceof Fiber) {
Fiber::suspend();
}
}
} finally {
$this->closeTerminal($terminal);
}
return null;
}
private function applyKey(InteractiveConsoleComponent $component, Console $console, array $validation): mixed
{
[$keyBindings, $inputHandlers] = $this->resolveHandlers($component);
while (true) {
while ($callback = array_shift($this->afterRenderCallbacks)) {
$callback($component);
}
usleep(50);
$key = $this->readKey($console);
// If there's no keypress, continue.
if ($key === '') {
Fiber::suspend();
continue;
}
// Otherwise, we will re-render after processing the key.
$this->shouldRerender = true;
if ($component->getState() === ComponentState::BLOCKED) {
continue;
}
/** @var MethodReflector[] $handlersForKey */
$handlersForKey = $keyBindings[$key] ?? [];
// If we have multiple handlers, we put the ones that return nothing
// first because the ones that return something will be overridden otherwise.
usort($handlersForKey, fn (MethodReflector $a, MethodReflector $b) => $b->getReturnType()->equals('void') <=> $a->getReturnType()->equals('void'));
// CTRL+C and CTRL+D means we exit the CLI, but only if there is no custom
// handler. When we exit, we want one last render to display pretty
// styles, so we will throw the exception in the next loop.
if ($handlersForKey === [] && ($key === Key::CTRL_C->value || $key === Key::CTRL_D->value)) {
$component->setState(ComponentState::CANCELLED);
$this->afterRenderCallbacks[] = fn () => throw new InterruptException();
$this->shouldRerender = true;
Fiber::suspend();
continue;
}
$return = null;
// If we have handlers for that key, apply them.
foreach ($handlersForKey as $handler) {
$return ??= $handler->invokeArgs($component);
}
// If we didn't have any handler for the key,
// we call catch-all handlers.
if ($handlersForKey === []) {
foreach ($inputHandlers as $handler) {
$return ??= $handler->invokeArgs($component, [$key]);
}
}
// If nothing's returned, we can continue waiting for the next key press
if ($return === null) {
Fiber::suspend();
continue;
}
// If something's returned, we'll need to validate the result
$this->validationErrors = [];
$validationError = $this->validate($return, $validation);
// If invalid, we'll remember the validation message and continue
if ($validationError !== null) {
$component->setState(ComponentState::ERROR);
$this->validationErrors[] = $validationError;
Fiber::suspend();
continue;
}
// The component is done, we can re-render and return.
$component->setState(ComponentState::DONE);
Fiber::suspend();
// If valid, we can return
return $return;
}
}
private function readKey(Console $console): string
{
while ($this->pendingKeys === []) {
$input = $console->read(16);
if ($input === '') {
return '';
}
$this->pendingInput .= $input;
$this->pendingKeys = $this->splitKeys($this->pendingInput);
}
return array_shift($this->pendingKeys);
}
/** @return list<string> */
private function splitKeys(string &$input): array
{
/** @var null|list<string> $knownKeys */
static $knownKeys = null;
if ($knownKeys === null) {
$knownKeys = array_map(
static fn (Key $key): string => $key->value,
Key::cases(),
);
usort($knownKeys, static fn (string $left, string $right): int => strlen($right) <=> strlen($left));
}
$keys = [];
while ($input !== '') {
foreach ($knownKeys as $knownKey) {
if (! str_starts_with($input, $knownKey)) {
continue;
}
$keys[] = $knownKey;
$input = substr($input, strlen($knownKey));
continue 2;
}
if (str_starts_with($input, "\e")) {
$matches = [];
preg_match('/^\e(?:\[[0-?]*[ -\/]*[@-~]|O.)/s', $input, $matches);
$key = $matches[0] ?? mb_substr($input, 0, 2);
} else {
$length = $this->getUtf8CharacterLength($input);
if (strlen($input) < $length) {
break;
}
$key = substr($input, 0, $length);
}
$keys[] = $key;
$input = substr($input, strlen($key));
}
return $keys;
}
private function getUtf8CharacterLength(string $input): int
{
$firstByte = ord($input[0]);
return match (true) {
($firstByte & 0x80) === 0 => 1,
($firstByte & 0xE0) === 0xC0 => 2,
($firstByte & 0xF0) === 0xE0 => 3,
($firstByte & 0xF8) === 0xF0 => 4,
default => 1,
};
}
private function renderFrames(InteractiveConsoleComponent $component, Terminal $terminal): mixed
{
while (true) {
usleep(100);
// If there are no updates,
// we won't spend time re-rendering the same frame
if (! $this->shouldRerender) {
Fiber::suspend();
continue;
}
$this->shouldRerender = false;
// Rerender the frames, it could be one or more
$frames = $terminal->render(
component: $component,
validationErrors: $this->validationErrors,
);
// Looping over the frames will display them
// (this happens within the Terminal class, might need to refactor)
// We suspend between each frame to allow key press interruptions
foreach ($frames as $frame) {
Fiber::suspend();
}
$return = $frames->getReturn();
if ($return !== null) {
return $return;
}
}
}
private function resolveHandlers(InteractiveConsoleComponent $component): array
{
/** @var \Tempest\Reflection\MethodReflector[][] $keyBindings */
$keyBindings = [];
$inputHandlers = [];
foreach (new ClassReflector($component)->getPublicMethods() as $method) {
foreach ($method->getAttributes(HandlesKey::class) as $handlesKey) {
if ($handlesKey->key === null) {
$inputHandlers[] = $method;
} else {
$keyBindings[$handlesKey->key->value][] = $method;
}
}
}
return [$keyBindings, $inputHandlers];
}
/**
* @param \Tempest\Validation\Rule[] $validation
*/
private function validate(mixed $value, array $validation): ?string
{
$failingRules = $this->validator->validateValue($value, $validation);
if ($failingRules === []) {
return null;
}
return $this->validator->getErrorMessage(
rule: Arr\first($failingRules),
);
}
public function isComponentSupported(Console $console, InteractiveConsoleComponent $component): bool
{
if (! arr($component->extensions ?? [])->every(fn (string $ext) => extension_loaded($ext))) {
return false;
}
return Terminal::supportsTty();
}
private function createTerminal(Console $console): Terminal
{
$terminal = new Terminal($console);
$terminal->cursor->clearAfter();
stream_set_blocking(STDIN, false);
return $terminal;
}
private function closeTerminal(Terminal $terminal): void
{
$terminal->placeCursorToEnd();
$terminal->switchToNormalMode();
stream_set_blocking(STDIN, true);
}
}