Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify examples by updating to new default loop #34

Merged
merged 1 commit into from
Aug 4, 2021
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
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"
Copy link
Owner

@clue clue Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the updated examples reference the event loop at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event loop is still necessary for some tests (see FunctionalControlCodeParserTest.php).

}
}
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();
8 changes: 3 additions & 5 deletions tests/FunctionalControlCodeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
namespace Clue\Tests\React\Term;

use Clue\React\Term\ControlCodeParser;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
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