forked from clue/reactphp-term
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdin-codes.php
More file actions
38 lines (31 loc) · 1.11 KB
/
stdin-codes.php
File metadata and controls
38 lines (31 loc) · 1.11 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
<?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
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);
$decoder = function ($code) {
echo 'Code:';
for ($i = 0; isset($code[$i]); ++$i) {
echo sprintf(" %02X", ord($code[$i]));
}
echo PHP_EOL;
};
$parser->on('csi', $decoder);
$parser->on('osc', $decoder);
$parser->on('c1', $decoder);
$parser->on('c0', $decoder);
$parser->on('data', function ($bytes) {
echo 'Data: ' . $bytes . PHP_EOL;
});