|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\CLI; |
| 15 | + |
| 16 | +/** |
| 17 | + * Reader of single keypresses from a stream. |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class KeyReader |
| 22 | +{ |
| 23 | + private string $savedMode = ''; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param resource $stream |
| 27 | + * @param bool $raw Whether to switch the terminal to raw mode while open |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + private readonly mixed $stream, |
| 31 | + private readonly bool $raw, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns `false` when raw mode was wanted but the terminal settings could not be read. |
| 37 | + */ |
| 38 | + public function open(): bool |
| 39 | + { |
| 40 | + if (! $this->raw) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + // @codeCoverageIgnoreStart |
| 45 | + $this->savedMode = trim((string) shell_exec('stty -g < /dev/tty 2>/dev/null')); |
| 46 | + |
| 47 | + if ($this->savedMode === '') { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + // Signals stay off so Ctrl+C arrives as a key, letting the terminal be restored before the process ends. |
| 52 | + shell_exec('stty -icanon -isig -echo < /dev/tty 2>/dev/null'); |
| 53 | + |
| 54 | + return true; |
| 55 | + // @codeCoverageIgnoreEnd |
| 56 | + } |
| 57 | + |
| 58 | + public function close(): void |
| 59 | + { |
| 60 | + if ($this->savedMode === '') { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // @codeCoverageIgnoreStart |
| 65 | + shell_exec(sprintf('stty %s < /dev/tty 2>/dev/null', escapeshellarg($this->savedMode))); |
| 66 | + |
| 67 | + $this->savedMode = ''; |
| 68 | + // @codeCoverageIgnoreEnd |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the next key, or an empty string at the end of the stream. |
| 73 | + */ |
| 74 | + public function read(): string |
| 75 | + { |
| 76 | + $key = $this->readByte(); |
| 77 | + |
| 78 | + if ($key !== "\e") { |
| 79 | + return $key; |
| 80 | + } |
| 81 | + |
| 82 | + $key .= $this->readByte(); |
| 83 | + |
| 84 | + if ($key === "\eO") { |
| 85 | + return $key . $this->readByte(); |
| 86 | + } |
| 87 | + |
| 88 | + if ($key !== "\e[") { |
| 89 | + return $key; |
| 90 | + } |
| 91 | + |
| 92 | + do { |
| 93 | + $byte = $this->readByte(); |
| 94 | + $key .= $byte; |
| 95 | + } while ($byte !== '' && preg_match('/[A-Za-z~]/', $byte) !== 1); |
| 96 | + |
| 97 | + return $key; |
| 98 | + } |
| 99 | + |
| 100 | + private function readByte(): string |
| 101 | + { |
| 102 | + $byte = fread($this->stream, 1); |
| 103 | + |
| 104 | + return $byte === false ? '' : $byte; |
| 105 | + } |
| 106 | +} |
0 commit comments