Go to file
R. Eric Wheeler 24e53cd2cc Update master 2024-07-18 13:13:34 -07:00
src/Console/Resources Update master 2024-07-18 13:13:34 -07:00
tests Update master 2024-07-18 13:13:34 -07:00
tools/php-cs-fixer Update dependencies 2024-07-18 12:34:40 -07:00
.gitignore Update dependencies 2024-07-18 12:34:40 -07:00
.gitlab-ci.yml Merge 1.x into master 2022-05-17 18:15:13 +00:00
.php-cs-fixer.dist.php Updated header comment 2024-07-18 12:44:34 -07:00
LICENSE Initial commit 2020-12-22 14:19:32 -08:00
README.md Update master 2024-07-18 13:13:34 -07:00
composer.json Update master 2024-07-18 13:13:34 -07:00
functions.php Update master 2024-07-18 13:13:34 -07:00
header.txt Updated header comment 2024-07-18 12:44:34 -07:00
phpunit.xml Update dependencies 2024-07-18 12:34:40 -07:00

README.md

getch

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

$ composer require olivebbs/getch
 use Olive\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 Olive\Console\getch;
$ord = getch($linuxLibrary = null);
print \chr($ord);

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

Tests

vendor/bin/phpunit