Compare commits

..

3 Commits
2.x ... 1.5.0

Author SHA1 Message Date
R. Eric Wheeler 1b01ff78f2 Fix readme 2022-05-17 10:04:07 -07:00
R. Eric Wheeler 5b92106e46 Update readme 2022-05-17 09:53:07 -07:00
R. Eric Wheeler d12cd38456 Move to new namespace 2022-05-17 09:47:34 -07:00
15 changed files with 2697 additions and 2801 deletions

1
.gitignore vendored
View File

@ -3,4 +3,3 @@
.php_cs.cache
vendor/
.phpunit.cache/
tools/php-cs-fixer/vendor/

View File

@ -1,18 +0,0 @@
<?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 Normal file
View File

@ -0,0 +1,14 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('var')
;
return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder)
;

View File

@ -2,15 +2,16 @@
This simply uses the FFI extension to enable _getch and _ungetch in Windows and linux.
[![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://repos.bgemi.net/sikofitt/getch/badges/1.x/coverage.svg)](https://repos.bgemi.net/sikofitt/getch/-/commits/1.x)
[![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)
```shell script
$ composer require sikofitt/getch:dev-master
$ composer require olivebbs/getch
```
```php
use Sikofitt\Console\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.
@ -45,7 +46,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();
```php
use function Sikofitt\Console\getch;
use function Olive\Console\getch;
$ord = getch($linuxLibrary = null);
print \chr($ord);

View File

@ -3,15 +3,16 @@
"description": "Implements _getch and _ungetch for windows and linux using ffi",
"type": "library",
"require": {
"php": "^8.2",
"php": ">=7.4",
"ext-ffi": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.6"
"friendsofphp/php-cs-fixer": "^2.18",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"Sikofitt\\Console\\": "src/Console/"
"Olive\\Console\\": "src/Console/"
},
"files": [
"functions.php"
@ -19,7 +20,7 @@
},
"autoload-dev": {
"psr-4": {
"Sikofitt\\Tests\\Console\\": "tests/"
"Olive\\Tests\\Console\\": "tests/"
}
},
"license": "MPL-2.0",

2811
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
declare(strict_types=1);
/*
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
*
* This Source Code Form is subject to the
* terms of the Mozilla Public License, v. 2.0.
@ -12,18 +12,22 @@ declare(strict_types=1);
* You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use Sikofitt\Console\Getch;
use Olive\Console\Getch;
if (!function_exists('getch')) {
function getch(?string $linuxLibrary = null): int
function getch(string $linuxLibrary = null): int
{
return (new Getch($linuxLibrary))->getch();
$g = new Getch($linuxLibrary);
return $g->getch();
}
}
if (!function_exists('ungetch')) {
function ungetch($char, ?string $linuxLibrary = null): int
function ungetch($char, string $linuxLibrary = null): int
{
return (new Getch($linuxLibrary))->ungetch($char);
$g = new Getch($linuxLibrary);
return $g->ungetch($char);
}
}

View File

@ -1,4 +1,4 @@
Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
This Source Code Form is subject to the
terms of the Mozilla Public License, v. 2.0.

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"

View File

@ -3,7 +3,7 @@
declare(strict_types=1);
/*
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
*
* This Source Code Form is subject to the
* terms of the Mozilla Public License, v. 2.0.
@ -12,7 +12,10 @@ declare(strict_types=1);
* You can obtain one at https://mozilla.org/MPL/2.0/.
*/
namespace Sikofitt\Console;
namespace Olive\Console;
use FFI;
use RuntimeException;
final class Getch
{
@ -22,7 +25,7 @@ final class Getch
public const KEY_E0 = 0;
public const KEY_E1 = 224;
// Supported scan codes.
// Supported scan scodes.
public const KEY_F1 = 59;
public const KEY_F2 = 60;
public const KEY_F3 = 61;
@ -53,14 +56,14 @@ final class Getch
int _ungetch(int c);
DECLARATIONS;
private static ?\FFI $ffi = null;
private static ?FFI $ffi = null;
public static function resetFFI(): void
{
self::$ffi = null;
static::$ffi = null;
}
public function __construct(?string $linuxLibrary = null)
public function __construct(string $linuxLibrary = null)
{
if (null === $linuxLibrary) {
$linuxLibrary = self::LINUX_LIBRARY;
@ -70,15 +73,15 @@ final class Getch
$osFamily = PHP_OS_FAMILY;
if ('Windows' === $osFamily) {
$declarations = self::DECLARATIONS.' int _kbhit();';
self::$ffi = \FFI::cdef($declarations, self::WINDOWS_LIBRARY);
self::$ffi = FFI::cdef($declarations, self::WINDOWS_LIBRARY);
} elseif ('Linux' === $osFamily) {
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();';
self::$ffi = \FFI::cdef($declarations, $linuxLibrary);
self::$ffi = FFI::cdef($declarations, $linuxLibrary);
} else {
throw new \RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily));
}
}
}
@ -86,9 +89,9 @@ final class Getch
public function peek(): int
{
if (PHP_OS_FAMILY === 'Windows') {
if (self::$ffi->_kbhit()) {
$result = self::$ffi->_getch();
self::$ffi->_ungetch($result);
if ($ffi->_kbhit()) {
$result = $ffi->_getch();
$ffi->_ungetch($result);
return $result;
}
@ -96,7 +99,7 @@ final class Getch
return -1;
}
return self::$ffi->cinPeek();
return $ffi->cinPeek();
}
public function getch(): int
@ -104,8 +107,12 @@ final class Getch
return self::$ffi->_getch();
}
public function ungetch(string|int $char): int
public function ungetch($char): int
{
if (!is_string($char) && !is_int($char)) {
throw new \TypeError('ungetch takes a parameter of int or string.');
}
if (is_string($char)) {
$char = ord($char[0]);
}

0
src/Console/Resources/libgetch.so Normal file → Executable file
View File

View File

@ -1,19 +1,9 @@
<?php
/*
* 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;
namespace Olive\Tests\Console\Getch;
use PHPUnit\Framework\TestCase;
use Sikofitt\Console\Getch;
use Olive\Console\Getch;
class GetchTest extends TestCase
{
@ -33,9 +23,7 @@ class GetchTest extends TestCase
/**
* @preserveGlobalState disabled
*
* @backupStaticAttributes false
*
* @backupGlobals false
*/
public function testFailureOnInvalidLibrary()

View File

@ -1,19 +1,9 @@
<?php
/*
* 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;
namespace Olive\Tests\Console\Ungetch;
use PHPUnit\Framework\TestCase;
use Sikofitt\Console\Getch;
use Olive\Console\Getch;
class UngetchTest extends TestCase
{

View File

@ -1,5 +0,0 @@
{
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.59"
}
}

File diff suppressed because it is too large Load Diff