forked from clue/reactphp-term
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-codes.php
More file actions
35 lines (28 loc) · 1.12 KB
/
remove-codes.php
File metadata and controls
35 lines (28 loc) · 1.12 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
<?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 Clue\React\Term\ControlCodeParser;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
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 ReadableResourceStream(STDIN);
$parser = new ControlCodeParser($stdin);
// pipe data from STDIN to STDOUT without any codes
$stdout = new WritableResourceStream(STDOUT);
$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);
}
});