From c909fa19f1e1fe842652deec9eb6865d8e7ccd11 Mon Sep 17 00:00:00 2001 From: "R. Eric Wheeler" Date: Thu, 7 Jan 2021 13:56:08 -0800 Subject: [PATCH] added _ungetch --- README.md | 6 +++--- src/Console/Getch.php | 13 +++++++++---- src/Console/Resources/Makefile | 4 ++-- src/Console/Resources/{getwch.c => getch.c} | 7 ++++++- .../Resources/{libgetwch.so => libgetch.so} | Bin 15952 -> 16080 bytes 5 files changed, 20 insertions(+), 10 deletions(-) rename src/Console/Resources/{getwch.c => getch.c} (84%) rename src/Console/Resources/{libgetwch.so => libgetch.so} (59%) diff --git a/README.md b/README.md index 0b1e51e..882750b 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ $ composer require sikofitt/getch:dev-master ```php use Sikofitt\Console\Getch; - $g = new Getch($linuxLibrary = null); // can also be a library that implements a function called _getwch; - // by default uses the bundled Resources/libgetwch.so - // on windows uses the built in _getwch function. + $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. $char = $g->getch(); print $char; ``` diff --git a/src/Console/Getch.php b/src/Console/Getch.php index ddb6e3b..a4b3b19 100644 --- a/src/Console/Getch.php +++ b/src/Console/Getch.php @@ -17,7 +17,7 @@ use RuntimeException; final class Getch { - private const LINUX_LIBRARY = __DIR__ . '/Resources/libgetwch.so'; + private const LINUX_LIBRARY = __DIR__ . '/Resources/libgetch.so'; private const WINDOWS_LIBRARY = 'ucrtbase.dll'; private static ?FFI $ffi = null; @@ -31,13 +31,13 @@ final class Getch if (self::$ffi === null) { $osFamily = PHP_OS_FAMILY; if ($osFamily === 'Windows') { - self::$ffi = FFI::cdef('char _getwch();', self::WINDOWS_LIBRARY); + self::$ffi = FFI::cdef('char _getch();', self::WINDOWS_LIBRARY); } elseif ($osFamily === 'Linux') { if (!file_exists($linuxLibrary)) { throw new RuntimeException(sprintf('Could not find library file %s.', $linuxLibrary)); } - self::$ffi = FFI::cdef('char _getwch();', $linuxLibrary); + self::$ffi = FFI::cdef('char _getch(); int _ungetch(int ch);', $linuxLibrary); } else { throw new RuntimeException(sprintf('Sorry, %s is not supported yet.', $osFamily)); } @@ -46,6 +46,11 @@ final class Getch public function getch(): string { - return self::$ffi->_getwch(); + return self::$ffi->_getch(); + } + + public function ungetch(string $char) + { + return self::$ffi->_ungetch($char); } } diff --git a/src/Console/Resources/Makefile b/src/Console/Resources/Makefile index 189e7d6..fa3a278 100644 --- a/src/Console/Resources/Makefile +++ b/src/Console/Resources/Makefile @@ -2,8 +2,8 @@ CC = gcc CFLAGS = -shared -Wall -fPIC all: - @${CC} ${CFLAGS} getwch.c -o libgetwch.so + @${CC} ${CFLAGS} getch.c -o libgetch.so clean: - @rm -f libgetwch.so + @rm -f libgetch.so diff --git a/src/Console/Resources/getwch.c b/src/Console/Resources/getch.c similarity index 84% rename from src/Console/Resources/getwch.c rename to src/Console/Resources/getch.c index c495867..5f2e769 100644 --- a/src/Console/Resources/getwch.c +++ b/src/Console/Resources/getch.c @@ -3,7 +3,7 @@ #include /* reads from keypress, doesn't echo */ -int _getwch(void) +int _getch(void) { struct termios oldattr, newattr; int ch; @@ -14,4 +14,9 @@ int _getwch(void) ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; +} + +int _ungetch(int ch) +{ + return ungetc(ch, stdin); } \ No newline at end of file diff --git a/src/Console/Resources/libgetwch.so b/src/Console/Resources/libgetch.so similarity index 59% rename from src/Console/Resources/libgetwch.so rename to src/Console/Resources/libgetch.so index dea1f53bed878fc52b801a40caded1be576e6d15..a6bc39f6e356edd20e2773fee8379dfe3564096c 100755 GIT binary patch delta 1989 zcmZ`)VN4uF6rS0;gehM1W2PZLNm784URnDx!>Ow*e9$jvwJ zd*96N&U^D%JH2*hE)<+oiEEYzm4o4H_I{h#%)zVj8?gSfQO^QW| zs1D+0eLtS98E`9%@r~>CPTH(pvF=Tm8If4(q z#i-vS+_A~>92PBu-!RH%!qS#EV_c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