Skip to content

Commit 3beb3f6

Browse files
committed
Fix and simplify restoring TTY mode when ext-readline is not in use
1 parent a62268d commit 3beb3f6

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

src/Readline.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -491,27 +491,6 @@ public function getDrawString()
491491
return $output;
492492
}
493493

494-
/**
495-
* Clears the current input prompt (if any)
496-
*
497-
* Usually, there should be no need to call this method manually. It will
498-
* be invoked automatically whenever we detect that output needs to be
499-
* written in place of the current prompt. The prompt will be rewritten
500-
* after clearing the prompt and writing the output.
501-
*
502-
* @return self
503-
* @see Stdio::write() which is responsible for invoking this method
504-
* @internal
505-
*/
506-
public function clear()
507-
{
508-
if ($this->prompt !== '' || ($this->echo !== false && $this->linebuffer !== '')) {
509-
$this->output->write("\r\033[K");
510-
}
511-
512-
return $this;
513-
}
514-
515494
/** @internal */
516495
public function onKeyBackspace()
517496
{

src/Stdio.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ public function end($data = null)
179179
$this->ending = true;
180180

181181
// clear readline output, close input and end output
182-
$this->readline->setInput('')->setPrompt('')->clear();
183-
$this->restoreTtyMode();
182+
$this->readline->setInput('')->setPrompt('');
184183
$this->input->close();
185184
$this->output->end();
186185
}
@@ -195,8 +194,7 @@ public function close()
195194
$this->closed = true;
196195

197196
// clear readline output and then close
198-
$this->readline->setInput('')->setPrompt('')->clear()->close();
199-
$this->restoreTtyMode();
197+
$this->readline->setInput('')->setPrompt('');
200198
$this->input->close();
201199
$this->output->close();
202200
}
@@ -227,6 +225,8 @@ public function handleEnd()
227225
/** @internal */
228226
public function handleCloseInput()
229227
{
228+
$this->restoreTtyMode();
229+
230230
if (!$this->output->isWritable()) {
231231
$this->close();
232232
}
@@ -249,9 +249,9 @@ private function restoreTtyMode()
249249
// remove dummy readline handler to turn to default input mode
250250
$this->usesExtReadlineHandler = false;
251251
readline_callback_handler_remove();
252-
} elseif ($this->originalTtyMode !== null && $this->isTty()) {
252+
} elseif ($this->originalTtyMode !== null && is_resource(STDIN) && $this->isTty()) {
253253
// Reset stty so it behaves normally again
254-
shell_exec(sprintf('stty %s', $this->originalTtyMode));
254+
shell_exec('stty ' . escapeshellarg($this->originalTtyMode));
255255
$this->originalTtyMode = null;
256256
}
257257

@@ -344,15 +344,10 @@ private function isTty()
344344
// For what it's worth, checking for device gid 5 (tty) is less reliable.
345345
// @link http://man7.org/linux/man-pages/man7/inode.7.html
346346
// @link https://www.kernel.org/doc/html/v4.11/admin-guide/devices.html#terminal-devices
347-
if (is_resource(STDIN)) {
348-
$stat = fstat(STDIN);
349-
$mode = isset($stat['mode']) ? ($stat['mode'] & 0170000) : 0;
350-
$major = isset($stat['dev']) ? (($stat['dev'] >> 8) & 0xff) : 0;
347+
$stat = fstat(STDIN);
348+
$mode = isset($stat['mode']) ? ($stat['mode'] & 0170000) : 0;
349+
$major = isset($stat['dev']) ? (($stat['dev'] >> 8) & 0xff) : 0;
351350

352-
if ($mode === 0020000 && $major >= 2 && $major <= 143 && ($major <=5 || $major >= 128)) {
353-
return true;
354-
}
355-
}
356-
return false;
351+
return ($mode === 0020000 && $major >= 2 && $major <= 143 && ($major <=5 || $major >= 128));
357352
}
358353
}

0 commit comments

Comments
 (0)