Merge remote-tracking branch 'origin/master'
Some checks failed
Gitea - Tutorials/php-kilo/master There was a failure building this commit

This commit is contained in:
Timothy Warren 2019-12-04 10:46:54 -05:00
commit 7acf38da9c

View File

@ -14,37 +14,13 @@ use Aviat\Kilo\Enum\{
// ! Terminal size
// ----------------------------------------------------------------------------
/**
* @TODO fix
* @codeCoverageIgnore
*/
function get_cursor_position()
{
write_stdout("\x1b[999C\x1b[999B");
write_stdout("\x1b[6n");
$rows = 0;
$cols = 0;
$buffer = read_stdout();
$res = sscanf($buffer, '\x1b[%d;%dR', $rows, $cols);
if ($res === -1 || $buffer[0] !== '\x1b' || $buffer[1] !== '[')
{
die('Failed to get screen size');
}
return [$rows, $cols];
}
/**
* Get the size of the current terminal window
*
* @codeCoverageIgnore
* @return array
*/
function get_window_size()
function get_window_size(): array
{
$ffi = get_ffi();
@ -53,9 +29,10 @@ function get_window_size()
if ($res === -1 || $ws->ws_col === 0)
{
// Return a default screen size until get_cursor_position function works
return [25, 80];
// return get_cursor_position();
$rows = trim(shell_exec('tput lines'));
$cols = trim(shell_exec('tput cols'));
return [(int)$rows, (int)$cols];
}
return [$ws->ws_row, $ws->ws_col];
@ -214,22 +191,6 @@ function write_stdout(string $str, int $len = NULL): int
return $res;
}
/**
* @codeCoverageIgnore
* @param int $len
* @return string
*/
function read_stdout(int $len = 128): string
{
$handle = fopen('php://stdout', 'rb');
$input = fread($handle, $len);
$input = rtrim($input);
fclose($handle);
return $input;
}
/**
* Replaces a slice of an array with the same value
*