getch/README.md

60 lines
1.6 KiB
Markdown
Raw Normal View History

2020-12-22 14:19:32 -08:00
# getch
2021-01-25 12:35:32 -08:00
This simply uses the FFI extension to enable _getch and _ungetch in Windows and linux.
2020-12-22 14:19:32 -08:00
2022-05-17 10:04:07 -07:00
[![Pipeline status](https://code.bgemi.net/olive/PHP/getch/badges/1.x/pipeline.svg)](https://code.bgemi.net/olive/PHP/getch/-/commits/1.x)
[![Coverage report](https://code.bgemi.net/olive/PHP/getch/badges/1.x/coverage.svg)](https://code.bgemi.net/olive/PHP/getch/-/commits/1.x)
[![Latest Release](https://code.bgemi.net/olive/PHP/getch/-/badges/release.svg)](https://code.bgemi.net/olive/PHP/getch/-/releases)
2021-01-25 14:03:39 -08:00
2020-12-22 14:19:32 -08:00
```shell script
2022-05-17 09:53:07 -07:00
$ composer require olivebbs/getch
2020-12-22 14:19:32 -08:00
```
```php
2022-05-17 09:53:07 -07:00
use Olive\Console\Getch;
2021-01-07 13:56:08 -08:00
$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.
2021-01-25 12:35:32 -08:00
$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.
```php
foreach(\str_split(\strrev('Hello World!')) as $char) {
ungetch($char);
}
$result = '';
do {
$ord = getch();
$result .= \chr($ord);
} while($ord !== ord('!'));
print $result; // Hello World!
2020-12-22 14:19:32 -08:00
```
2021-01-25 12:35:32 -08:00
There are also helper functions called getch() and ungetch();
2020-12-22 14:19:32 -08:00
```php
2022-05-17 09:53:07 -07:00
use function Olive\Console\getch;
2021-01-25 12:35:32 -08:00
$ord = getch($linuxLibrary = null);
print \chr($ord);
$ord = ungetch('B');
$res = getch();
$ord === $res // 66
2020-12-22 14:19:32 -08:00
```
## Tests
2021-01-25 12:35:32 -08:00
vendor/bin/phpunit