Skip to content

Commit 27e6d2c

Browse files
committed
Make CR act as an alias for LF for custom key bindings
1 parent d08aad4 commit 27e6d2c

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/Readline.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,10 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
4444

4545
$that = $this;
4646
$codes = array(
47-
// The user confirms input with enter key which should usually
48-
// generate a NL (`\n`) character. Common terminals also seem to
49-
// accept a CR (`\r`) character in place and handle this just like a
50-
// NL. Similarly `ext-readline` uses different `icrnl` and `igncr`
51-
// TTY settings on some platforms, so we also accept both here.
52-
"\n" => 'onKeyEnter', // ^J
53-
"\r" => 'onKeyEnter', // ^M
54-
55-
"\x7f" => 'onKeyBackspace',
56-
"\t" => 'onKeyTab',
57-
"\x04" => 'handleEnd', // CTRL+D
47+
"\n" => 'onKeyEnter', // ^J
48+
"\x7f" => 'onKeyBackspace', // ^?
49+
"\t" => 'onKeyTab', // ^I
50+
"\x04" => 'handleEnd', // ^D
5851

5952
"\033[A" => 'onKeyUp',
6053
"\033[B" => 'onKeyDown',
@@ -69,6 +62,16 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
6962
// "\033[20~" => 'onKeyF10',
7063
);
7164
$decode = function ($code) use ($codes, $that) {
65+
// The user confirms input with enter key which should usually
66+
// generate a NL (`\n`) character. Common terminals also seem to
67+
// accept a CR (`\r`) character in place and handle this just like a
68+
// NL. Similarly `ext-readline` uses different `icrnl` and `igncr`
69+
// TTY settings on some platforms, so we also accept CR as an alias
70+
// for NL here. This implies key binding for NL will also trigger.
71+
if ($code === "\r") {
72+
$code = "\n";
73+
}
74+
7275
if ($that->listeners($code)) {
7376
$that->emit($code, array($code));
7477
return;

tests/ReadlineTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,23 @@ public function testBindCustomFunctionCanOverwriteAutocompleteBehavior()
988988
$this->input->emit('data', array("\t"));
989989
}
990990

991+
public function testBindCustomFunctionToNlOverwritesDataEvent()
992+
{
993+
$this->readline->on("\n", $this->expectCallableOnceWith("\n"));
994+
$this->readline->on('line', $this->expectCallableNever());
995+
996+
$this->input->emit('data', array("hello\n"));
997+
}
998+
999+
public function testBindCustomFunctionToNlFiresOnCr()
1000+
{
1001+
$this->readline->on("\n", $this->expectCallableOnceWith("\n"));
1002+
$this->readline->on("\r", $this->expectCallableNever());
1003+
$this->readline->on('line', $this->expectCallableNever());
1004+
1005+
$this->input->emit('data', array("hello\r"));
1006+
}
1007+
9911008
public function testEmitEmptyInputOnEnter()
9921009
{
9931010
$this->readline->on('data', $this->expectCallableOnceWith("\n"));

0 commit comments

Comments
 (0)