public function __construct(ReadableStreamInterface $input, WritableStreamInterface $output) { $this->input = $input; $this->output = $output; if (!$this->input->isReadable()) { return $this->close(); } // push input through control code parser $parser = new ControlCodeParser($input); $that = $this; $codes = array("\n" => 'onKeyEnter', "" => 'onKeyBackspace', "\t" => 'onKeyTab', "[A" => 'onKeyUp', "[B" => 'onKeyDown', "[C" => 'onKeyRight', "[D" => 'onKeyLeft', "[1~" => 'onKeyHome', "[2~" => 'onKeyInsert', "[3~" => 'onKeyDelete', "[4~" => 'onKeyEnd'); $decode = function ($code) use($codes, $that) { if (isset($codes[$code])) { $method = $codes[$code]; $that->{$method}($code); return; } }; $parser->on('csi', $decode); $parser->on('c0', $decode); // push resulting data through utf8 sequencer $utf8 = new Utf8Sequencer($parser); $utf8->on('data', array($this, 'onFallback')); // process all stream events (forwarded from input stream) $utf8->on('end', array($this, 'handleEnd')); $utf8->on('error', array($this, 'handleError')); $utf8->on('close', array($this, 'close')); }
public function testPipingReadme() { $loop = Factory::create(); $input = new Stream(fopen(__DIR__ . '/../README.md', 'r'), $loop); $parser = new ControlCodeParser($input); $buffer = ''; $parser->on('data', function ($chunk) use(&$buffer) { $buffer .= $chunk; }); $loop->run(); $readme = str_replace("\n", '', file_get_contents(__DIR__ . '/../README.md')); $this->assertEquals($readme, $buffer); }
// // 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 = "[" . 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) {
// // 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; 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); $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; }); $loop->run();
// 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();