From 7554c69d6368c9f916f85825fa73ba38b8de4d45 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 12 Oct 2023 16:02:09 -0400 Subject: [PATCH] Make some code more concise --- src/Terminal/Termios.php | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/Terminal/Termios.php b/src/Terminal/Termios.php index 572e318..18739ec 100644 --- a/src/Terminal/Termios.php +++ b/src/Terminal/Termios.php @@ -59,8 +59,10 @@ class Termios { $termios = clone $instance->originalTermios; - // Turn on raw mode + // Use the stdlib func to set raw mode parameters self::ffi()->cfmakeraw(FFI::addr($termios)); + + // Apply raw mode to the terminal $res = self::ffi() ->tcsetattr(C::STDIN_FILENO, C::TCSANOW, FFI::addr($termios)); @@ -102,14 +104,9 @@ class Termios { $ws = $ffi->new('struct winsize'); if ($ws !== NULL) { - if (self::getLibType() === LibType::MUSL) - { - $res = $ffi->tcgetwinsize(C::STDOUT_FILENO, FFI::addr($ws)); - } - else - { - $res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws)); - } + $res = (self::getLibType() === LibType::MUSL) + ? $ffi->tcgetwinsize(C::STDOUT_FILENO, FFI::addr($ws)) + : $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws)); if ($res === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0) { @@ -126,9 +123,10 @@ class Termios { 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")) { $type = LibType::MUSL; @@ -145,10 +143,7 @@ class Termios { { static $instance; - if ($instance === NULL) - { - $instance = new self(); - } + $instance ??= new self(); return $instance; } @@ -167,11 +162,10 @@ class Termios { if (self::getLibType() === LibType::MUSL) { $ffi = FFI::load(__DIR__ . '/ffi_musl.h'); + return $ffi; } - else - { - $ffi = FFI::load(__DIR__ . '/ffi.h'); - } + + $ffi = FFI::load(__DIR__ . '/ffi.h'); } return $ffi;