Skip to content

Commit dc6294e

Browse files
committed
Use proper streaming interfaces for Readline input and output
1 parent b6cf7f3 commit dc6294e

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/Readline.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ class Readline extends EventEmitter
3333
private $history = null;
3434
private $encoding = 'utf-8';
3535

36+
private $input;
3637
private $output;
3738
private $sequencer;
3839

3940
public function __construct(ReadableStreamInterface $input, WritableStreamInterface $output)
4041
{
41-
// input data emits a single char into readline
42-
$input->on('data', array($this, 'onChar'));
43-
4442
$this->output = $output;
4543

4644
$this->sequencer = new Sequencer();
@@ -89,6 +87,9 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
8987
$this->sequencer->addFallback(self::ESC_SEQUENCE, function ($bytes) {
9088
echo 'unknown sequence: ' . ord($bytes) . PHP_EOL;
9189
});
90+
91+
// input data emits a single char into readline
92+
$input->on('data', array($this->sequencer, 'push'));
9293
}
9394

9495
/**
@@ -374,7 +375,7 @@ public function redraw()
374375
// write output, then move back $reverse chars (by sending backspace)
375376
$output .= $buffer . str_repeat("\x08", $this->strwidth($buffer) - $this->getCursorCell());
376377
}
377-
$this->write($output);
378+
$this->output->write($output);
378379

379380
return $this;
380381
}
@@ -394,18 +395,12 @@ public function redraw()
394395
public function clear()
395396
{
396397
if ($this->prompt !== '' || ($this->echo !== false && $this->linebuffer !== '')) {
397-
$this->write("\r\033[K");
398+
$this->output->write("\r\033[K");
398399
}
399400

400401
return $this;
401402
}
402403

403-
/** @internal */
404-
public function onChar($char)
405-
{
406-
$this->sequencer->push($char);
407-
}
408-
409404
/** @internal */
410405
public function onKeyBackspace()
411406
{
@@ -454,7 +449,7 @@ public function onKeyTab()
454449
public function onKeyEnter()
455450
{
456451
if ($this->echo !== false) {
457-
$this->write("\n");
452+
$this->output->write("\n");
458453
}
459454
$this->processLine();
460455
}
@@ -575,9 +570,4 @@ private function strwidth($str)
575570
{
576571
return mb_strwidth($str, $this->encoding);
577572
}
578-
579-
protected function write($data)
580-
{
581-
$this->output->write($data);
582-
}
583573
}

tests/ReadlineTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22

33
use Clue\React\Stdio\Readline;
4+
use React\Stream\ReadableStream;
45

56
class ReadlineTest extends TestCase
67
{
8+
private $input;
9+
private $output;
10+
private $readline;
11+
712
public function setUp()
813
{
9-
$this->input = $this->getMock('React\Stream\ReadableStreamInterface');
10-
$this->output = $this->getMockBuilder('Clue\React\Stdio\Stdout')->disableOriginalConstructor()->getMock();
14+
$this->input = new ReadableStream();
15+
$this->output = $this->getMock('React\Stream\WritableStreamInterface');
16+
1117
$this->readline = new Readline($this->input, $this->output);
1218
}
1319

@@ -480,7 +486,7 @@ public function testSetInputDuringEmitKeepsInput()
480486
private function pushInputBytes(Readline $readline, $bytes)
481487
{
482488
foreach (str_split($bytes, 1) as $byte) {
483-
$readline->onChar($byte);
489+
$this->input->emit('data', array($byte));
484490
}
485491
}
486492
}

0 commit comments

Comments
 (0)