More cleanup
timw4mail/php-kilo/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2021-03-04 16:41:12 -05:00
parent b642ff5c59
commit 825966ac54
5 changed files with 33 additions and 16 deletions

View File

@ -5,4 +5,10 @@ parameters:
level: 8 level: 8
paths: paths:
- src - src
- ./kilo - ./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

View File

@ -451,7 +451,7 @@ class Editor {
if ($match !== FALSE) if ($match !== FALSE)
{ {
$lastMatch = $current; $lastMatch = $current;
$this->cursorY = $current; $this->cursorY = (int)$current;
$this->cursorX = $this->rowRxToCx($row, $match); $this->cursorX = $this->rowRxToCx($row, $match);
$this->rowOffset = $this->numRows; $this->rowOffset = $this->numRows;
@ -692,7 +692,7 @@ class Editor {
$this->outputBuffer .= ANSI::SHOW_CURSOR; $this->outputBuffer .= ANSI::SHOW_CURSOR;
echo $this->outputBuffer; write_stdout($this->outputBuffer, strlen($this->outputBuffer));
} }
public function setStatusMessage(string $fmt, mixed ...$args): void public function setStatusMessage(string $fmt, mixed ...$args): void

View File

@ -59,10 +59,8 @@ class Termios {
register_shutdown_function([static::class, 'disableRawMode']); register_shutdown_function([static::class, 'disableRawMode']);
$termios = clone $instance->originalTermios; $termios = clone $instance->originalTermios;
// $termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON); $termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON);
// $termios->c_oflag &= ~(C::OPOST); $termios->c_oflag &= ~(C::OPOST);
$termios->c_iflag = 0;
$termios->c_oflag = 0;
$termios->c_cflag |= (C::CS8); $termios->c_cflag |= (C::CS8);
$termios->c_lflag &= ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG ); $termios->c_lflag &= ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG );
$termios->c_cc[C::VMIN] = 0; $termios->c_cc[C::VMIN] = 0;

View File

@ -21,6 +21,7 @@ class PHP8 extends PhpToken {
*/ */
public static function getTokens(string $code): array public static function getTokens(string $code): array
{ {
// Make the lines/tokens 1-indexed
$lines = explode("\n", $code); $lines = explode("\n", $code);
array_unshift($lines, ''); array_unshift($lines, '');
unset($lines[0]); unset($lines[0]);
@ -81,7 +82,6 @@ class PHP8 extends PhpToken {
if (str_starts_with($char, "\n") && trim($char) === '') if (str_starts_with($char, "\n") && trim($char) === '')
{ {
$this->tokens[$currentLine][] = $current; $this->tokens[$currentLine][] = $current;
// $this->lineNum++;
return; return;
} }

View File

@ -38,10 +38,13 @@ function get_window_size(): array
// First, try to get the answer from ioctl // First, try to get the answer from ioctl
$ffi = get_ffi(); $ffi = get_ffi();
$ws = $ffi->new('struct winsize'); $ws = $ffi->new('struct winsize');
$res = $ffi->ioctl(C::STDOUT_FILENO, C::TIOCGWINSZ, FFI::addr($ws)); if ($ws !== NULL)
if ($res === 0 && $ws->ws_col !== 0 && $ws->ws_row !== 0)
{ {
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 // Try using tput
@ -138,8 +141,8 @@ function is_space(string $char): bool
KeyCode::NEWLINE, KeyCode::NEWLINE,
KeyCode::SPACE, KeyCode::SPACE,
KeyCode::TAB, KeyCode::TAB,
KeyCode::VERTICAL_TAB KeyCode::VERTICAL_TAB => true,
=> true,
default => false, default => false,
}; };
} }
@ -193,10 +196,15 @@ function is_separator(string $char): bool
function read_stdin(int $len = 128): string function read_stdin(int $len = 128): string
{ {
$handle = fopen('php://stdin', 'rb'); $handle = fopen('php://stdin', 'rb');
if ($handle === false)
{
return '';
}
$input = fread($handle, $len); $input = fread($handle, $len);
fclose($handle); fclose($handle);
return $input; return (is_string($input)) ? $input : '';
} }
/** /**
@ -205,11 +213,16 @@ function read_stdin(int $len = 128): string
* @codeCoverageIgnore * @codeCoverageIgnore
* @param string $str * @param string $str
* @param int|NULL $len * @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'); $handle = fopen('php://stdout', 'ab');
if ($handle === false)
{
return false;
}
$res = (is_int($len)) $res = (is_int($len))
? fwrite($handle, $str, $len) ? fwrite($handle, $str, $len)
: fwrite($handle, $str); : fwrite($handle, $str);