Skip to content

Commit

Permalink
Use proper streaming interfaces for Readline input and output
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jun 10, 2016
1 parent b6cf7f3 commit dc6294e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
24 changes: 7 additions & 17 deletions src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ class Readline extends EventEmitter
private $history = null;
private $encoding = 'utf-8';

private $input;
private $output;
private $sequencer;

public function __construct(ReadableStreamInterface $input, WritableStreamInterface $output)
{
// input data emits a single char into readline
$input->on('data', array($this, 'onChar'));

$this->output = $output;

$this->sequencer = new Sequencer();
Expand Down Expand Up @@ -89,6 +87,9 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf
$this->sequencer->addFallback(self::ESC_SEQUENCE, function ($bytes) {
echo 'unknown sequence: ' . ord($bytes) . PHP_EOL;
});

// input data emits a single char into readline
$input->on('data', array($this->sequencer, 'push'));
}

/**
Expand Down Expand Up @@ -374,7 +375,7 @@ public function redraw()
// write output, then move back $reverse chars (by sending backspace)
$output .= $buffer . str_repeat("\x08", $this->strwidth($buffer) - $this->getCursorCell());
}
$this->write($output);
$this->output->write($output);

return $this;
}
Expand All @@ -394,18 +395,12 @@ public function redraw()
public function clear()
{
if ($this->prompt !== '' || ($this->echo !== false && $this->linebuffer !== '')) {
$this->write("\r\033[K");
$this->output->write("\r\033[K");
}

return $this;
}

/** @internal */
public function onChar($char)
{
$this->sequencer->push($char);
}

/** @internal */
public function onKeyBackspace()
{
Expand Down Expand Up @@ -454,7 +449,7 @@ public function onKeyTab()
public function onKeyEnter()
{
if ($this->echo !== false) {
$this->write("\n");
$this->output->write("\n");
}
$this->processLine();
}
Expand Down Expand Up @@ -575,9 +570,4 @@ private function strwidth($str)
{
return mb_strwidth($str, $this->encoding);
}

protected function write($data)
{
$this->output->write($data);
}
}
12 changes: 9 additions & 3 deletions tests/ReadlineTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php

use Clue\React\Stdio\Readline;
use React\Stream\ReadableStream;

class ReadlineTest extends TestCase
{
private $input;
private $output;
private $readline;

public function setUp()
{
$this->input = $this->getMock('React\Stream\ReadableStreamInterface');
$this->output = $this->getMockBuilder('Clue\React\Stdio\Stdout')->disableOriginalConstructor()->getMock();
$this->input = new ReadableStream();
$this->output = $this->getMock('React\Stream\WritableStreamInterface');

$this->readline = new Readline($this->input, $this->output);
}

Expand Down Expand Up @@ -480,7 +486,7 @@ public function testSetInputDuringEmitKeepsInput()
private function pushInputBytes(Readline $readline, $bytes)
{
foreach (str_split($bytes, 1) as $byte) {
$readline->onChar($byte);
$this->input->emit('data', array($byte));
}
}
}

0 comments on commit dc6294e

Please sign in to comment.