diff --git a/src/Editor.php b/src/Editor.php index 576bc3f..065140b 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -36,7 +36,7 @@ class Editor { protected string $statusMsg = ''; protected int $statusMsgTime; - public ?Syntax $syntax; + public ?Syntax $syntax = NULL; // Tokens for highlighting PHP public array $tokens = []; diff --git a/src/functions.php b/src/functions.php index 771c7a7..794094e 100644 --- a/src/functions.php +++ b/src/functions.php @@ -18,7 +18,7 @@ use Aviat\Kilo\Enum\{ */ function has_tput(): bool { - return shell_exec('type tput') === 0; + return (int)shell_exec('type tput') === 0; } // ---------------------------------------------------------------------------- @@ -33,26 +33,29 @@ function has_tput(): bool */ function get_window_size(): array { + // First, try to get the answer from ioctl $ffi = get_ffi(); - $ws = $ffi->new('struct winsize'); $res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws)); - - if ($res === -1 || $ws->ws_col === 0) + if ($res === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0) { - if (has_tput()) - { - $rows = trim(shell_exec('tput lines')); - $cols = trim(shell_exec('tput cols')); - - return [(int)$rows, (int)$cols]; - } - - // Worst-case, return an arbitrary 'standard' size - return [80, 25]; + return [$ws->ws_row, $ws->ws_col]; } - return [$ws->ws_row, $ws->ws_col]; + // Try using tput + if (has_tput()) + { + $rows = (int)trim(shell_exec('tput lines')); + $cols = (int)trim(shell_exec('tput cols')); + + if ($rows > 0 && $cols > 0) + { + return [$rows, $cols]; + } + } + + // Worst-case, return an arbitrary 'standard' size + return [25, 80]; } // ----------------------------------------------------------------------------