Don't refresh until there's a keypress

This commit is contained in:
Timothy Warren 2019-10-16 22:14:30 -04:00
parent 77473f9602
commit 340dc459f5
2 changed files with 18 additions and 5 deletions

13
kilo
View File

@ -13,7 +13,6 @@ function main(int $argc, array $argv): int
enableRawMode(); enableRawMode();
// ob_start();
$editor = Editor::new($ffi); $editor = Editor::new($ffi);
if ($argc >= 2) if ($argc >= 2)
@ -27,8 +26,16 @@ function main(int $argc, array $argv): int
while (true) while (true)
{ {
$editor->refreshScreen(); $editor->refreshScreen();
$editor->processKeypress(); $char = $editor->processKeypress();
// ob_flush(); while ($char === '')
{
$char = $editor->processKeypress();
}
if ($char === NULL)
{
break;
}
} }
return 0; return 0;

View File

@ -534,9 +534,15 @@ class Editor {
} }
} }
public function processKeypress(): string public function processKeypress(): ?string
{ {
$c = $this->readKey(); $c = $this->readKey();
if ($c === "\0")
{
return '';
}
switch ($c) switch ($c)
{ {
case Key::HOME_KEY: case Key::HOME_KEY:
@ -565,7 +571,7 @@ class Editor {
case chr(ctrl_key('q')): case chr(ctrl_key('q')):
write_stdout("\x1b[2J"); // Clear the screen write_stdout("\x1b[2J"); // Clear the screen
write_stdout("\x1b[H"); // Reposition cursor to top-left write_stdout("\x1b[H"); // Reposition cursor to top-left
exit(0); return NULL;
break; break;
} }