Skip to content

Commit

Permalink
Simplify usage by supporting new default loop
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed Aug 4, 2021
1 parent af4441c commit 26df371
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ It wraps a given `ReadableStreamInterface` and exposes its plain data through
the same interface.

```php
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new React\Stream\ReadableResourceStream(STDIN);

$stream = new ControlCodeParser($stdin);

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
},
"require": {
"php": ">=5.3",
"react/stream": "^1.0 || ^0.7"
"react/stream": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3"
"react/event-loop": "^1.2"
}
}
9 changes: 2 additions & 7 deletions examples/random-colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@
// with random colors:
// $ phpunit --color=always | php random-colors.php

use React\EventLoop\Factory;
use Clue\React\Term\ControlCodeParser;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
}

// process control codes from STDIN
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);
$parser = new ControlCodeParser($stdin);

$stdout = new WritableResourceStream(STDOUT, $loop);
$stdout = new WritableResourceStream(STDOUT);

// pass all c0 codes through to output
$parser->on('c0', array($stdout, 'write'));
Expand All @@ -55,5 +52,3 @@

// start with random color
$stdin->emit('data', array("\033[m"));

$loop->run();
9 changes: 2 additions & 7 deletions examples/remove-codes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@
// codes like this:
// $ phpunit --color=always | php remove-codes.php

use React\EventLoop\Factory;
use Clue\React\Term\ControlCodeParser;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
}

// process control codes from STDIN
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);
$parser = new ControlCodeParser($stdin);

// pipe data from STDIN to STDOUT without any codes
$stdout = new WritableResourceStream(STDOUT, $loop);
$stdout = new WritableResourceStream(STDOUT);
$parser->pipe($stdout);

// only forward \r, \n and \t
Expand All @@ -36,5 +33,3 @@
$stdout->write($code);
}
});

$loop->run();
7 changes: 1 addition & 6 deletions examples/stdin-codes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@
// codes like this:
// $ phpunit --color=always | php stdin-codes.php

use React\EventLoop\Factory;
use Clue\React\Term\ControlCodeParser;
use React\Stream\ReadableResourceStream;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
}

// process control codes from STDIN
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);
$parser = new ControlCodeParser($stdin);

$decoder = function ($code) {
Expand All @@ -42,5 +39,3 @@
$parser->on('data', function ($bytes) {
echo 'Data: ' . $bytes . PHP_EOL;
});

$loop->run();
7 changes: 3 additions & 4 deletions tests/FunctionalControlCodeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
namespace Clue\Tests\React\Term;

use Clue\React\Term\ControlCodeParser;
use React\EventLoop\Loop;
use React\EventLoop\Factory;
use React\Stream\ReadableResourceStream;

class FunctionalControlCodeParserTest extends TestCase
{
public function testPipingReadme()
{
$loop = Factory::create();

$input = new ReadableResourceStream(fopen(__DIR__ . '/../README.md', 'r+'), $loop);
$input = new ReadableResourceStream(fopen(__DIR__ . '/../README.md', 'r+'));
$parser = new ControlCodeParser($input);

$buffer = '';
$parser->on('data', function ($chunk) use (&$buffer) {
$buffer .= $chunk;
});

$loop->run();
Loop::run();

$readme = str_replace(
"\n",
Expand Down

0 comments on commit 26df371

Please sign in to comment.