Make some code more concise
timw4mail/php-kilo/pipeline/head There was a failure building this commit Details

This commit is contained in:
Timothy Warren 2023-10-12 16:02:09 -04:00
parent 4a5f074f28
commit 7554c69d63
1 changed files with 13 additions and 19 deletions

View File

@ -59,8 +59,10 @@ class Termios {
$termios = clone $instance->originalTermios; $termios = clone $instance->originalTermios;
// Turn on raw mode // Use the stdlib func to set raw mode parameters
self::ffi()->cfmakeraw(FFI::addr($termios)); self::ffi()->cfmakeraw(FFI::addr($termios));
// Apply raw mode to the terminal
$res = self::ffi() $res = self::ffi()
->tcsetattr(C::STDIN_FILENO, C::TCSANOW, FFI::addr($termios)); ->tcsetattr(C::STDIN_FILENO, C::TCSANOW, FFI::addr($termios));
@ -102,14 +104,9 @@ class Termios {
$ws = $ffi->new('struct winsize'); $ws = $ffi->new('struct winsize');
if ($ws !== NULL) if ($ws !== NULL)
{ {
if (self::getLibType() === LibType::MUSL) $res = (self::getLibType() === LibType::MUSL)
{ ? $ffi->tcgetwinsize(C::STDOUT_FILENO, FFI::addr($ws))
$res = $ffi->tcgetwinsize(C::STDOUT_FILENO, FFI::addr($ws)); : $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws));
}
else
{
$res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws));
}
if ($res === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0) if ($res === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0)
{ {
@ -126,9 +123,10 @@ class Termios {
if ($type === NULL) if ($type === NULL)
{ {
if (file_exists("/usr/lib/libc.so")) $maybeLibc = "/usr/lib/libc.so";
if (file_exists($maybeLibc))
{ {
$rawLibInfo = (string)shell_exec("/usr/lib/libc.so"); $rawLibInfo = (string)shell_exec($maybeLibc);
if (str_contains(strtolower($rawLibInfo), "musl")) if (str_contains(strtolower($rawLibInfo), "musl"))
{ {
$type = LibType::MUSL; $type = LibType::MUSL;
@ -145,10 +143,7 @@ class Termios {
{ {
static $instance; static $instance;
if ($instance === NULL) $instance ??= new self();
{
$instance = new self();
}
return $instance; return $instance;
} }
@ -167,11 +162,10 @@ class Termios {
if (self::getLibType() === LibType::MUSL) if (self::getLibType() === LibType::MUSL)
{ {
$ffi = FFI::load(__DIR__ . '/ffi_musl.h'); $ffi = FFI::load(__DIR__ . '/ffi_musl.h');
return $ffi;
} }
else
{ $ffi = FFI::load(__DIR__ . '/ffi.h');
$ffi = FFI::load(__DIR__ . '/ffi.h');
}
} }
return $ffi; return $ffi;