Skip to content

Explicitly end stream on CTRL+D and emit final data on EOF without EOL #56

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 2 commits into from
Nov 1, 2017
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
12 changes: 9 additions & 3 deletions examples/01-periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
});

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

$timer->cancel();
$loop->cancelTimer($timer);
$stdio->end();
});

// cancel periodic timer if STDIN closed
$stdio->on('end', function () use ($stdio, $loop, $timer) {
$loop->cancelTimer($timer);
$stdio->end();
});

Expand Down
6 changes: 6 additions & 0 deletions src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
"\x7f" => 'onKeyBackspace',
"\t" => 'onKeyTab',

"\x04" => 'handleEnd', // CTRL+D

"\033[A" => 'onKeyUp',
"\033[B" => 'onKeyDown',
"\033[C" => 'onKeyRight',
Expand Down Expand Up @@ -803,6 +805,10 @@ public function strwidth($str)
/** @internal */
public function handleEnd()
{
if ($this->linebuffer !== '') {
$this->processLine();
}

if (!$this->closed) {
$this->emit('end');
$this->close();
Expand Down
2 changes: 1 addition & 1 deletion src/Stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function handleError(\Exception $e)
/** @internal */
public function handleEnd()
{
$this->emit('end', array());
$this->emit('end');
}

/** @internal */
Expand Down
31 changes: 31 additions & 0 deletions tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ public function testDataEventWillBeEmittedForEmptyLine()
$this->input->emit('data', array("\n"));
}

public function testEndInputWithoutDataOnCtrlD()
{
$this->readline->on('data', $this->expectCallableNever());
$this->readline->on('end', $this->expectCallableOnce());
$this->readline->on('close', $this->expectCallableOnce());

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

public function testEndInputWithIncompleteLineOnCtrlD()
{
$this->readline->on('data', $this->expectCallableOnceWith('hello'));
$this->readline->on('end', $this->expectCallableOnce());
$this->readline->on('close', $this->expectCallableOnce());

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

public function testWriteSimpleCharWritesOnce()
{
$this->output->expects($this->once())->method('write')->with($this->equalTo("\r\033[K" . "k"));
Expand Down Expand Up @@ -963,9 +981,22 @@ public function testEmitErrorWillEmitErrorAndClose()

public function testEmitEndWillEmitEndAndClose()
{
$this->readline->on('data', $this->expectCallableNever());
$this->readline->on('end', $this->expectCallableOnce());
$this->readline->on('close', $this->expectCallableOnce());

$this->input->emit('end');

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

public function testEmitEndAfterDataWillEmitDataAndEndAndClose()
{
$this->readline->on('data', $this->expectCallableOnce('hello'));
$this->readline->on('end', $this->expectCallableOnce());
$this->readline->on('close', $this->expectCallableOnce());

$this->input->emit('data', array('hello'));
$this->input->emit('end');

$this->assertFalse($this->readline->isReadable());
Expand Down