Up to step 21

This commit is contained in:
Timothy Warren 2019-10-11 16:32:47 -04:00
parent 10369accac
commit e0a9d066ca
3 changed files with 38 additions and 25 deletions

29
kilo
View File

@ -5,41 +5,20 @@ namespace Kilo;
require_once __DIR__ . '/src/constants.php';
require_once __DIR__ . '/src/functions.php';
require_once __DIR__ . '/src/editor.php';
function main(): int
{
global $ffi;
enableRawMode();
$editor = new Editor();
// Input Loop
while (true)
{
$char = read_stdin(1);
$c = ord($char);
if (empty($char))
{
continue;
}
if ($ffi->iscntrl($c))
{
printf("%d\r\n", $c);
}
else
{
printf("%d ('%c')\r\n", $c, $c);
}
if ($char === 'q')
{
break;
}
$editor->processKeypress();
}
disableRawMode();
return 0;
}

27
src/editor.php Normal file
View File

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace Kilo;
class Editor {
public function __construct()
{
}
public function processKeypress(): void
{
$c = $this->readKey();
switch($c)
{
case chr(ctrl_key('q')):
exit(0);
break;
}
}
protected function readKey(): string
{
return read_stdin(1);
}
}

View File

@ -12,6 +12,8 @@ function enableRawMode(): void
global $ffi;
global $original_termios;
register_shutdown_function('Kilo\\disableRawMode');
// Populate the original terminal settings
$ffi->tcgetattr(STDIN_FILENO, FFI::addr($original_termios));
@ -44,4 +46,9 @@ function read_stdin(int $len = 128): string
fclose($handle);
return $input;
}
function ctrl_key(string $char): int
{
return ord($char) & 0x1f;
}