From 825966ac54b3c2c2c4119f37c1f751ebd039216d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 4 Mar 2021 16:41:12 -0500 Subject: [PATCH] More cleanup --- phpstan.neon | 8 +++++++- src/Editor.php | 4 ++-- src/Termios.php | 6 ++---- src/Tokens/PHP8.php | 2 +- src/functions.php | 29 +++++++++++++++++++++-------- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 6633eab..29b5b53 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,4 +5,10 @@ parameters: level: 8 paths: - src - - ./kilo \ No newline at end of file + - ./kilo + ignoreErrors: + - '#Call to an undefined method FFI::[a-zA-Z0-9_]+\(\)#' + - '#Access to an undefined property FFI\\CData#' + - '#Match arm comparison between \*NEVER\*#' + universalObjectCratesClasses: + - FFI \ No newline at end of file diff --git a/src/Editor.php b/src/Editor.php index 6d08cb9..b06740b 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -451,7 +451,7 @@ class Editor { if ($match !== FALSE) { $lastMatch = $current; - $this->cursorY = $current; + $this->cursorY = (int)$current; $this->cursorX = $this->rowRxToCx($row, $match); $this->rowOffset = $this->numRows; @@ -692,7 +692,7 @@ class Editor { $this->outputBuffer .= ANSI::SHOW_CURSOR; - echo $this->outputBuffer; + write_stdout($this->outputBuffer, strlen($this->outputBuffer)); } public function setStatusMessage(string $fmt, mixed ...$args): void diff --git a/src/Termios.php b/src/Termios.php index a1a95d4..76159f8 100644 --- a/src/Termios.php +++ b/src/Termios.php @@ -59,10 +59,8 @@ class Termios { register_shutdown_function([static::class, 'disableRawMode']); $termios = clone $instance->originalTermios; - // $termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON); - // $termios->c_oflag &= ~(C::OPOST); - $termios->c_iflag = 0; - $termios->c_oflag = 0; + $termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON); + $termios->c_oflag &= ~(C::OPOST); $termios->c_cflag |= (C::CS8); $termios->c_lflag &= ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG ); $termios->c_cc[C::VMIN] = 0; diff --git a/src/Tokens/PHP8.php b/src/Tokens/PHP8.php index 04705ad..6d3bf7f 100644 --- a/src/Tokens/PHP8.php +++ b/src/Tokens/PHP8.php @@ -21,6 +21,7 @@ class PHP8 extends PhpToken { */ public static function getTokens(string $code): array { + // Make the lines/tokens 1-indexed $lines = explode("\n", $code); array_unshift($lines, ''); unset($lines[0]); @@ -81,7 +82,6 @@ class PHP8 extends PhpToken { if (str_starts_with($char, "\n") && trim($char) === '') { $this->tokens[$currentLine][] = $current; - // $this->lineNum++; return; } diff --git a/src/functions.php b/src/functions.php index 6536f88..bb15ce3 100644 --- a/src/functions.php +++ b/src/functions.php @@ -38,10 +38,13 @@ 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 === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0) + if ($ws !== NULL) { - return [$ws->ws_row, $ws->ws_col]; + $res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws)); + if ($res === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0) + { + return [$ws->ws_row, $ws->ws_col]; + } } // Try using tput @@ -138,8 +141,8 @@ function is_space(string $char): bool KeyCode::NEWLINE, KeyCode::SPACE, KeyCode::TAB, - KeyCode::VERTICAL_TAB - => true, + KeyCode::VERTICAL_TAB => true, + default => false, }; } @@ -193,10 +196,15 @@ function is_separator(string $char): bool function read_stdin(int $len = 128): string { $handle = fopen('php://stdin', 'rb'); + if ($handle === false) + { + return ''; + } + $input = fread($handle, $len); fclose($handle); - return $input; + return (is_string($input)) ? $input : ''; } /** @@ -205,11 +213,16 @@ function read_stdin(int $len = 128): string * @codeCoverageIgnore * @param string $str * @param int|NULL $len - * @return int + * @return int|false */ -function write_stdout(string $str, int $len = NULL): int +function write_stdout(string $str, int $len = NULL): int|false { $handle = fopen('php://stdout', 'ab'); + if ($handle === false) + { + return false; + } + $res = (is_int($len)) ? fwrite($handle, $str, $len) : fwrite($handle, $str);