Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
R. Eric Wheeler | cf7da6c32c | |
R. Eric Wheeler | 48420375b4 | |
R. Eric Wheeler | d9af97eab2 |
|
@ -3,3 +3,4 @@
|
||||||
.php_cs.cache
|
.php_cs.cache
|
||||||
vendor/
|
vendor/
|
||||||
.phpunit.cache/
|
.phpunit.cache/
|
||||||
|
tools/php-cs-fixer/vendor/
|
||||||
|
|
|
@ -18,6 +18,7 @@ before_script:
|
||||||
- php composer.phar install
|
- php composer.phar install
|
||||||
|
|
||||||
# Run our tests
|
# Run our tests
|
||||||
|
# If Xdebug was installed you can generate a coverage report and see code coverage metrics.
|
||||||
test:7.4:
|
test:7.4:
|
||||||
only:
|
only:
|
||||||
- 1.x
|
- 1.x
|
||||||
|
@ -34,11 +35,3 @@ test:8.0:
|
||||||
image: php:8.0
|
image: php:8.0
|
||||||
script:
|
script:
|
||||||
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
||||||
test:8.1:
|
|
||||||
only:
|
|
||||||
- 1.x
|
|
||||||
tags:
|
|
||||||
- default
|
|
||||||
image: php:8.1
|
|
||||||
script:
|
|
||||||
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PhpCsFixer\Config;
|
||||||
|
|
||||||
|
$header = file_exists(__DIR__.'/header.txt') ? file_get_contents(__DIR__.'/header.txt') : '';
|
||||||
|
$finder = PhpCsFixer\Finder::create()
|
||||||
|
->in(__DIR__ . '/src')
|
||||||
|
->in(__DIR__ . '/tests')
|
||||||
|
;
|
||||||
|
return (new Config())
|
||||||
|
|
||||||
|
->setRules([
|
||||||
|
'@Symfony' => true,
|
||||||
|
'array_syntax' => ['syntax' => 'short'],
|
||||||
|
'header_comment' => ['header' => $header],
|
||||||
|
])
|
||||||
|
->setFinder($finder)
|
||||||
|
;
|
14
.php_cs.dist
14
.php_cs.dist
|
@ -1,14 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
$finder = PhpCsFixer\Finder::create()
|
|
||||||
->in(__DIR__)
|
|
||||||
->exclude('var')
|
|
||||||
;
|
|
||||||
|
|
||||||
return PhpCsFixer\Config::create()
|
|
||||||
->setRules([
|
|
||||||
'@Symfony' => true,
|
|
||||||
'array_syntax' => ['syntax' => 'short'],
|
|
||||||
])
|
|
||||||
->setFinder($finder)
|
|
||||||
;
|
|
11
README.md
11
README.md
|
@ -2,16 +2,15 @@
|
||||||
|
|
||||||
This simply uses the FFI extension to enable _getch and _ungetch in Windows and linux.
|
This simply uses the FFI extension to enable _getch and _ungetch in Windows and linux.
|
||||||
|
|
||||||
[![Pipeline status](https://code.bgemi.net/olive/PHP/getch/badges/1.x/pipeline.svg)](https://code.bgemi.net/olive/PHP/getch/-/commits/1.x)
|
[![pipeline status](https://repos.bgemi.net/sikofitt/getch/badges/1.x/pipeline.svg)](https://repos.bgemi.net/sikofitt/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)
|
[![coverage report](https://repos.bgemi.net/sikofitt/getch/badges/1.x/coverage.svg)](https://repos.bgemi.net/sikofitt/getch/-/commits/1.x)
|
||||||
[![Latest Release](https://code.bgemi.net/olive/PHP/getch/-/badges/release.svg)](https://code.bgemi.net/olive/PHP/getch/-/releases)
|
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
$ composer require olivebbs/getch
|
$ composer require sikofitt/getch:dev-master
|
||||||
```
|
```
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use Olive\Console\Getch;
|
use Sikofitt\Console\Getch;
|
||||||
$g = new Getch($linuxLibrary = null); // can also be a library that implements a function called _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
|
// by default uses the bundled Resources/libgetch.so
|
||||||
// on windows uses the built in _getch function.
|
// on windows uses the built in _getch function.
|
||||||
|
@ -46,7 +45,7 @@ Note that if you want to put a word into the STDIN stack, you need to do it in r
|
||||||
There are also helper functions called getch() and ungetch();
|
There are also helper functions called getch() and ungetch();
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use function Olive\Console\getch;
|
use function Sikofitt\Console\getch;
|
||||||
$ord = getch($linuxLibrary = null);
|
$ord = getch($linuxLibrary = null);
|
||||||
print \chr($ord);
|
print \chr($ord);
|
||||||
|
|
||||||
|
|
|
@ -2,18 +2,16 @@
|
||||||
"name": "olivebbs/getch",
|
"name": "olivebbs/getch",
|
||||||
"description": "Implements _getch and _ungetch for windows and linux using ffi",
|
"description": "Implements _getch and _ungetch for windows and linux using ffi",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"keywords": ["getch", "windows", "conio", "linux", "console", "conio.h", "hotkey", "termios"],
|
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.4",
|
"php": "^8.2",
|
||||||
"ext-ffi": "*"
|
"ext-ffi": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"friendsofphp/php-cs-fixer": "^2.18",
|
"phpunit/phpunit": "^9.6"
|
||||||
"phpunit/phpunit": "^9.5"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Olive\\Console\\": "src/Console/"
|
"Sikofitt\\Console\\": "src/Console/"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"functions.php"
|
"functions.php"
|
||||||
|
@ -21,7 +19,7 @@
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Olive\\Tests\\Console\\": "tests/"
|
"Sikofitt\\Tests\\Console\\": "tests/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"license": "MPL-2.0",
|
"license": "MPL-2.0",
|
||||||
|
@ -30,8 +28,5 @@
|
||||||
"name": "R. Eric Wheeler",
|
"name": "R. Eric Wheeler",
|
||||||
"email": "sikofitt@gmail.com"
|
"email": "sikofitt@gmail.com"
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"conflict": {
|
|
||||||
"sikofitt/getch": "*"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -3,7 +3,7 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
|
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
|
||||||
*
|
*
|
||||||
* This Source Code Form is subject to the
|
* This Source Code Form is subject to the
|
||||||
* terms of the Mozilla Public License, v. 2.0.
|
* terms of the Mozilla Public License, v. 2.0.
|
||||||
|
@ -12,22 +12,18 @@ declare(strict_types=1);
|
||||||
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Olive\Console\Getch;
|
use Sikofitt\Console\Getch;
|
||||||
|
|
||||||
if (!function_exists('getch')) {
|
if (!function_exists('getch')) {
|
||||||
function getch(string $linuxLibrary = null): int
|
function getch(?string $linuxLibrary = null): int
|
||||||
{
|
{
|
||||||
$g = new Getch($linuxLibrary);
|
return (new Getch($linuxLibrary))->getch();
|
||||||
|
|
||||||
return $g->getch();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('ungetch')) {
|
if (!function_exists('ungetch')) {
|
||||||
function ungetch($char, string $linuxLibrary = null): int
|
function ungetch($char, ?string $linuxLibrary = null): int
|
||||||
{
|
{
|
||||||
$g = new Getch($linuxLibrary);
|
return (new Getch($linuxLibrary))->ungetch($char);
|
||||||
|
|
||||||
return $g->ungetch($char);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
|
Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
|
||||||
|
|
||||||
This Source Code Form is subject to the
|
This Source Code Form is subject to the
|
||||||
terms of the Mozilla Public License, v. 2.0.
|
terms of the Mozilla Public License, v. 2.0.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
|
||||||
bootstrap="vendor/autoload.php"
|
bootstrap="vendor/autoload.php"
|
||||||
cacheResultFile=".phpunit.cache/test-results"
|
cacheResultFile=".phpunit.cache/test-results"
|
||||||
executionOrder="depends,defects"
|
executionOrder="depends,defects"
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
|
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
|
||||||
*
|
*
|
||||||
* This Source Code Form is subject to the
|
* This Source Code Form is subject to the
|
||||||
* terms of the Mozilla Public License, v. 2.0.
|
* terms of the Mozilla Public License, v. 2.0.
|
||||||
|
@ -12,10 +12,7 @@ declare(strict_types=1);
|
||||||
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Olive\Console;
|
namespace Sikofitt\Console;
|
||||||
|
|
||||||
use FFI;
|
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
final class Getch
|
final class Getch
|
||||||
{
|
{
|
||||||
|
@ -25,7 +22,7 @@ final class Getch
|
||||||
public const KEY_E0 = 0;
|
public const KEY_E0 = 0;
|
||||||
public const KEY_E1 = 224;
|
public const KEY_E1 = 224;
|
||||||
|
|
||||||
// Supported scan scodes.
|
// Supported scan codes.
|
||||||
public const KEY_F1 = 59;
|
public const KEY_F1 = 59;
|
||||||
public const KEY_F2 = 60;
|
public const KEY_F2 = 60;
|
||||||
public const KEY_F3 = 61;
|
public const KEY_F3 = 61;
|
||||||
|
@ -56,14 +53,14 @@ final class Getch
|
||||||
int _ungetch(int c);
|
int _ungetch(int c);
|
||||||
DECLARATIONS;
|
DECLARATIONS;
|
||||||
|
|
||||||
private static ?FFI $ffi = null;
|
private static ?\FFI $ffi = null;
|
||||||
|
|
||||||
public static function resetFFI(): void
|
public static function resetFFI(): void
|
||||||
{
|
{
|
||||||
static::$ffi = null;
|
self::$ffi = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(string $linuxLibrary = null)
|
public function __construct(?string $linuxLibrary = null)
|
||||||
{
|
{
|
||||||
if (null === $linuxLibrary) {
|
if (null === $linuxLibrary) {
|
||||||
$linuxLibrary = self::LINUX_LIBRARY;
|
$linuxLibrary = self::LINUX_LIBRARY;
|
||||||
|
@ -73,15 +70,15 @@ final class Getch
|
||||||
$osFamily = PHP_OS_FAMILY;
|
$osFamily = PHP_OS_FAMILY;
|
||||||
if ('Windows' === $osFamily) {
|
if ('Windows' === $osFamily) {
|
||||||
$declarations = self::DECLARATIONS.' int _kbhit();';
|
$declarations = self::DECLARATIONS.' int _kbhit();';
|
||||||
self::$ffi = FFI::cdef($declarations, self::WINDOWS_LIBRARY);
|
self::$ffi = \FFI::cdef($declarations, self::WINDOWS_LIBRARY);
|
||||||
} elseif ('Linux' === $osFamily) {
|
} elseif ('Linux' === $osFamily) {
|
||||||
if (!file_exists($linuxLibrary)) {
|
if (!file_exists($linuxLibrary)) {
|
||||||
throw new RuntimeException(sprintf('Could not find library file %s.', $linuxLibrary));
|
throw new \RuntimeException(sprintf('Could not find library file %s.', $linuxLibrary));
|
||||||
}
|
}
|
||||||
$declarations = self::DECLARATIONS.' int cinPeek();';
|
$declarations = self::DECLARATIONS.' int cinPeek();';
|
||||||
self::$ffi = FFI::cdef($declarations, $linuxLibrary);
|
self::$ffi = \FFI::cdef($declarations, $linuxLibrary);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
|
throw new \RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,9 +86,9 @@ final class Getch
|
||||||
public function peek(): int
|
public function peek(): int
|
||||||
{
|
{
|
||||||
if (PHP_OS_FAMILY === 'Windows') {
|
if (PHP_OS_FAMILY === 'Windows') {
|
||||||
if ($ffi->_kbhit()) {
|
if (self::$ffi->_kbhit()) {
|
||||||
$result = $ffi->_getch();
|
$result = self::$ffi->_getch();
|
||||||
$ffi->_ungetch($result);
|
self::$ffi->_ungetch($result);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
@ -99,7 +96,7 @@ final class Getch
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ffi->cinPeek();
|
return self::$ffi->cinPeek();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getch(): int
|
public function getch(): int
|
||||||
|
@ -107,12 +104,8 @@ final class Getch
|
||||||
return self::$ffi->_getch();
|
return self::$ffi->_getch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ungetch($char): int
|
public function ungetch(string|int $char): int
|
||||||
{
|
{
|
||||||
if (!is_string($char) && !is_int($char)) {
|
|
||||||
throw new \TypeError('ungetch takes a parameter of int or string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_string($char)) {
|
if (is_string($char)) {
|
||||||
$char = ord($char[0]);
|
$char = ord($char[0]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Olive\Tests\Console\Getch;
|
/*
|
||||||
|
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the
|
||||||
|
* terms of the Mozilla Public License, v. 2.0.
|
||||||
|
*
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sikofitt\Tests\Console\Getch;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Olive\Console\Getch;
|
use Sikofitt\Console\Getch;
|
||||||
|
|
||||||
class GetchTest extends TestCase
|
class GetchTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -23,7 +33,9 @@ class GetchTest extends TestCase
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @preserveGlobalState disabled
|
* @preserveGlobalState disabled
|
||||||
|
*
|
||||||
* @backupStaticAttributes false
|
* @backupStaticAttributes false
|
||||||
|
*
|
||||||
* @backupGlobals false
|
* @backupGlobals false
|
||||||
*/
|
*/
|
||||||
public function testFailureOnInvalidLibrary()
|
public function testFailureOnInvalidLibrary()
|
||||||
|
|
|
@ -1,9 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Olive\Tests\Console\Ungetch;
|
/*
|
||||||
|
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the
|
||||||
|
* terms of the Mozilla Public License, v. 2.0.
|
||||||
|
*
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sikofitt\Tests\Console\Ungetch;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Olive\Console\Getch;
|
use Sikofitt\Console\Getch;
|
||||||
|
|
||||||
class UngetchTest extends TestCase
|
class UngetchTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"require-dev": {
|
||||||
|
"friendsofphp/php-cs-fixer": "^3.59"
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue