Clean functions, fix sizing on mac
Gitea - Tutorials/php-kilo/master There was a failure building this commit Details

This commit is contained in:
Timothy Warren 2019-12-02 16:27:22 -05:00
parent f5aafb465a
commit c3bdc6f67b
1 changed files with 5 additions and 44 deletions

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
*