Improve terminal size checking
Some checks failed
timw4mail/php-kilo/master There was a failure building this commit

This commit is contained in:
Timothy Warren 2020-01-23 13:41:35 -05:00
parent 83c5c51b58
commit 24baca3cb8
2 changed files with 19 additions and 16 deletions

View File

@ -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 = [];

View File

@ -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];
}
// ----------------------------------------------------------------------------