From c3bdc6f67bc0a5858e10b5a7266aa7e1cc8e60f8 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 2 Dec 2019 16:27:22 -0500 Subject: [PATCH] Clean functions, fix sizing on mac --- src/functions.php | 49 +++++------------------------------------------ 1 file changed, 5 insertions(+), 44 deletions(-) diff --git a/src/functions.php b/src/functions.php index f4661de..c012a62 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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 *