From 8c159dd5bcfb985090f76573e38f659fa265b2af Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 5 Dec 2019 11:00:03 -0500 Subject: [PATCH] Attempt to verify tput exists before using it --- src/functions.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/functions.php b/src/functions.php index c012a62..771c7a7 100644 --- a/src/functions.php +++ b/src/functions.php @@ -10,6 +10,17 @@ use Aviat\Kilo\Enum\{ Highlight, }; +/** + * See if tput exists for fallback terminal size detection + * + * @return bool + * @codeCoverageIgnore + */ +function has_tput(): bool +{ + return shell_exec('type tput') === 0; +} + // ---------------------------------------------------------------------------- // ! Terminal size // ---------------------------------------------------------------------------- @@ -29,10 +40,16 @@ function get_window_size(): array if ($res === -1 || $ws->ws_col === 0) { - $rows = trim(shell_exec('tput lines')); - $cols = trim(shell_exec('tput cols')); + if (has_tput()) + { + $rows = trim(shell_exec('tput lines')); + $cols = trim(shell_exec('tput cols')); - return [(int)$rows, (int)$cols]; + return [(int)$rows, (int)$cols]; + } + + // Worst-case, return an arbitrary 'standard' size + return [80, 25]; } return [$ws->ws_row, $ws->ws_col]; @@ -110,7 +127,7 @@ function is_digit(string $char): bool */ function is_space(string $char): bool { - $ws = [' ', "\t", "\n", "\r", "\xa", "\xb", "\xc"]; + $ws = [' ', "\t", "\n", "\r", "\v", "\f"]; return is_ascii($char) && in_array($char, $ws, TRUE); }