Skip to content
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
78 changes: 0 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ built on top of for [ReactPHP](https://reactphp.org).
* [Cursor](#cursor)
* [History](#history)
* [Autocomplete](#autocomplete)
* [Advanced](#advanced)
* [Stdout](#stdout)
* [Stdin](#stdin)
* [Pitfalls](#pitfalls)
* [Install](#install)
* [Tests](#tests)
Expand Down Expand Up @@ -82,23 +79,6 @@ $stdio->write('hello');
$stdio->write(" world\n");
```

[Deprecated] The `writeln($line)` method can be used to print a line to console output.
A trailing newline will be added automatically.

```php
// deprecated
$stdio->writeln('hello world');
```

[Deprecated] The `overwrite($text)` method can be used to overwrite/replace the last
incomplete line with the given text:

```php
$stdio->write('Loading…');
// deprecated
$stdio->overwrite('Done!');
```

Alternatively, you can also use the `Stdio` as a writable stream.
You can `pipe()` any readable stream into this stream.

Expand Down Expand Up @@ -126,19 +106,6 @@ streams.
You can control various aspects of the console input through the [`Readline`](#readline),
so read on..

[Deprecated] It will emit a `line` event for every line read from console input.
The event will contain the input buffer as-is, without the trailing newline.
You can register any number of event handlers like this:

```php
// deprecated
$stdio->on('line', function ($line) {
if ($line === 'start') {
doSomething();
}
});
```

### Readline

The [`Readline`](#readline) class is responsible for reacting to user input and presenting a prompt to the user.
Expand Down Expand Up @@ -486,51 +453,6 @@ disable the autocomplete function:
$readline->setAutocomplete(null);
```

### Advanced

#### Stdout

[Deprecated] The `Stdout` represents a `WritableStream` and is responsible for handling console output.

Interfacing with it directly is *not recommended* and considered *advanced usage*.

If you want to print some text to console output, use the [`Stdio::write()`](#output) instead:

```php
$stdio->write('hello');
```

Should you need to interface with the `Stdout`, you can access the current instance through the [`Stdio`](#stdio):

```php
// deprecated
$stdout = $stdio->getOutput();
```

#### Stdin

[Deprecated] The `Stdin` represents a `ReadableStream` and is responsible for handling console input.

Interfacing with it directly is *not recommended* and considered *advanced usage*.

If you want to read a line from console input, use the [`Stdio::on()`](#input) instead:

```php
$stdio->on('data', function ($line) use ($stdio) {
$line = rtrim($line, "\r\n");
$stdio->write('You said "' . $line . '"' . PHP_EOL);
});
```

Should you need to interface with the `Stdin`, you can access the current instance through the [`Stdio`](#stdio):

You can access the current instance through the [`Stdio`](#stdio):

```php
// deprecated
$stdin = $stdio->getInput();
```

## Pitfalls

The [`Readline`](#readline) has to redraw the current user
Expand Down
4 changes: 2 additions & 2 deletions src/Stdin.php → src/Io/Stdin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Clue\React\Stdio;
namespace Clue\React\Stdio\Io;

use React\Stream\Stream;
use React\EventLoop\LoopInterface;

/**
* @deprecated
* @internal
*/
class Stdin extends Stream
{
Expand Down
4 changes: 2 additions & 2 deletions src/Stdout.php → src/Io/Stdout.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Clue\React\Stdio;
namespace Clue\React\Stdio\Io;

use React\Stream\WritableStream;

/**
* @deprecated
* @internal
*/
class Stdout extends WritableStream
{
Expand Down
47 changes: 4 additions & 43 deletions src/Stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Clue\React\Stdio;

use Clue\React\Stdio\Io\Stdin;
use Clue\React\Stdio\Io\Stdout;
use Evenement\EventEmitter;
use React\Stream\DuplexStreamInterface;
use React\EventLoop\LoopInterface;
use React\Stream\DuplexStreamInterface;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;

class Stdio extends EventEmitter implements DuplexStreamInterface
{
Expand Down Expand Up @@ -47,9 +49,6 @@ public function __construct(LoopInterface $loop, ReadableStreamInterface $input

// emit data with trailing newline in order to preserve readable API
$that->emit('data', array($line . PHP_EOL));

// emit custom line event for ease of use
$that->emit('line', array($line, $that));
});

// handle all input events (readline forwards all input events)
Expand Down Expand Up @@ -162,28 +161,6 @@ public function write($data)
}
}

/**
* @deprecated
*/
public function writeln($line)
{
$this->write($line . PHP_EOL);
}

/**
* @deprecated
*/
public function overwrite($data = '')
{
if ($this->incompleteLine !== '') {
// move one line up, move to start of line and clear everything
$data = "\033[A\r\033[K" . $data;
$this->incompleteLine = '';
}

$this->write($data);
}

public function end($data = null)
{
if ($this->ending) {
Expand Down Expand Up @@ -217,22 +194,6 @@ public function close()
$this->output->close();
}

/**
* @deprecated
*/
public function getInput()
{
return $this->input;
}

/**
* @deprecated
*/
public function getOutput()
{
return $this->output;
}

public function getReadline()
{
return $this->readline;
Expand Down
81 changes: 4 additions & 77 deletions tests/StdioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testCtorDefaultArgs()
$stdio->close();
}

public function testCtorArgsWillBeReturnedByGetters()
public function testCtorReadlineArgWillBeReturnedBygetReadline()
{
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
Expand All @@ -31,8 +31,6 @@ public function testCtorArgsWillBeReturnedByGetters()

$stdio = new Stdio($this->loop, $input, $output, $readline);

$this->assertSame($input, $stdio->getInput());
$this->assertSame($output, $stdio->getOutput());
$this->assertSame($readline, $stdio->getReadline());
}

Expand Down Expand Up @@ -168,77 +166,7 @@ public function testWriteAfterReadlineInputWillClearReadlineWriteOutputAndRestor
$buffer .= $data;
}));

$stdio->writeln('test');

$this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer);
}

public function testOverwriteWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline()
{
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
$readline = new Readline($input, $output);
$readline->setPrompt('> ');
$readline->setInput('input');

$stdio = new Stdio($this->loop, $input, $output, $readline);

$stdio->write('first');

$buffer = '';
$output->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
}));

$stdio->overwrite('overwrite');

$this->assertEquals("\r\033[K" . "\033[A" . "\r\033[K" . "overwrite\n" . "> input", $buffer);
}

public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndRestoreReadline()
{
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
$readline = new Readline($input, $output);
$readline->setPrompt('> ');
$readline->setInput('input');

$stdio = new Stdio($this->loop, $input, $output, $readline);

$stdio->write("first\n");

$buffer = '';
$output->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
}));

$stdio->overwrite('overwrite');

$this->assertEquals("\r\033[K" . "overwrite\n" . "> input", $buffer);
}

public function testWriteLineWillClearReadlineWriteOutputAndRestoreReadline()
{
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
$readline = new Readline($input, $output);
$readline->setPrompt('> ');
$readline->setInput('input');

$stdio = new Stdio($this->loop, $input, $output, $readline);

$buffer = '';
$output->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
}));

$stdio->writeln('test');
$stdio->write("test\n");

$this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer);
}
Expand All @@ -260,8 +188,8 @@ public function testWriteTwoLinesWillClearReadlineWriteOutputAndRestoreReadline(
$buffer .= $data;
}));

$stdio->writeln('hello');
$stdio->writeln('world');
$stdio->write("hello\n");
$stdio->write("world\n");

$this->assertEquals("\r\033[K" . "hello\n" . "> input" . "\r\033[K" . "world\n" . "> input", $buffer);
}
Expand Down Expand Up @@ -454,7 +382,6 @@ public function testDataEventWillBeForwarded()
$stdio = new Stdio($this->loop, $input, $output, $readline);

$stdio->on('data', $this->expectCallableOnceWith("hello\n"));
$stdio->on('line', $this->expectCallableOnceWith('hello'));

$readline->emit('data', array('hello'));
}
Expand Down