Go to file
R. Eric Wheeler aa72be226d Updated constants to match C declarations 2021-03-01 10:46:07 -08:00
src/Console Updated constants to match C declarations 2021-03-01 10:46:07 -08:00
tests Updated libgetch, now includes cinPeek function 2021-02-27 19:10:44 -08:00
.gitignore Add tests 2021-01-25 12:35:32 -08:00
.gitlab-ci.yml Update gitlab-ci 2021-01-25 14:03:39 -08:00
.php_cs.dist Initial commit 2020-12-22 14:19:32 -08:00
LICENSE Initial commit 2020-12-22 14:19:32 -08:00
README.md Update gitlab-ci 2021-01-25 14:03:39 -08:00
composer.json Add tests 2021-01-25 12:35:32 -08:00
composer.lock Implement returns of special character codes 2021-02-15 11:44:39 -08:00
functions.php Add tests 2021-01-25 12:35:32 -08:00
header.txt Initial commit 2020-12-22 14:19:32 -08:00
phpunit.xml Add tests 2021-01-25 12:35:32 -08:00

README.md

getch

This simply uses the FFI extension to enable _getch and _ungetch in Windows and linux.

pipeline status coverage report

$ composer require sikofitt/getch:dev-master
 use Sikofitt\Console\Getch;
 $g = new Getch($linuxLibrary = null); // can also be a library that implements a function called _getch;
                                       // by default uses the bundled Resources/libgetch.so
                                       // on windows uses the built in _getch function.
 $ord = $g->getch();
 print \chr($ord);
 
 $ord = $g->ungetch('A');
 $res = $g->getch();
 $ord === $res // 65

Note that if you want to put a word into the STDIN stack, you need to do it in reverse.


    foreach(\str_split(\strrev('Hello World!')) as $char) {
        ungetch($char);
    }

    $result = '';

    do {
        $ord = getch();
        $result .= \chr($ord);
   } while($ord !== ord('!'));

   print $result; // Hello World!

There are also helper functions called getch() and ungetch();

use function Sikofitt\Console\getch;
$ord = getch($linuxLibrary = null);
print \chr($ord);

$ord = ungetch('B');
$res = getch();
 $ord === $res // 66

Tests

vendor/bin/phpunit