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
59 changes: 59 additions & 0 deletions examples/random-colors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

// this simple example reads from STDIN, assigns a random text color and then prints to STDOUT.
// you can run this example and notice anything you type will get a random color:
// $ php random-colors.php
//
// you can also pipe the output of other commands into this like this:
// $ echo hello | php random-colors.php
//
// notice how if the input contains any colors to begin with, they will be replaced
// with random colors:
// $ phpunit --color=always | php random-colors.php

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

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 Stream(STDIN, $loop);
$parser = new ControlCodeParser($stdin);

$stdout = new Stream(STDOUT, $loop);
$stdout->pause();

// pass all c0 codes through to output
$parser->on('c0', array($stdout, 'write'));

// replace any color codes (SGR) with a random color
$parser->on('csi', function ($code) use ($stdout) {
// we read any color code (SGR) on the input
// assign a new random foreground and background color instead
if (substr($code, -1) === 'm') {
$code = "\033[" . mt_rand(30, 37) . ';' . mt_rand(40, 47) . "m";
}

$stdout->write($code);
});

// reset to default color at the end
$stdin->on('close', function() use ($stdout) {
$stdout->write("\033[m");
});

// pass plain data to output
$parser->pipe($stdout, array('end' => false));

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

$loop->run();
41 changes: 41 additions & 0 deletions examples/remove-codes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

// this simple example reads from STDIN, removes ALL codes and then prints to STDOUT.
// you can run this example and notice that special keys will be filtered out:
// $ php remove-codes.php
//
// you can also pipe the output of other commands into this to remove any control
// codes like this:
// $ phpunit --color=always | php remove-codes.php

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

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 Stream(STDIN, $loop);
$parser = new ControlCodeParser($stdin);

$stdout = new Stream(STDOUT, $loop);
$stdout->pause();

// pipe data from STDIN to STDOUT without any codes
$parser->pipe($stdout);

// only forward \r, \n and \t
$parser->on('c0', function ($code) use ($stdout) {
if ($code === "\n" || $code === "\r" || $code === "\t") {
$stdout->write($code);
}
});

$loop->run();
15 changes: 13 additions & 2 deletions examples/stdin-codes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

// this example prints anything you type on STDIN to STDOUT
// control codes will be given as their hex values for easier inspection.
// also try this out with special keys on your keyboard:
// $ php stdin-codes.php
//
// you can also pipe the output of other commands into this to see any control
// codes like this:
// $ phpunit --color=always | php stdin-codes.php

use React\Stream\Stream;
use React\EventLoop\Factory;
use Clue\React\Term\ControlCodeParser;
Expand All @@ -8,8 +17,10 @@

$loop = Factory::create();

// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
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 Stream(STDIN, $loop);
Expand Down