Skip to content

Commit d08aad4

Browse files
committed
Also accept CR in place of LF to support more platforms
1 parent 5da4d99 commit d08aad4

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Readline.php

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

4545
$that = $this;
4646
$codes = array(
47-
"\n" => 'onKeyEnter',
48-
"\x7f" => 'onKeyBackspace',
49-
"\t" => 'onKeyTab',
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
5054

55+
"\x7f" => 'onKeyBackspace',
56+
"\t" => 'onKeyTab',
5157
"\x04" => 'handleEnd', // CTRL+D
5258

5359
"\033[A" => 'onKeyUp',

tests/ReadlineTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,20 @@ public function testMovingCursorWithoutEchoDoesNotNeedToRedraw()
228228
$this->assertSame($this->readline, $this->readline->moveCursorBy(2));
229229
}
230230

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

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

238+
public function testDataEventWillBeEmittedWithNlAlsoForCompleteLineWithCr()
239+
{
240+
$this->readline->on('data', $this->expectCallableOnceWith("hello\n"));
241+
242+
$this->input->emit('data', array("hello\r"));
243+
}
244+
238245
public function testDataEventWillNotBeEmittedForIncompleteLineButWillStayInInputBuffer()
239246
{
240247
$this->readline->on('data', $this->expectCallableNever());

0 commit comments

Comments
 (0)