Skip to content

Don't use ext-readline handler on Mac to fix CR/LF issues on Mac only #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ If this extension is missing, then this library will use a slighty slower Regex
work-around that should otherwise work equally well.
Installing `ext-mbstring` is highly recommended.

Internally, it will use the `ext-readline` to enable raw terminal input mode.
If this extension is missing, then this library will manually set the required
TTY settings on start and will try to restore previous settings on exit.
Input line editing is handled entirely within this library and does not rely on
`ext-readline`.
Internally, it will use the `ext-readline` to enable raw terminal input mode (on
Linux only as it is known to cause issues on Macs). Otherwise, this library will
manually set the required TTY settings on start and will try to restore previous
settings on exit. Input line editing is handled entirely within this library and
does not rely on `ext-readline`.
Installing `ext-readline` is entirely optional.

Note that *Microsoft Windows is not supported*.
Expand Down
11 changes: 7 additions & 4 deletions src/Stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Clue\React\Stdio;

use Clue\React\Stdio\Io\Stdin;
use Clue\React\Stdio\Io\Stdout;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Stream\DuplexStreamInterface;
Expand All @@ -23,6 +21,7 @@ class Stdio extends EventEmitter implements DuplexStreamInterface
private $closed = false;
private $incompleteLine = '';
private $originalTtyMode = null;
private $usesExtReadlineHandler = false;

public function __construct(LoopInterface $loop, ReadableStreamInterface $input = null, WritableStreamInterface $output = null, Readline $readline = null)
{
Expand Down Expand Up @@ -246,8 +245,9 @@ public function handleCloseOutput()
*/
private function restoreTtyMode()
{
if (function_exists('readline_callback_handler_remove')) {
if ($this->usesExtReadlineHandler) {
// remove dummy readline handler to turn to default input mode
$this->usesExtReadlineHandler = false;
readline_callback_handler_remove();
} elseif ($this->originalTtyMode !== null && $this->isTty()) {
// Reset stty so it behaves normally again
Expand Down Expand Up @@ -279,11 +279,14 @@ private function createStdin(LoopInterface $loop)

$stream = new ReadableResourceStream(STDIN, $loop);

if (function_exists('readline_callback_handler_install')) {
if (PHP_OS === 'Linux' && function_exists('readline_callback_handler_install')) {
// Prefer `ext-readline` to install dummy handler to turn on raw input mode.
// This is known to work on Linux and known to cause issues with CR/LF
// on Mac, so we only use this on Linux for now, see also issue #66.
// We will nevery actually feed the readline handler and instead
// handle all input in our `Readline` implementation.
readline_callback_handler_install('', function () { });
$this->usesExtReadlineHandler = true;
return $stream;
}

Expand Down