Skip to content

Commit 88f9dfa

Browse files
committed
Support piping to STDIN
1 parent cde3a4c commit 88f9dfa

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

examples/stdin-codes.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
// this example prints anything you type on STDIN to STDOUT
4+
// control codes will be given as their hex values for easier inspection.
5+
// also try this out with special keys on your keyboard:
6+
// $ php stdin-codes.php
7+
//
8+
// you can also pipe the output of other commands into this to see any control
9+
// codes like this:
10+
// $ phpunit --color=always | php stdin-codes.php
11+
312
use React\Stream\Stream;
413
use React\EventLoop\Factory;
514
use Clue\React\Term\ControlCodeParser;
@@ -8,8 +17,10 @@
817

918
$loop = Factory::create();
1019

11-
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
12-
shell_exec('stty -icanon -echo');
20+
if (function_exists('posix_isatty') && posix_isatty(STDIN)) {
21+
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
22+
shell_exec('stty -icanon -echo');
23+
}
1324

1425
// process control codes from STDIN
1526
$stdin = new Stream(STDIN, $loop);

0 commit comments

Comments
 (0)