forked from clue/reactphp-term
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-colors.php
More file actions
50 lines (39 loc) · 1.62 KB
/
random-colors.php
File metadata and controls
50 lines (39 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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
require __DIR__ . '/../vendor/autoload.php';
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 React\Stream\ReadableResourceStream(STDIN);
$parser = new Clue\React\Term\ControlCodeParser($stdin);
$stdout = new React\Stream\WritableResourceStream(STDOUT);
// 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"));