Attempt to verify tput exists before using it
All checks were successful
Gitea - Tutorials/php-kilo/master This commit looks good

This commit is contained in:
Timothy Warren 2019-12-05 11:00:03 -05:00
parent 5ffa267e15
commit 8c159dd5bc

View File

@ -10,6 +10,17 @@ use Aviat\Kilo\Enum\{
Highlight, 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 // ! Terminal size
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -28,6 +39,8 @@ function get_window_size(): array
$res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws)); $res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws));
if ($res === -1 || $ws->ws_col === 0) if ($res === -1 || $ws->ws_col === 0)
{
if (has_tput())
{ {
$rows = trim(shell_exec('tput lines')); $rows = trim(shell_exec('tput lines'));
$cols = trim(shell_exec('tput cols')); $cols = trim(shell_exec('tput cols'));
@ -35,6 +48,10 @@ function get_window_size(): array
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]; return [$ws->ws_row, $ws->ws_col];
} }
@ -110,7 +127,7 @@ function is_digit(string $char): bool
*/ */
function is_space(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); return is_ascii($char) && in_array($char, $ws, TRUE);
} }