| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Stdio; |
| 4 |
|
| 5 |
use Clue\React\Term\ControlCodeParser; |
| 6 |
use Clue\React\Utf8\Sequencer as Utf8Sequencer; |
| 7 |
use Evenement\EventEmitter; |
| 8 |
use Evenement\EventEmitterInterface; |
| 9 |
use React\Stream\ReadableStreamInterface; |
| 10 |
use React\Stream\Util; |
| 11 |
use React\Stream\WritableStreamInterface; |
| 12 |
|
| 13 |
/** |
| 14 |
* @deprecated 2.3.0 Use `Stdio` instead |
| 15 |
* @see Stdio |
| 16 |
*/ |
| 17 |
class Readline extends EventEmitter implements ReadableStreamInterface |
| 18 |
{ |
| 19 |
private $prompt = ''; |
| 20 |
private $linebuffer = ''; |
| 21 |
private $linepos = 0; |
| 22 |
private $echo = true; |
| 23 |
private $move = true; |
| 24 |
private $bell = true; |
| 25 |
private $encoding = 'utf-8'; |
| 26 |
|
| 27 |
private $input; |
| 28 |
private $output; |
| 29 |
private $sequencer; |
| 30 |
private $closed = false; |
| 31 |
|
| 32 |
private $historyLines = array(); |
| 33 |
private $historyPosition = null; |
| 34 |
private $historyUnsaved = null; |
| 35 |
private $historyLimit = 500; |
| 36 |
|
| 37 |
private $autocomplete = null; |
| 38 |
private $autocompleteSuggestions = 8; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param ReadableStreamInterface $input |
| 42 |
* @param WritableStreamInterface $output |
| 43 |
* @param ?EventEmitterInterface $base |
| 44 |
*/ |
| 45 |
public function __construct(ReadableStreamInterface $input, WritableStreamInterface $output, $base = null) |
| 46 |
{ |
| 47 |
$this->input = $input; |
| 48 |
$this->output = $output; |
| 49 |
|
| 50 |
if (!$this->input->isReadable()) { |
| 51 |
$this->close(); |
| 52 |
return; |
| 53 |
} |
| 54 |
// push input through control code parser |
| 55 |
$parser = new ControlCodeParser($input); |
| 56 |
|
| 57 |
if ($base !== null && !$base instanceof EventEmitterInterface) { // manual type check to support legacy PHP < 7.1 |
| 58 |
throw new \InvalidArgumentException('Argument #3 ($base) expected null|Evenement\EventEmitterInterface'); |
| 59 |
} |
| 60 |
|
| 61 |
$that = $this; |
| 62 |
$codes = array( |
| 63 |
"\n" => 'onKeyEnter', // ^J |
| 64 |
"\x7f" => 'onKeyBackspace', // ^? |
| 65 |
"\t" => 'onKeyTab', // ^I |
| 66 |
"\x04" => 'handleEnd', // ^D |
| 67 |
|
| 68 |
"\033[A" => 'onKeyUp', |
| 69 |
"\033[B" => 'onKeyDown', |
| 70 |
"\033[C" => 'onKeyRight', |
| 71 |
"\033[D" => 'onKeyLeft', |
| 72 |
|
| 73 |
"\033[1~" => 'onKeyHome', |
| 74 |
// "\033[2~" => 'onKeyInsert', |
| 75 |
"\033[3~" => 'onKeyDelete', |
| 76 |
"\033[4~" => 'onKeyEnd', |
| 77 |
|
| 78 |
// "\033[20~" => 'onKeyF10', |
| 79 |
); |
| 80 |
$decode = function ($code) use ($codes, $that, $base) { |
| 81 |
// The user confirms input with enter key which should usually |
| 82 |
// generate a NL (`\n`) character. Common terminals also seem to |
| 83 |
// accept a CR (`\r`) character in place and handle this just like a |
| 84 |
// NL. Similarly `ext-readline` uses different `icrnl` and `igncr` |
| 85 |
// TTY settings on some platforms, so we also accept CR as an alias |
| 86 |
// for NL here. This implies key binding for NL will also trigger. |
| 87 |
if ($code === "\r") { |
| 88 |
$code = "\n"; |
| 89 |
} |
| 90 |
|
| 91 |
// forward compatibility: check if any key binding exists on base Stdio instance |
| 92 |
if ($base !== null && $base->listeners($code)) { |
| 93 |
$base->emit($code, array($code)); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// deprecated: check if any key binding exists on this Readline instance |
| 98 |
if ($that->listeners($code)) { |
| 99 |
$that->emit($code, array($code)); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
if (isset($codes[$code])) { |
| 104 |
$method = $codes[$code]; |
| 105 |
$that->$method($code); |
| 106 |
return; |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
$parser->on('csi', $decode); |
| 111 |
$parser->on('c0', $decode); |
| 112 |
|
| 113 |
// push resulting data through utf8 sequencer |
| 114 |
$utf8 = new Utf8Sequencer($parser); |
| 115 |
$utf8->on('data', function ($data) use ($that, $base) { |
| 116 |
$that->onFallback($data, $base); |
| 117 |
}); |
| 118 |
|
| 119 |
// process all stream events (forwarded from input stream) |
| 120 |
$utf8->on('end', array($this, 'handleEnd')); |
| 121 |
$utf8->on('error', array($this, 'handleError')); |
| 122 |
$utf8->on('close', array($this, 'close')); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* prompt to prepend to input line |
| 127 |
* |
| 128 |
* Will redraw the current input prompt with the current input buffer. |
| 129 |
* |
| 130 |
* @param string $prompt |
| 131 |
* @return self |
| 132 |
* @uses self::redraw() |
| 133 |
* @deprecated use Stdio::setPrompt() instead |
| 134 |
*/ |
| 135 |
public function setPrompt($prompt) |
| 136 |
{ |
| 137 |
if ($prompt === $this->prompt) { |
| 138 |
return $this; |
| 139 |
} |
| 140 |
|
| 141 |
$this->prompt = $prompt; |
| 142 |
|
| 143 |
return $this->redraw(); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* returns the prompt to prepend to input line |
| 148 |
* |
| 149 |
* @return string |
| 150 |
* @see self::setPrompt() |
| 151 |
* @deprecated use Stdio::getPrompt() instead |
| 152 |
*/ |
| 153 |
public function getPrompt() |
| 154 |
{ |
| 155 |
return $this->prompt; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* sets whether/how to echo text input |
| 160 |
* |
| 161 |
* The default setting is `true`, which means that every character will be |
| 162 |
* echo'ed as-is, i.e. you can see what you're typing. |
| 163 |
* For example: Typing "test" shows "test". |
| 164 |
* |
| 165 |
* You can turn this off by supplying `false`, which means that *nothing* |
| 166 |
* will be echo'ed while you're typing. This could be a good idea for |
| 167 |
* password prompts. Note that this could be confusing for users, so using |
| 168 |
* a character replacement as following is often preferred. |
| 169 |
* For example: Typing "test" shows "" (nothing). |
| 170 |
* |
| 171 |
* Alternative, you can supply a single character replacement character |
| 172 |
* that will be echo'ed for each character in the text input. This could |
| 173 |
* be a good idea for password prompts, where an asterisk character ("*") |
| 174 |
* is often used to indicate typing activity and password length. |
| 175 |
* For example: Typing "test" shows "****" (with asterisk replacement) |
| 176 |
* |
| 177 |
* Changing this setting will redraw the current prompt and echo the current |
| 178 |
* input buffer according to the new setting. |
| 179 |
* |
| 180 |
* @param boolean|string $echo echo can be turned on (boolean true) or off (boolean true), or you can supply a single character replacement string |
| 181 |
* @return self |
| 182 |
* @uses self::redraw() |
| 183 |
* @deprecated use Stdio::setEcho() instead |
| 184 |
*/ |
| 185 |
public function setEcho($echo) |
| 186 |
{ |
| 187 |
if ($echo === $this->echo) { |
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
$this->echo = $echo; |
| 192 |
|
| 193 |
// only redraw if there is any input |
| 194 |
if ($this->linebuffer !== '') { |
| 195 |
$this->redraw(); |
| 196 |
} |
| 197 |
|
| 198 |
return $this; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* whether or not to support moving cursor left and right |
| 203 |
* |
| 204 |
* switching cursor support moves the cursor to the end of the current |
| 205 |
* input buffer (if any). |
| 206 |
* |
| 207 |
* @param boolean $move |
| 208 |
* @return self |
| 209 |
* @uses self::redraw() |
| 210 |
* @deprecated use Stdio::setMove() instead |
| 211 |
*/ |
| 212 |
public function setMove($move) |
| 213 |
{ |
| 214 |
$this->move = !!$move; |
| 215 |
|
| 216 |
return $this->moveCursorTo($this->strlen($this->linebuffer)); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Gets current cursor position measured in number of text characters. |
| 221 |
* |
| 222 |
* Note that the number of text characters doesn't necessarily reflect the |
| 223 |
* number of monospace cells occupied by the text characters. If you want |
| 224 |
* to know the latter, use `self::getCursorCell()` instead. |
| 225 |
* |
| 226 |
* @return int |
| 227 |
* @see self::getCursorCell() to get the position measured in monospace cells |
| 228 |
* @see self::moveCursorTo() to move the cursor to a given character position |
| 229 |
* @see self::moveCursorBy() to move the cursor by given number of characters |
| 230 |
* @see self::setMove() to toggle whether the user can move the cursor position |
| 231 |
* @deprecated use Stdio::getCursorPosition() instead |
| 232 |
*/ |
| 233 |
public function getCursorPosition() |
| 234 |
{ |
| 235 |
return $this->linepos; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Gets current cursor position measured in monospace cells. |
| 240 |
* |
| 241 |
* Note that the cell position doesn't necessarily reflect the number of |
| 242 |
* text characters. If you want to know the latter, use |
| 243 |
* `self::getCursorPosition()` instead. |
| 244 |
* |
| 245 |
* Most "normal" characters occupy a single monospace cell, i.e. the ASCII |
| 246 |
* sequence for "A" requires a single cell, as do most UTF-8 sequences |
| 247 |
* like "Ä". |
| 248 |
* |
| 249 |
* However, there are a number of code points that do not require a cell |
| 250 |
* (i.e. invisible surrogates) or require two cells (e.g. some asian glyphs). |
| 251 |
* |
| 252 |
* Also note that this takes the echo mode into account, i.e. the cursor is |
| 253 |
* always at position zero if echo is off. If using a custom echo character |
| 254 |
* (like asterisk), it will take its width into account instead of the actual |
| 255 |
* input characters. |
| 256 |
* |
| 257 |
* @return int |
| 258 |
* @see self::getCursorPosition() to get current cursor position measured in characters |
| 259 |
* @see self::moveCursorTo() to move the cursor to a given character position |
| 260 |
* @see self::moveCursorBy() to move the cursor by given number of characters |
| 261 |
* @see self::setMove() to toggle whether the user can move the cursor position |
| 262 |
* @see self::setEcho() |
| 263 |
* @deprecated use Stdio::getCursorCell() instead |
| 264 |
*/ |
| 265 |
public function getCursorCell() |
| 266 |
{ |
| 267 |
if ($this->echo === false) { |
| 268 |
return 0; |
| 269 |
} |
| 270 |
if ($this->echo !== true) { |
| 271 |
return $this->strwidth($this->echo) * $this->linepos; |
| 272 |
} |
| 273 |
return $this->strwidth($this->substr($this->linebuffer, 0, $this->linepos)); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Moves cursor to right by $n chars (or left if $n is negative). |
| 278 |
* |
| 279 |
* Zero value or values out of range (exceeding current input buffer) are |
| 280 |
* simply ignored. |
| 281 |
* |
| 282 |
* Will redraw() the readline only if the visible cell position changes, |
| 283 |
* see `self::getCursorCell()` for more details. |
| 284 |
* |
| 285 |
* @param int $n |
| 286 |
* @return self |
| 287 |
* @uses self::moveCursorTo() |
| 288 |
* @uses self::redraw() |
| 289 |
* @deprecated use Stdio::moveCursorBy() instead |
| 290 |
*/ |
| 291 |
public function moveCursorBy($n) |
| 292 |
{ |
| 293 |
return $this->moveCursorTo($this->linepos + $n); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Moves cursor to given position in current line buffer. |
| 298 |
* |
| 299 |
* Values out of range (exceeding current input buffer) are simply ignored. |
| 300 |
* |
| 301 |
* Will redraw() the readline only if the visible cell position changes, |
| 302 |
* see `self::getCursorCell()` for more details. |
| 303 |
* |
| 304 |
* @param int $n |
| 305 |
* @return self |
| 306 |
* @uses self::redraw() |
| 307 |
* @deprecated use Stdio::moveCursorTo() instead |
| 308 |
*/ |
| 309 |
public function moveCursorTo($n) |
| 310 |
{ |
| 311 |
if ($n < 0 || $n === $this->linepos || $n > $this->strlen($this->linebuffer)) { |
| 312 |
return $this; |
| 313 |
} |
| 314 |
|
| 315 |
$old = $this->getCursorCell(); |
| 316 |
$this->linepos = $n; |
| 317 |
|
| 318 |
// only redraw if visible cell position change (implies cursor is actually visible) |
| 319 |
if ($this->getCursorCell() !== $old) { |
| 320 |
$this->redraw(); |
| 321 |
} |
| 322 |
|
| 323 |
return $this; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Appends the given input to the current text input buffer at the current position |
| 328 |
* |
| 329 |
* This moves the cursor accordingly to the number of characters added. |
| 330 |
* |
| 331 |
* @param string $input |
| 332 |
* @return self |
| 333 |
* @uses self::redraw() |
| 334 |
* @deprecated use Stdio::addInput() instead |
| 335 |
*/ |
| 336 |
public function addInput($input) |
| 337 |
{ |
| 338 |
if ($input === '') { |
| 339 |
return $this; |
| 340 |
} |
| 341 |
|
| 342 |
// read everything up until before current position |
| 343 |
$pre = $this->substr($this->linebuffer, 0, $this->linepos); |
| 344 |
$post = $this->substr($this->linebuffer, $this->linepos); |
| 345 |
|
| 346 |
$this->linebuffer = $pre . $input . $post; |
| 347 |
$this->linepos += $this->strlen($input); |
| 348 |
|
| 349 |
// only redraw if input should be echo'ed (i.e. is not hidden anyway) |
| 350 |
if ($this->echo !== false) { |
| 351 |
$this->redraw(); |
| 352 |
} |
| 353 |
|
| 354 |
return $this; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* set current text input buffer |
| 359 |
* |
| 360 |
* this moves the cursor to the end of the current |
| 361 |
* input buffer (if any). |
| 362 |
* |
| 363 |
* @param string $input |
| 364 |
* @return self |
| 365 |
* @uses self::redraw() |
| 366 |
* @deprecated use Stdio::setInput() instead |
| 367 |
*/ |
| 368 |
public function setInput($input) |
| 369 |
{ |
| 370 |
if ($this->linebuffer === $input) { |
| 371 |
return $this; |
| 372 |
} |
| 373 |
|
| 374 |
// remember old input length if echo replacement is used |
| 375 |
$oldlen = (is_string($this->echo)) ? $this->strlen($this->linebuffer) : null; |
| 376 |
|
| 377 |
$this->linebuffer = $input; |
| 378 |
$this->linepos = $this->strlen($this->linebuffer); |
| 379 |
|
| 380 |
// only redraw if input should be echo'ed (i.e. is not hidden anyway) |
| 381 |
// and echo replacement is used, make sure the input length changes |
| 382 |
if ($this->echo !== false && $this->linepos !== $oldlen) { |
| 383 |
$this->redraw(); |
| 384 |
} |
| 385 |
|
| 386 |
return $this; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* get current text input buffer |
| 391 |
* |
| 392 |
* @return string |
| 393 |
* @deprecated use Stdio::getInput() instead |
| 394 |
*/ |
| 395 |
public function getInput() |
| 396 |
{ |
| 397 |
return $this->linebuffer; |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Adds a new line to the (bottom position of the) history list |
| 402 |
* |
| 403 |
* @param string $line |
| 404 |
* @return self |
| 405 |
* @uses self::limitHistory() to make sure list does not exceed limits |
| 406 |
* @deprecated use Stdio::addHistory() instead |
| 407 |
*/ |
| 408 |
public function addHistory($line) |
| 409 |
{ |
| 410 |
$this->historyLines []= $line; |
| 411 |
|
| 412 |
return $this->limitHistory($this->historyLimit); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Clears the complete history list |
| 417 |
* |
| 418 |
* @return self |
| 419 |
* @deprecated use Stdio::clearHistory() instead |
| 420 |
*/ |
| 421 |
public function clearHistory() |
| 422 |
{ |
| 423 |
$this->historyLines = array(); |
| 424 |
$this->historyPosition = null; |
| 425 |
|
| 426 |
if ($this->historyUnsaved !== null) { |
| 427 |
$this->setInput($this->historyUnsaved); |
| 428 |
$this->historyUnsaved = null; |
| 429 |
} |
| 430 |
|
| 431 |
return $this; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Returns an array with all lines in the history |
| 436 |
* |
| 437 |
* @return string[] |
| 438 |
* @deprecated use Stdio::listHistory() instead |
| 439 |
*/ |
| 440 |
public function listHistory() |
| 441 |
{ |
| 442 |
return $this->historyLines; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Limits the history to a maximum of N entries and truncates the current history list accordingly |
| 447 |
* |
| 448 |
* @param int|null $limit |
| 449 |
* @return self |
| 450 |
* @deprecated use Stdio::limitHistory() instead |
| 451 |
*/ |
| 452 |
public function limitHistory($limit) |
| 453 |
{ |
| 454 |
$this->historyLimit = $limit === null ? null : $limit; |
| 455 |
|
| 456 |
// limit send and currently exceeded |
| 457 |
if ($this->historyLimit !== null && isset($this->historyLines[$this->historyLimit])) { |
| 458 |
// adjust position in history according to new position after applying limit |
| 459 |
if ($this->historyPosition !== null) { |
| 460 |
$this->historyPosition -= count($this->historyLines) - $this->historyLimit; |
| 461 |
|
| 462 |
// current position will drop off from list => restore original |
| 463 |
if ($this->historyPosition < 0) { |
| 464 |
$this->setInput($this->historyUnsaved); |
| 465 |
$this->historyPosition = null; |
| 466 |
$this->historyUnsaved = null; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
$this->historyLines = array_slice($this->historyLines, -$this->historyLimit, $this->historyLimit); |
| 471 |
} |
| 472 |
|
| 473 |
return $this; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* set autocompletion handler to use |
| 478 |
* |
| 479 |
* The autocomplete handler will be called whenever the user hits the TAB |
| 480 |
* key. |
| 481 |
* |
| 482 |
* @param callable|null $autocomplete |
| 483 |
* @return self |
| 484 |
* @throws \InvalidArgumentException if the given callable is invalid |
| 485 |
* @deprecated use Stdio::setAutocomplete() instead |
| 486 |
*/ |
| 487 |
public function setAutocomplete($autocomplete) |
| 488 |
{ |
| 489 |
if ($autocomplete !== null && !is_callable($autocomplete)) { |
| 490 |
throw new \InvalidArgumentException('Invalid autocomplete function given'); |
| 491 |
} |
| 492 |
|
| 493 |
$this->autocomplete = $autocomplete; |
| 494 |
|
| 495 |
return $this; |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Whether or not to emit a audible/visible BELL signal when using a disabled function |
| 500 |
* |
| 501 |
* By default, this class will emit a BELL signal when using a disable function, |
| 502 |
* such as using the <kbd>left</kbd> or <kbd>backspace</kbd> keys when |
| 503 |
* already at the beginning of the line. |
| 504 |
* |
| 505 |
* Whether or not the BELL is audible/visible depends on the termin and its |
| 506 |
* settings, i.e. some terminals may "beep" or flash the screen or emit a |
| 507 |
* short vibration. |
| 508 |
* |
| 509 |
* @param bool $bell |
| 510 |
* @return void |
| 511 |
* @internal use Stdio::setBell() instead |
| 512 |
*/ |
| 513 |
public function setBell($bell) |
| 514 |
{ |
| 515 |
$this->bell = (bool)$bell; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* redraw the current input prompt |
| 520 |
* |
| 521 |
* Usually, there should be no need to call this method manually. It will |
| 522 |
* be invoked automatically whenever we detect the readline input needs to |
| 523 |
* be (re)written to the output. |
| 524 |
* |
| 525 |
* Clear the current line and draw the input prompt. If input echo is |
| 526 |
* enabled, will also draw the current input buffer and move to the current |
| 527 |
* input buffer position. |
| 528 |
* |
| 529 |
* @return self |
| 530 |
* @internal |
| 531 |
*/ |
| 532 |
public function redraw() |
| 533 |
{ |
| 534 |
// Erase characters from cursor to end of line and then redraw actual input |
| 535 |
$this->output->write("\r\033[K" . $this->getDrawString()); |
| 536 |
|
| 537 |
return $this; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Returns the string that is used to draw the input prompt |
| 542 |
* |
| 543 |
* @return string |
| 544 |
* @internal |
| 545 |
*/ |
| 546 |
public function getDrawString() |
| 547 |
{ |
| 548 |
$output = $this->prompt; |
| 549 |
if ($this->echo !== false) { |
| 550 |
if ($this->echo === true) { |
| 551 |
$buffer = $this->linebuffer; |
| 552 |
} else { |
| 553 |
$buffer = str_repeat($this->echo, $this->strlen($this->linebuffer)); |
| 554 |
} |
| 555 |
|
| 556 |
// write output, then move back $reverse chars (by sending backspace) |
| 557 |
$output .= $buffer . str_repeat("\x08", $this->strwidth($buffer) - $this->getCursorCell()); |
| 558 |
} |
| 559 |
|
| 560 |
return $output; |
| 561 |
} |
| 562 |
|
| 563 |
/** @internal */ |
| 564 |
public function onKeyBackspace() |
| 565 |
{ |
| 566 |
// left delete only if not at the beginning |
| 567 |
if ($this->linepos === 0) { |
| 568 |
$this->bell(); |
| 569 |
} else { |
| 570 |
$this->deleteChar($this->linepos - 1); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
/** @internal */ |
| 575 |
public function onKeyDelete() |
| 576 |
{ |
| 577 |
// right delete only if not at the end |
| 578 |
if ($this->isEol()) { |
| 579 |
$this->bell(); |
| 580 |
} else { |
| 581 |
$this->deleteChar($this->linepos); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
/** @internal */ |
| 586 |
public function onKeyHome() |
| 587 |
{ |
| 588 |
if ($this->move && $this->linepos !== 0) { |
| 589 |
$this->moveCursorTo(0); |
| 590 |
} else { |
| 591 |
$this->bell(); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
/** @internal */ |
| 596 |
public function onKeyEnd() |
| 597 |
{ |
| 598 |
if ($this->move && !$this->isEol()) { |
| 599 |
$this->moveCursorTo($this->strlen($this->linebuffer)); |
| 600 |
} else { |
| 601 |
$this->bell(); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
/** @internal */ |
| 606 |
public function onKeyTab() |
| 607 |
{ |
| 608 |
if ($this->autocomplete === null) { |
| 609 |
$this->bell(); |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
// current word prefix and offset for start of word in input buffer |
| 614 |
// "echo foo|bar world" will return just "foo" with word offset 5 |
| 615 |
$word = $this->substr($this->linebuffer, 0, $this->linepos); |
| 616 |
$start = 0; |
| 617 |
$end = $this->linepos; |
| 618 |
|
| 619 |
// buffer prefix and postfix for everything that will *not* be matched |
| 620 |
// above example will return "echo " and "bar world" |
| 621 |
$prefix = ''; |
| 622 |
$postfix = $this->substr($this->linebuffer, $this->linepos); |
| 623 |
|
| 624 |
// skip everything before last space |
| 625 |
$pos = strrpos($word, ' '); |
| 626 |
if ($pos !== false) { |
| 627 |
$prefix = (string)substr($word, 0, $pos + 1); |
| 628 |
$word = (string)substr($word, $pos + 1); |
| 629 |
$start = $this->strlen($prefix); |
| 630 |
} |
| 631 |
|
| 632 |
// skip double quote (") or single quote (') from argument |
| 633 |
$quote = null; |
| 634 |
if (isset($word[0]) && ($word[0] === '"' || $word[0] === '\'')) { |
| 635 |
$quote = $word[0]; |
| 636 |
++$start; |
| 637 |
$prefix .= $word[0]; |
| 638 |
$word = (string)substr($word, 1); |
| 639 |
} |
| 640 |
|
| 641 |
// invoke autocomplete callback |
| 642 |
$words = call_user_func($this->autocomplete, $word, $start, $end); |
| 643 |
|
| 644 |
// return early if autocomplete does not return anything |
| 645 |
if ($words === null) { |
| 646 |
return; |
| 647 |
} |
| 648 |
|
| 649 |
// remove from list of possible words that do not start with $word or are duplicates |
| 650 |
$words = array_unique($words); |
| 651 |
if ($word !== '' && $words) { |
| 652 |
$words = array_filter($words, function ($w) use ($word) { |
| 653 |
return strpos($w, $word) === 0; |
| 654 |
}); |
| 655 |
} |
| 656 |
|
| 657 |
// return if neither of the possible words match |
| 658 |
if (!$words) { |
| 659 |
$this->bell(); |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
// search longest common prefix among all possible matches |
| 664 |
$found = reset($words); |
| 665 |
$all = count($words); |
| 666 |
if ($all > 1) { |
| 667 |
while ($found !== '') { |
| 668 |
// count all words that start with $found |
| 669 |
$matches = count(array_filter($words, function ($w) use ($found) { |
| 670 |
return strpos($w, $found) === 0; |
| 671 |
})); |
| 672 |
|
| 673 |
// ALL words match $found => common substring found |
| 674 |
if ($all === $matches) { |
| 675 |
break; |
| 676 |
} |
| 677 |
|
| 678 |
// remove last letter from $found and try again |
| 679 |
$found = $this->substr($found, 0, -1); |
| 680 |
} |
| 681 |
|
| 682 |
// found more than one possible match with this prefix => print options |
| 683 |
if ($found === $word || $found === '') { |
| 684 |
// limit number of possible matches |
| 685 |
if (count($words) > $this->autocompleteSuggestions) { |
| 686 |
$more = count($words) - ($this->autocompleteSuggestions - 1); |
| 687 |
$words = array_slice($words, 0, $this->autocompleteSuggestions - 1); |
| 688 |
$words []= '(+' . $more . ' others)'; |
| 689 |
} |
| 690 |
|
| 691 |
$this->output->write("\n" . implode(' ', $words) . "\n"); |
| 692 |
$this->redraw(); |
| 693 |
|
| 694 |
return; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
if ($quote !== null && $all === 1 && (strpos($postfix, $quote) === false || strpos($postfix, $quote) > strpos($postfix, ' '))) { |
| 699 |
// add closing quote if word started in quotes and postfix does not already contain closing quote before next space |
| 700 |
$found .= $quote; |
| 701 |
} elseif ($found === '') { |
| 702 |
// add single quotes around empty match |
| 703 |
$found = '\'\''; |
| 704 |
} |
| 705 |
|
| 706 |
if ($postfix === '' && $all === 1) { |
| 707 |
// append single space after match unless there's a postfix or there are multiple completions |
| 708 |
$found .= ' '; |
| 709 |
} |
| 710 |
|
| 711 |
// replace word in input with best match and adjust cursor |
| 712 |
$this->linebuffer = $prefix . $found . $postfix; |
| 713 |
$this->moveCursorBy($this->strlen($found) - $this->strlen($word)); |
| 714 |
} |
| 715 |
|
| 716 |
/** @internal */ |
| 717 |
public function onKeyEnter() |
| 718 |
{ |
| 719 |
if ($this->echo !== false) { |
| 720 |
$this->output->write("\n"); |
| 721 |
} |
| 722 |
$this->processLine("\n"); |
| 723 |
} |
| 724 |
|
| 725 |
/** @internal */ |
| 726 |
public function onKeyLeft() |
| 727 |
{ |
| 728 |
if ($this->move && $this->linepos !== 0) { |
| 729 |
$this->moveCursorBy(-1); |
| 730 |
} else { |
| 731 |
$this->bell(); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/** @internal */ |
| 736 |
public function onKeyRight() |
| 737 |
{ |
| 738 |
if ($this->move && !$this->isEol()) { |
| 739 |
$this->moveCursorBy(1); |
| 740 |
} else { |
| 741 |
$this->bell(); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
/** @internal */ |
| 746 |
public function onKeyUp() |
| 747 |
{ |
| 748 |
// ignore if already at top or history is empty |
| 749 |
if ($this->historyPosition === 0 || !$this->historyLines) { |
| 750 |
$this->bell(); |
| 751 |
return; |
| 752 |
} |
| 753 |
|
| 754 |
if ($this->historyPosition === null) { |
| 755 |
// first time up => move to last entry |
| 756 |
$this->historyPosition = count($this->historyLines) - 1; |
| 757 |
$this->historyUnsaved = $this->getInput(); |
| 758 |
} else { |
| 759 |
// somewhere in the list => move by one |
| 760 |
$this->historyPosition--; |
| 761 |
} |
| 762 |
|
| 763 |
$this->setInput($this->historyLines[$this->historyPosition]); |
| 764 |
} |
| 765 |
|
| 766 |
/** @internal */ |
| 767 |
public function onKeyDown() |
| 768 |
{ |
| 769 |
// ignore if not currently cycling through history |
| 770 |
if ($this->historyPosition === null) { |
| 771 |
$this->bell(); |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
if (isset($this->historyLines[$this->historyPosition + 1])) { |
| 776 |
// this is still a valid position => advance by one and apply |
| 777 |
$this->historyPosition++; |
| 778 |
$this->setInput($this->historyLines[$this->historyPosition]); |
| 779 |
} else { |
| 780 |
// moved beyond bottom => restore original unsaved input |
| 781 |
$this->setInput($this->historyUnsaved); |
| 782 |
$this->historyPosition = null; |
| 783 |
$this->historyUnsaved = null; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
* Will be invoked for character(s) that could not otherwise be processed by the sequencer |
| 789 |
* |
| 790 |
* @param string $chars |
| 791 |
* @param ?EventEmitterInterface $base |
| 792 |
* @internal |
| 793 |
*/ |
| 794 |
public function onFallback($chars, $base = null) |
| 795 |
{ |
| 796 |
// check if there's any special key binding for any of the chars |
| 797 |
$buffer = ''; |
| 798 |
foreach ($this->strsplit($chars) as $char) { |
| 799 |
// forward compatibility: check if any key binding exists on base Stdio instance |
| 800 |
// deprecated: check if any key binding exists on this Readline instance |
| 801 |
$emit = null; |
| 802 |
if ($base !== null && $base->listeners($char)) { |
| 803 |
$emit = $base; |
| 804 |
} else if ($this->listeners($char)) { |
| 805 |
$emit = $this; |
| 806 |
} |
| 807 |
|
| 808 |
if ($emit !== null) { |
| 809 |
// special key binding for this character found |
| 810 |
// process all characters before this one before invoking function |
| 811 |
if ($buffer !== '') { |
| 812 |
$this->addInput($buffer); |
| 813 |
$buffer = ''; |
| 814 |
} |
| 815 |
$emit->emit($char, array($char)); |
| 816 |
} else { |
| 817 |
$buffer .= $char; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
// process remaining input characters after last special key binding |
| 822 |
if ($buffer !== '') { |
| 823 |
$this->addInput($buffer); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
/** |
| 828 |
* delete a character at the given position |
| 829 |
* |
| 830 |
* Removing a character left to the current cursor will also move the cursor |
| 831 |
* to the left. |
| 832 |
* |
| 833 |
* @param int $n |
| 834 |
*/ |
| 835 |
private function deleteChar($n) |
| 836 |
{ |
| 837 |
// read everything up until before current position |
| 838 |
$pre = $this->substr($this->linebuffer, 0, $n); |
| 839 |
$post = $this->substr($this->linebuffer, $n + 1); |
| 840 |
|
| 841 |
$this->linebuffer = $pre . $post; |
| 842 |
|
| 843 |
// move cursor one cell to the left if we're deleting in front of the cursor |
| 844 |
if ($n < $this->linepos) { |
| 845 |
--$this->linepos; |
| 846 |
} |
| 847 |
|
| 848 |
$this->redraw(); |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* process the current line buffer, emit event and redraw empty line |
| 853 |
* |
| 854 |
* @uses self::setInput() |
| 855 |
*/ |
| 856 |
protected function processLine($eol) |
| 857 |
{ |
| 858 |
// reset history cycle position |
| 859 |
$this->historyPosition = null; |
| 860 |
$this->historyUnsaved = null; |
| 861 |
|
| 862 |
// store and reset/clear/redraw current input |
| 863 |
$line = $this->linebuffer; |
| 864 |
if ($line !== '') { |
| 865 |
// the line is not empty, reset it (and implicitly redraw prompt) |
| 866 |
$this->setInput(''); |
| 867 |
} elseif ($this->echo !== false) { |
| 868 |
// explicitly redraw prompt after empty line |
| 869 |
$this->redraw(); |
| 870 |
} |
| 871 |
|
| 872 |
// process stored input buffer |
| 873 |
$this->emit('data', array($line . $eol)); |
| 874 |
} |
| 875 |
|
| 876 |
/** |
| 877 |
* @param string $str |
| 878 |
* @return int |
| 879 |
* @codeCoverageIgnore |
| 880 |
*/ |
| 881 |
private function strlen($str) |
| 882 |
{ |
| 883 |
// prefer mb_strlen() if available |
| 884 |
if (function_exists('mb_strlen')) { |
| 885 |
return mb_strlen($str, $this->encoding); |
| 886 |
} |
| 887 |
|
| 888 |
// otherwise replace all unicode chars with dots and count dots |
| 889 |
return strlen(preg_replace('/./us', '.', $str)); |
| 890 |
} |
| 891 |
|
| 892 |
/** |
| 893 |
* @param string $str |
| 894 |
* @param int $start |
| 895 |
* @param ?int $len |
| 896 |
* @return string |
| 897 |
* @codeCoverageIgnore |
| 898 |
*/ |
| 899 |
private function substr($str, $start = 0, $len = null) |
| 900 |
{ |
| 901 |
if ($len === null) { |
| 902 |
$len = $this->strlen($str) - $start; |
| 903 |
} |
| 904 |
|
| 905 |
// prefer mb_substr() if available |
| 906 |
if (function_exists('mb_substr')) { |
| 907 |
return (string)mb_substr($str, $start, $len, $this->encoding); |
| 908 |
} |
| 909 |
|
| 910 |
// otherwise build array with all unicode chars and slice array |
| 911 |
preg_match_all('/./us', $str, $matches); |
| 912 |
|
| 913 |
return implode('', array_slice($matches[0], $start, $len)); |
| 914 |
} |
| 915 |
|
| 916 |
/** |
| 917 |
* @internal |
| 918 |
* @param string $str |
| 919 |
* @return int |
| 920 |
* @codeCoverageIgnore |
| 921 |
*/ |
| 922 |
public function strwidth($str) |
| 923 |
{ |
| 924 |
// prefer mb_strwidth() if available |
| 925 |
if (function_exists('mb_strwidth')) { |
| 926 |
return mb_strwidth($str, $this->encoding); |
| 927 |
} |
| 928 |
|
| 929 |
// otherwise replace each double-width unicode graphemes with two dots, all others with single dot and count number of dots |
| 930 |
// mbstring's list of double-width graphemes is *very* long: https://3v4l.org/GEg3u |
| 931 |
// let's use symfony's list from https://github.com/symfony/polyfill-mbstring/blob/e79d363049d1c2128f133a2667e4f4190904f7f4/Mbstring.php#L523 |
| 932 |
// which looks like they originally came from http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c |
| 933 |
return strlen(preg_replace( |
| 934 |
array( |
| 935 |
'/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', |
| 936 |
'/./us', |
| 937 |
), |
| 938 |
array( |
| 939 |
'..', |
| 940 |
'.', |
| 941 |
), |
| 942 |
$str |
| 943 |
)); |
| 944 |
} |
| 945 |
|
| 946 |
/** |
| 947 |
* @param string $str |
| 948 |
* @return string[] |
| 949 |
*/ |
| 950 |
private function strsplit($str) |
| 951 |
{ |
| 952 |
return preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY); |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
* @return bool |
| 957 |
*/ |
| 958 |
private function isEol() |
| 959 |
{ |
| 960 |
return $this->linepos === $this->strlen($this->linebuffer); |
| 961 |
} |
| 962 |
|
| 963 |
/** |
| 964 |
* @return void |
| 965 |
*/ |
| 966 |
private function bell() |
| 967 |
{ |
| 968 |
if ($this->bell) { |
| 969 |
$this->output->write("\x07"); // BEL a.k.a. \a |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
/** @internal */ |
| 974 |
public function handleEnd() |
| 975 |
{ |
| 976 |
if ($this->linebuffer !== '') { |
| 977 |
$this->processLine(''); |
| 978 |
} |
| 979 |
|
| 980 |
if (!$this->closed) { |
| 981 |
$this->emit('end'); |
| 982 |
$this->close(); |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
/** @internal */ |
| 987 |
public function handleError(\Exception $error) |
| 988 |
{ |
| 989 |
$this->emit('error', array($error)); |
| 990 |
$this->close(); |
| 991 |
} |
| 992 |
|
| 993 |
public function isReadable() |
| 994 |
{ |
| 995 |
return !$this->closed && $this->input->isReadable(); |
| 996 |
} |
| 997 |
|
| 998 |
public function pause() |
| 999 |
{ |
| 1000 |
$this->input->pause(); |
| 1001 |
} |
| 1002 |
|
| 1003 |
public function resume() |
| 1004 |
{ |
| 1005 |
$this->input->resume(); |
| 1006 |
} |
| 1007 |
|
| 1008 |
public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 1009 |
{ |
| 1010 |
Util::pipe($this, $dest, $options); |
| 1011 |
|
| 1012 |
return $dest; |
| 1013 |
} |
| 1014 |
|
| 1015 |
public function close() |
| 1016 |
{ |
| 1017 |
if ($this->closed) { |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
|
| 1021 |
$this->closed = true; |
| 1022 |
|
| 1023 |
$this->input->close(); |
| 1024 |
|
| 1025 |
$this->emit('close'); |
| 1026 |
$this->removeAllListeners(); |
| 1027 |
} |
| 1028 |
} |