This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SttyAutocompletingReader.php
445 lines (398 loc) · 11.9 KB
/
SttyAutocompletingReader.php
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Component\Io;
use Ulrack\Cli\Common\Io\ReaderInterface;
use Ulrack\Cli\Common\Io\WriterInterface;
use Ulrack\Cli\Common\Io\PointerInterface;
use Ulrack\Cli\Common\Theme\StyleInterface;
use Ulrack\Cli\Common\Io\OptionProviderInterface;
class SttyAutocompletingReader implements ReaderInterface
{
/**
* Contains the input for opening a stream.
*
* @var string
*/
private $streamInput;
/**
* Contains the mode for the stream.
*
* @var string
*/
private $mode;
/**
* Object to retrieve all options for a command.
*
* @var OptionProviderInterface
*/
private $options;
/**
* Contains the router used to manipulate the input display.
*
* @var WriterInterface
*/
private $writer;
/**
* Contains the pointer.
*
* @var PointerInterface
*/
private $pointer;
/**
* Contains the style for the autocompleter.
*
* @var StyleInterface
*/
private $autocompleteStyle;
/**
* Contains the current pointer location.
*
* @var int
*/
private $pointerLocation = 0;
/**
* Determines the currently selected autocomplete option.
*
* @var int
*/
private $autocompleteSelector = -1;
/**
* Determines if autocompleting is active.
*
* @var bool
*/
private $autocompletingActive = false;
/**
* Contains the current autocomplete suggestion.
*
* @var string
*/
private $autocompleteSuggestion = '';
/**
* Contains the remainder of which is shown in the autocomplete.
*
* @var string
*/
private $autocompleteRemainder = '';
/**
* Contains the current input.
*
* @var string
*/
private $input = '';
/**
* Contains all active autocomplete options.
*
* @var array
*/
private $currentOptions = [];
/**
* Constructor.
*
* @param OptionProviderInterface $options
* @param WriterInterface $writer
* @param PointerInterface $pointer
* @param StyleInterface $autocompleteStyle
* @param string $streamInput
* @param string $mode
*/
public function __construct(
OptionProviderInterface $options,
WriterInterface $writer,
PointerInterface $pointer,
StyleInterface $autocompleteStyle,
string $streamInput = 'php://stdin',
string $mode = 'rb'
) {
$this->options = $options;
$this->writer = $writer;
$this->pointer = $pointer;
$this->autocompleteStyle = $autocompleteStyle;
$this->streamInput = $streamInput;
$this->mode = $mode;
}
/**
* Reads the input and returns the users input.
*
* @return string
*/
public function read(): string
{
$sttyMode = shell_exec('stty -g');
system('stty -icanon -echo');
$inputStream = fopen($this->streamInput, $this->mode, false);
$this->pointerLocation = 0;
$this->clearAutocomplete();
$this->input = '';
$this->currentOptions = $this->options->__invoke($this->input);
while (!feof($inputStream)) {
$character = fread($inputStream, 1);
// Interpret the escape sequence
if ($character === "\033") {
$this->handleEscapeSequences($inputStream);
continue;
}
// Line feed, finish recording the input.
if ($character === chr(10)) {
$this->finishRecording();
break;
}
// Backspace
if ($character === chr(127)) {
$this->handleBackspace();
continue;
} elseif ($character === chr(9)) {
// Tab
$this->tabAutocomplete();
continue;
}
if (!empty($character)) {
$this->handleAddCharacter($character);
$this->currentOptions = $this->options->__invoke($this->input);
if (!in_array($this->autocompleteSuggestion, $this->currentOptions)) {
if (count($this->currentOptions) === 0) {
$this->clearAutocomplete();
continue;
}
$this->clearAutocomplete();
$this->moveSuggestion(1);
$this->redrawSuggestion();
}
}
}
fclose($inputStream);
shell_exec(sprintf('stty %s', $sttyMode));
return $this->input;
}
/**
* Handles writing a character to the input.
*
* @param string $character
*
* @return void
*/
private function handleAddCharacter(string $character): void
{
// Write at the end.
if ($this->pointerLocation === strlen($this->input)) {
$this->input .= $character;
$this->writer->write($character);
$this->pointerLocation++;
return;
}
// Write somewhere inside the stream.
$this->input = substr(
$this->input,
0,
$this->pointerLocation
) . $character . substr(
$this->input,
$this->pointerLocation
);
$this->redrawInput($this->pointerLocation + 1);
}
/**
* Handles a backspace action.
*
* @return void
*/
private function handleBackspace(): void
{
if ($this->pointerLocation !== 0) {
$this->input = substr($this->input, 0, $this->pointerLocation - 1) .
substr($this->input, $this->pointerLocation);
$this->redrawInput($this->pointerLocation - 1);
}
$this->clearAutocomplete();
$this->redrawSuggestion();
}
/**
* Handles the internal functionality of tab autocomplete.
*
* @return void
*/
private function tabAutocomplete(): void
{
if (count($this->currentOptions) === 1) {
$this->input = array_pop($this->currentOptions);
$this->redrawInput(strlen($this->input));
} elseif ($this->autocompletingActive) {
$this->input = $this->autocompleteSuggestion;
$this->redrawInput(strlen($this->input));
$this->clearAutocomplete();
$this->redrawSuggestion();
}
}
/**
* Finish up recording the input.
*
* @return void
*/
private function finishRecording(): void
{
if ($this->autocompletingActive === true) {
$this->input = $this->autocompleteSuggestion;
$this->clearAutocomplete();
$this->redrawInput(strlen($this->input));
}
$this->writer->writeLine('');
}
/**
* Handles the escape sequence characters.
*
* @param resource $inputStream
*
* @return void
*/
private function handleEscapeSequences($inputStream): void
{
$character = fread($inputStream, 2);
switch ($character) {
// Left
case '[D':
if ($this->pointerLocation !== 0) {
$this->pointerLocation--;
$this->pointer->pointerLeft(1);
}
break;
// Right
case '[C':
if ($this->pointerLocation !== strlen($this->input)) {
$this->pointerLocation++;
$this->pointer->pointerRight(1);
} elseif ($this->autocompletingActive = true) {
$this->tabAutocomplete();
}
break;
// Up
case '[A':
$this->moveSuggestion(-1);
$this->redrawSuggestion();
break;
// Down
case '[B':
$this->moveSuggestion(1);
$this->redrawSuggestion();
break;
// Delete
case '[3':
fread($inputStream, 1);
$this->input = substr($this->input, 0, $this->pointerLocation) .
substr($this->input, $this->pointerLocation + 1);
$this->redrawInput($this->pointerLocation);
$this->clearAutocomplete();
break;
// Home
case '[H':
$this->pointer->pointerLeft($this->pointerLocation);
$this->pointerLocation = 0;
break;
// End
case '[F':
$inputLength = strlen($this->input);
$this->pointer->pointerRight(
$inputLength - $this->pointerLocation
);
$this->pointerLocation = $inputLength;
break;
}
}
/**
* Resets the autocompleter.
*
* @return void
*/
private function clearAutocomplete(): void
{
$this->autocompletingActive = false;
$this->autocompleteSelector = -1;
$this->autocompleteSuggestion = '';
$this->autocompleteRemainder = '';
}
/**
* Redraws the input and sets the pointer.
*
* @param int $newPointerLocation
*
* @return void
*/
private function redrawInput(int $newPointerLocation): void
{
// Reset the pointer to the beginning so we can redraw the input.
$this->pointer->pointerLeft($this->pointerLocation);
// Remove current input.
$this->writer->write("\033[K");
// Write new input.
$this->writer->write($this->input);
// Set pointer to new location.
$this->pointerLocation = $newPointerLocation;
// Back up to the desired point.
$this->pointer->pointerLeft(
strlen($this->input) - $this->pointerLocation
);
}
/**
* Moves the suggestion cursor.
*
* @param int $move
*
* @return void
*/
private function moveSuggestion(int $move): void
{
if (count($this->currentOptions) > 0) {
$optionKeys = array_keys($this->currentOptions);
$this->autocompletingActive = true;
// Negative number check.
if (abs($move) !== $move) {
if ($this->autocompleteSelector <= 0) {
// Go to the last suggestion
$this->autocompleteSelector = count($optionKeys) - 1;
} else {
$this->autocompleteSelector--;
}
} else {
if (
$this->autocompleteSelector === -1
|| $this->autocompleteSelector === count($optionKeys) - 1
) {
// Go to the first suggestion
$this->autocompleteSelector = 0;
} else {
$this->autocompleteSelector++;
}
}
$this->autocompleteSuggestion = $this->currentOptions[
$optionKeys[$this->autocompleteSelector]
];
return;
}
$this->clearAutocomplete();
}
/**
* Draws the suggestion for the user.
*
* @return void
*/
private function redrawSuggestion(): void
{
$move = strlen($this->input) - $this->pointerLocation;
$this->pointer->pointerRight($move);
$this->writer->write("\033[K");
if ($this->autocompletingActive) {
$this->autocompleteStyle->apply();
$this->autocompleteRemainder = preg_replace(
sprintf('/^%s/', preg_quote($this->input)),
'',
$this->autocompleteSuggestion
);
$move = $move + strlen($this->autocompleteRemainder);
$this->writer->write($this->autocompleteRemainder);
$this->autocompleteStyle->reset();
}
$this->pointer->pointerLeft($move);
}
}