diff --git a/kilo b/kilo index 1fa1c2b..e271952 100755 --- a/kilo +++ b/kilo @@ -20,18 +20,20 @@ function main(int $argc, array $argv): int $editor->open($argv[1]); } - $editor->setStatusMessage("HELP: Ctrl-Q = quit"); + $editor->setStatusMessage('HELP: Ctrl-Q = quit'); // Input Loop while (true) { $editor->refreshScreen(); + + // Spin while waiting for a keypress $char = $editor->processKeypress(); while ($char === '') { $char = $editor->processKeypress(); } - + if ($char === NULL) { break; diff --git a/src/Editor.php b/src/Editor.php index 38c538c..9bb66ee 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -22,10 +22,10 @@ trait MagicProperties { } class Key { - public const ARROW_LEFT = 'ARROW_LEFT'; - public const ARROW_RIGHT = 'ARROW_RIGHT'; - public const ARROW_UP = 'ARROW_UP'; - public const ARROW_DOWN = 'ARROW_DOWN'; + public const ARROW_LEFT = 'a';//'ARROW_LEFT'; + public const ARROW_RIGHT = 'd';//ARROW_RIGHT'; + public const ARROW_UP = 'w';//'ARROW_UP'; + public const ARROW_DOWN = 's';//'ARROW_DOWN'; public const DEL_KEY = 'DEL'; public const HOME_KEY = 'HOME'; public const END_KEY = 'END'; @@ -327,7 +327,7 @@ class Editor { { $this->rowOffset = $this->cursorY; } - if ($this->cursorY >= $this->rowOffset + $this->screenRows) + if ($this->cursorY >= ($this->rowOffset + $this->screenRows)) { $this->rowOffset = $this->cursorY - $this->screenRows + 1; } @@ -337,7 +337,7 @@ class Editor { { $this->colOffset = $this->renderX; } - if ($this->renderX >= $this->colOffset + $this->screenCols) + if ($this->renderX >= ($this->colOffset + $this->screenCols)) { $this->colOffset = $this->renderX - $this->screenCols + 1; } @@ -348,7 +348,7 @@ class Editor { for ($y = 0; $y < $this->screenRows; $y++) { $filerow = $y + $this->rowOffset; - if ($filerow >= count($this->rows)) + if ($filerow >= $this->numRows) { if ($this->numRows === 0 && $y === $this->screenRows / 3) { @@ -365,7 +365,7 @@ class Editor { $this->ab .= '~'; $padding--; } - while ($padding--) + for (; $padding >= 0; $padding--) { $this->ab .= ' '; } @@ -402,8 +402,8 @@ class Editor { $this->ab .= "\x1b[7m"; $statusFilename = $this->filename !== '' ? $this->filename : '[No Name]'; - $status = sprintf("%.20s - %d lines", $statusFilename, count($this->rows)); - $rstatus = sprintf("%d/%d", $this->cursorY + 1, count($this->rows)); + $status = sprintf("%.20s - %d lines", $statusFilename, $this->numRows); + $rstatus = sprintf("%d/%d", $this->cursorY + 1, $this->numRows); $len = strlen($status); $rlen = strlen($rstatus); if ($len > $this->screenCols) @@ -573,6 +573,9 @@ class Editor { write_stdout("\x1b[H"); // Reposition cursor to top-left return NULL; break; + + default: + return $c; } return $c; @@ -595,7 +598,7 @@ class Editor { } $times = $this->screenRows; - while ($times--) + for (; $times > 0; $times--) { $this->moveCursor($c === Key::PAGE_UP ? Key::ARROW_UP : Key::ARROW_DOWN); } diff --git a/src/constants.php b/src/constants.php index e345ff6..85bbff1 100644 --- a/src/constants.php +++ b/src/constants.php @@ -18,7 +18,7 @@ define('STDERR_FILENO', 2); define('TCSAFLUSH', 2); // ----------------------------------------------------------------------------- -// ! Termios bitflags +// ! Termios flags and constants // ----------------------------------------------------------------------------- /* Input modes */ diff --git a/src/functions.php b/src/functions.php index ebb2896..25b25d0 100644 --- a/src/functions.php +++ b/src/functions.php @@ -44,6 +44,8 @@ function disableRawMode(): void $res = $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($original_termios)); + echo "\n"; + if ($res === -1) { die('tcsetattr');