Skip to content
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

Accept CR as an alias for LF to support more platforms #79

Merged
merged 2 commits into from
Sep 1, 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
19 changes: 14 additions & 5 deletions src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf

$that = $this;
$codes = array(
"\n" => 'onKeyEnter',
"\x7f" => 'onKeyBackspace',
"\t" => 'onKeyTab',

"\x04" => 'handleEnd', // CTRL+D
"\n" => 'onKeyEnter', // ^J
"\x7f" => 'onKeyBackspace', // ^?
"\t" => 'onKeyTab', // ^I
"\x04" => 'handleEnd', // ^D

"\033[A" => 'onKeyUp',
"\033[B" => 'onKeyDown',
Expand All @@ -63,6 +62,16 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
// "\033[20~" => 'onKeyF10',
);
$decode = function ($code) use ($codes, $that) {
// The user confirms input with enter key which should usually
// generate a NL (`\n`) character. Common terminals also seem to
// accept a CR (`\r`) character in place and handle this just like a
// NL. Similarly `ext-readline` uses different `icrnl` and `igncr`
// TTY settings on some platforms, so we also accept CR as an alias
// for NL here. This implies key binding for NL will also trigger.
if ($code === "\r") {
$code = "\n";
}

if ($that->listeners($code)) {
$that->emit($code, array($code));
return;
Expand Down
26 changes: 25 additions & 1 deletion tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,20 @@ public function testMovingCursorWithoutEchoDoesNotNeedToRedraw()
$this->assertSame($this->readline, $this->readline->moveCursorBy(2));
}

public function testDataEventWillBeEmittedForCompleteLine()
public function testDataEventWillBeEmittedForCompleteLineWithNl()
{
$this->readline->on('data', $this->expectCallableOnceWith("hello\n"));

$this->input->emit('data', array("hello\n"));
}

public function testDataEventWillBeEmittedWithNlAlsoForCompleteLineWithCr()
{
$this->readline->on('data', $this->expectCallableOnceWith("hello\n"));

$this->input->emit('data', array("hello\r"));
}

public function testDataEventWillNotBeEmittedForIncompleteLineButWillStayInInputBuffer()
{
$this->readline->on('data', $this->expectCallableNever());
Expand Down Expand Up @@ -981,6 +988,23 @@ public function testBindCustomFunctionCanOverwriteAutocompleteBehavior()
$this->input->emit('data', array("\t"));
}

public function testBindCustomFunctionToNlOverwritesDataEvent()
{
$this->readline->on("\n", $this->expectCallableOnceWith("\n"));
$this->readline->on('line', $this->expectCallableNever());

$this->input->emit('data', array("hello\n"));
}

public function testBindCustomFunctionToNlFiresOnCr()
{
$this->readline->on("\n", $this->expectCallableOnceWith("\n"));
$this->readline->on("\r", $this->expectCallableNever());
$this->readline->on('line', $this->expectCallableNever());

$this->input->emit('data', array("hello\r"));
}

public function testEmitEmptyInputOnEnter()
{
$this->readline->on('data', $this->expectCallableOnceWith("\n"));
Expand Down