Skip to content

Commit 339f7c6

Browse files
committed
Emit final data when input ends (EOF without EOL)
1 parent 50b5a6a commit 339f7c6

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

examples/01-periodic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
});
1717

1818
// react to commands the user entered
19-
$stdio->on('line', function ($line) use ($stdio, $loop, $timer) {
20-
$stdio->writeln('you just said: ' . $line . ' (' . strlen($line) . ')');
19+
$stdio->on('data', function ($line) use ($stdio, $loop, $timer) {
20+
$stdio->writeln('you just said: ' . addcslashes($line, "\0..\37") . ' (' . strlen($line) . ')');
2121

2222
$loop->cancelTimer($timer);
2323
$stdio->end();

src/Readline.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
4747
"\n" => 'onKeyEnter',
4848
"\x7f" => 'onKeyBackspace',
4949
"\t" => 'onKeyTab',
50-
"\x04" => 'closeInput', // CTRL+D
50+
51+
"\x04" => 'handleEnd', // CTRL+D
5152

5253
"\033[A" => 'onKeyUp',
5354
"\033[B" => 'onKeyDown',
@@ -804,6 +805,10 @@ public function strwidth($str)
804805
/** @internal */
805806
public function handleEnd()
806807
{
808+
if ($this->linebuffer !== '') {
809+
$this->processLine();
810+
}
811+
807812
if (!$this->closed) {
808813
$this->emit('end');
809814
$this->close();
@@ -839,14 +844,6 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
839844
return $dest;
840845
}
841846

842-
public function closeInput()
843-
{
844-
if ($this->linebuffer !== '') {
845-
$this->processLine();
846-
}
847-
$this->handleEnd();
848-
}
849-
850847
public function close()
851848
{
852849
if ($this->closed) {

tests/ReadlineTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,22 @@ public function testEmitErrorWillEmitErrorAndClose()
981981

982982
public function testEmitEndWillEmitEndAndClose()
983983
{
984+
$this->readline->on('data', $this->expectCallableNever());
985+
$this->readline->on('end', $this->expectCallableOnce());
986+
$this->readline->on('close', $this->expectCallableOnce());
987+
988+
$this->input->emit('end');
989+
990+
$this->assertFalse($this->readline->isReadable());
991+
}
992+
993+
public function testEmitEndAfterDataWillEmitDataAndEndAndClose()
994+
{
995+
$this->readline->on('data', $this->expectCallableOnce('hello'));
984996
$this->readline->on('end', $this->expectCallableOnce());
985997
$this->readline->on('close', $this->expectCallableOnce());
986998

999+
$this->input->emit('data', array('hello'));
9871000
$this->input->emit('end');
9881001

9891002
$this->assertFalse($this->readline->isReadable());

0 commit comments

Comments
 (0)