Skip to content

Commit 50b5a6a

Browse files
committed
Explicitly end input stream on CTRL+D
1 parent 0cf3696 commit 50b5a6a

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

examples/01-periodic.php

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

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

22-
$timer->cancel();
22+
$loop->cancelTimer($timer);
23+
$stdio->end();
24+
});
25+
26+
// cancel periodic timer if STDIN closed
27+
$stdio->on('end', function () use ($stdio, $loop, $timer) {
28+
$loop->cancelTimer($timer);
2329
$stdio->end();
2430
});
2531

src/Readline.php

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

5152
"\033[A" => 'onKeyUp',
5253
"\033[B" => 'onKeyDown',
@@ -838,6 +839,14 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
838839
return $dest;
839840
}
840841

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

src/Stdio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function handleError(\Exception $e)
241241
/** @internal */
242242
public function handleEnd()
243243
{
244-
$this->emit('end', array());
244+
$this->emit('end');
245245
}
246246

247247
/** @internal */

tests/ReadlineTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ public function testDataEventWillBeEmittedForEmptyLine()
223223
$this->input->emit('data', array("\n"));
224224
}
225225

226+
public function testEndInputWithoutDataOnCtrlD()
227+
{
228+
$this->readline->on('data', $this->expectCallableNever());
229+
$this->readline->on('end', $this->expectCallableOnce());
230+
$this->readline->on('close', $this->expectCallableOnce());
231+
232+
$this->input->emit('data', array("\x04"));
233+
}
234+
235+
public function testEndInputWithIncompleteLineOnCtrlD()
236+
{
237+
$this->readline->on('data', $this->expectCallableOnceWith('hello'));
238+
$this->readline->on('end', $this->expectCallableOnce());
239+
$this->readline->on('close', $this->expectCallableOnce());
240+
241+
$this->input->emit('data', array("hello\x04"));
242+
}
243+
226244
public function testWriteSimpleCharWritesOnce()
227245
{
228246
$this->output->expects($this->once())->method('write')->with($this->equalTo("\r\033[K" . "k"));

0 commit comments

Comments
 (0)