diff --git a/src/Readline.php b/src/Readline.php index 56327a2..165233b 100644 --- a/src/Readline.php +++ b/src/Readline.php @@ -491,27 +491,6 @@ public function getDrawString() return $output; } - /** - * Clears the current input prompt (if any) - * - * Usually, there should be no need to call this method manually. It will - * be invoked automatically whenever we detect that output needs to be - * written in place of the current prompt. The prompt will be rewritten - * after clearing the prompt and writing the output. - * - * @return self - * @see Stdio::write() which is responsible for invoking this method - * @internal - */ - public function clear() - { - if ($this->prompt !== '' || ($this->echo !== false && $this->linebuffer !== '')) { - $this->output->write("\r\033[K"); - } - - return $this; - } - /** @internal */ public function onKeyBackspace() { diff --git a/src/Stdio.php b/src/Stdio.php index 77035d5..0329741 100644 --- a/src/Stdio.php +++ b/src/Stdio.php @@ -179,8 +179,7 @@ public function end($data = null) $this->ending = true; // clear readline output, close input and end output - $this->readline->setInput('')->setPrompt('')->clear(); - $this->restoreTtyMode(); + $this->readline->setInput('')->setPrompt(''); $this->input->close(); $this->output->end(); } @@ -195,8 +194,7 @@ public function close() $this->closed = true; // clear readline output and then close - $this->readline->setInput('')->setPrompt('')->clear()->close(); - $this->restoreTtyMode(); + $this->readline->setInput('')->setPrompt(''); $this->input->close(); $this->output->close(); } @@ -227,6 +225,8 @@ public function handleEnd() /** @internal */ public function handleCloseInput() { + $this->restoreTtyMode(); + if (!$this->output->isWritable()) { $this->close(); } @@ -249,9 +249,9 @@ private function restoreTtyMode() // remove dummy readline handler to turn to default input mode $this->usesExtReadlineHandler = false; readline_callback_handler_remove(); - } elseif ($this->originalTtyMode !== null && $this->isTty()) { + } elseif ($this->originalTtyMode !== null && is_resource(STDIN) && $this->isTty()) { // Reset stty so it behaves normally again - shell_exec(sprintf('stty %s', $this->originalTtyMode)); + shell_exec('stty ' . escapeshellarg($this->originalTtyMode)); $this->originalTtyMode = null; } @@ -344,15 +344,10 @@ private function isTty() // For what it's worth, checking for device gid 5 (tty) is less reliable. // @link http://man7.org/linux/man-pages/man7/inode.7.html // @link https://www.kernel.org/doc/html/v4.11/admin-guide/devices.html#terminal-devices - if (is_resource(STDIN)) { - $stat = fstat(STDIN); - $mode = isset($stat['mode']) ? ($stat['mode'] & 0170000) : 0; - $major = isset($stat['dev']) ? (($stat['dev'] >> 8) & 0xff) : 0; + $stat = fstat(STDIN); + $mode = isset($stat['mode']) ? ($stat['mode'] & 0170000) : 0; + $major = isset($stat['dev']) ? (($stat['dev'] >> 8) & 0xff) : 0; - if ($mode === 0020000 && $major >= 2 && $major <= 143 && ($major <=5 || $major >= 128)) { - return true; - } - } - return false; + return ($mode === 0020000 && $major >= 2 && $major <= 143 && ($major <=5 || $major >= 128)); } }