Skip to content

Commit 48b53db

Browse files
authored
Merge pull request #14 from clue-labs/examples
Add more examples
2 parents b741435 + ded2100 commit 48b53db

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

examples/random-colors.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
// this simple example reads from STDIN, assigns a random text color and then prints to STDOUT.
4+
// you can run this example and notice anything you type will get a random color:
5+
// $ php random-colors.php
6+
//
7+
// you can also pipe the output of other commands into this like this:
8+
// $ echo hello | php random-colors.php
9+
//
10+
// notice how if the input contains any colors to begin with, they will be replaced
11+
// with random colors:
12+
// $ phpunit --color=always | php random-colors.php
13+
14+
use React\Stream\Stream;
15+
use React\EventLoop\Factory;
16+
use Clue\React\Term\ControlCodeParser;
17+
18+
require __DIR__ . '/../vendor/autoload.php';
19+
20+
$loop = Factory::create();
21+
22+
if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
23+
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
24+
shell_exec('stty -icanon -echo');
25+
}
26+
27+
// process control codes from STDIN
28+
$stdin = new Stream(STDIN, $loop);
29+
$parser = new ControlCodeParser($stdin);
30+
31+
$stdout = new Stream(STDOUT, $loop);
32+
$stdout->pause();
33+
34+
// pass all c0 codes through to output
35+
$parser->on('c0', array($stdout, 'write'));
36+
37+
// replace any color codes (SGR) with a random color
38+
$parser->on('csi', function ($code) use ($stdout) {
39+
// we read any color code (SGR) on the input
40+
// assign a new random foreground and background color instead
41+
if (substr($code, -1) === 'm') {
42+
$code = "\033[" . mt_rand(30, 37) . ';' . mt_rand(40, 47) . "m";
43+
}
44+
45+
$stdout->write($code);
46+
});
47+
48+
// reset to default color at the end
49+
$stdin->on('close', function() use ($stdout) {
50+
$stdout->write("\033[m");
51+
});
52+
53+
// pass plain data to output
54+
$parser->pipe($stdout, array('end' => false));
55+
56+
// start with random color
57+
$stdin->emit('data', array("\033[m"));
58+
59+
$loop->run();

examples/remove-codes.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
// this simple example reads from STDIN, removes ALL codes and then prints to STDOUT.
4+
// you can run this example and notice that special keys will be filtered out:
5+
// $ php remove-codes.php
6+
//
7+
// you can also pipe the output of other commands into this to remove any control
8+
// codes like this:
9+
// $ phpunit --color=always | php remove-codes.php
10+
11+
use React\Stream\Stream;
12+
use React\EventLoop\Factory;
13+
use Clue\React\Term\ControlCodeParser;
14+
15+
require __DIR__ . '/../vendor/autoload.php';
16+
17+
$loop = Factory::create();
18+
19+
if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
20+
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
21+
shell_exec('stty -icanon -echo');
22+
}
23+
24+
// process control codes from STDIN
25+
$stdin = new Stream(STDIN, $loop);
26+
$parser = new ControlCodeParser($stdin);
27+
28+
$stdout = new Stream(STDOUT, $loop);
29+
$stdout->pause();
30+
31+
// pipe data from STDIN to STDOUT without any codes
32+
$parser->pipe($stdout);
33+
34+
// only forward \r, \n and \t
35+
$parser->on('c0', function ($code) use ($stdout) {
36+
if ($code === "\n" || $code === "\r" || $code === "\t") {
37+
$stdout->write($code);
38+
}
39+
});
40+
41+
$loop->run();

examples/stdin-codes.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
// this example prints anything you type on STDIN to STDOUT
4+
// control codes will be given as their hex values for easier inspection.
5+
// also try this out with special keys on your keyboard:
6+
// $ php stdin-codes.php
7+
//
8+
// you can also pipe the output of other commands into this to see any control
9+
// codes like this:
10+
// $ phpunit --color=always | php stdin-codes.php
11+
312
use React\Stream\Stream;
413
use React\EventLoop\Factory;
514
use Clue\React\Term\ControlCodeParser;
@@ -8,8 +17,10 @@
817

918
$loop = Factory::create();
1019

11-
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
12-
shell_exec('stty -icanon -echo');
20+
if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
21+
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
22+
shell_exec('stty -icanon -echo');
23+
}
1324

1425
// process control codes from STDIN
1526
$stdin = new Stream(STDIN, $loop);

0 commit comments

Comments
 (0)