diff --git a/examples/random-colors.php b/examples/random-colors.php new file mode 100644 index 0000000..6d0e67c --- /dev/null +++ b/examples/random-colors.php @@ -0,0 +1,59 @@ +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 = "\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")); + +$loop->run(); diff --git a/examples/remove-codes.php b/examples/remove-codes.php new file mode 100644 index 0000000..a4e0242 --- /dev/null +++ b/examples/remove-codes.php @@ -0,0 +1,41 @@ +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(); diff --git a/examples/stdin-codes.php b/examples/stdin-codes.php index ee82fb9..3362f1d 100644 --- a/examples/stdin-codes.php +++ b/examples/stdin-codes.php @@ -1,5 +1,14 @@