More consistent names for keys

This commit is contained in:
Timothy Warren 2021-04-09 13:52:01 -04:00
parent 35ec8f27ad
commit 644f27bb37
8 changed files with 67 additions and 61 deletions

View File

@ -2,7 +2,7 @@
namespace Aviat\Kilo; namespace Aviat\Kilo;
use Aviat\Kilo\Enum\KeyCode; use Aviat\Kilo\Enum\RawKeyCode;
use Aviat\Kilo\Enum\KeyType; use Aviat\Kilo\Enum\KeyType;
use Aviat\Kilo\Tokens\PHP8; use Aviat\Kilo\Tokens\PHP8;
use Aviat\Kilo\Type\Point; use Aviat\Kilo\Type\Point;
@ -102,7 +102,7 @@ class Document {
return; return;
} }
if ($c === KeyType::ENTER || $c === KeyCode::CARRIAGE_RETURN) if ($c === KeyType::ENTER || $c === RawKeyCode::CARRIAGE_RETURN)
{ {
$this->insertNewline($at); $this->insertNewline($at);
return; return;

View File

@ -5,7 +5,7 @@ namespace Aviat\Kilo;
use Aviat\Kilo\Type\TerminalSize; use Aviat\Kilo\Type\TerminalSize;
use Aviat\Kilo\Enum\{ use Aviat\Kilo\Enum\{
Color, Color,
KeyCode, RawKeyCode,
KeyType, KeyType,
Highlight, Highlight,
SearchDirection SearchDirection
@ -163,7 +163,7 @@ class Editor {
$rx = 0; $rx = 0;
for ($i = 0; $i < $cx; $i++) for ($i = 0; $i < $cx; $i++)
{ {
if ($row->chars[$i] === KeyCode::TAB) if ($row->chars[$i] === RawKeyCode::TAB)
{ {
$rx += (KILO_TAB_STOP - 1) - ($rx % KILO_TAB_STOP); $rx += (KILO_TAB_STOP - 1) - ($rx % KILO_TAB_STOP);
} }
@ -185,7 +185,7 @@ class Editor {
$cur_rx = 0; $cur_rx = 0;
for ($cx = 0; $cx < $row->size; $cx++) for ($cx = 0; $cx < $row->size; $cx++)
{ {
if ($row->chars[$cx] === KeyCode::TAB) if ($row->chars[$cx] === RawKeyCode::TAB)
{ {
$cur_rx += (KILO_TAB_STOP - 1) - ($cur_rx % KILO_TAB_STOP); $cur_rx += (KILO_TAB_STOP - 1) - ($cur_rx % KILO_TAB_STOP);
} }
@ -266,7 +266,7 @@ class Editor {
$lastMatch = NO_MATCH; $lastMatch = NO_MATCH;
$direction = SearchDirection::FORWARD; $direction = SearchDirection::FORWARD;
if ($key === KeyCode::ENTER || $key === KeyCode::ESCAPE) if ($key === RawKeyCode::ENTER || $key === RawKeyCode::ESCAPE)
{ {
return; return;
} }
@ -577,17 +577,17 @@ class Editor {
$c = Terminal::readKey(); $c = Terminal::readKey();
$isModifier = in_array($c, $modifiers, TRUE); $isModifier = in_array($c, $modifiers, TRUE);
if ($c === KeyType::ESCAPE || ($c === KeyCode::ENTER && $buffer !== '')) if ($c === KeyType::ESCAPE || ($c === RawKeyCode::ENTER && $buffer !== ''))
{ {
$this->setStatusMessage(''); $this->setStatusMessage('');
if ($callback !== NULL) if ($callback !== NULL)
{ {
$callback($buffer, $c); $callback($buffer, $c);
} }
return ($c === KeyCode::ENTER) ? $buffer : ''; return ($c === RawKeyCode::ENTER) ? $buffer : '';
} }
if ($c === KeyType::DEL_KEY || $c === KeyType::BACKSPACE) if ($c === KeyType::DELETE || $c === KeyType::BACKSPACE)
{ {
$buffer = substr($buffer, 0, -1); $buffer = substr($buffer, 0, -1);
} }
@ -610,35 +610,28 @@ class Editor {
{ {
$c = Terminal::readKey(); $c = Terminal::readKey();
if ($c === KeyCode::NULL || $c === KeyCode::EMPTY) if ($c === RawKeyCode::NULL || $c === RawKeyCode::EMPTY)
{ {
return; return;
} }
switch ($c) switch ($c)
{ {
case KeyCode::CTRL('q'): case RawKeyCode::CTRL('q'):
$this->quitAttempt(); $this->quitAttempt();
return; return;
case KeyCode::CTRL('s'): case RawKeyCode::CTRL('s'):
$this->save(); $this->save();
break; break;
case KeyCode::CTRL('f'): case RawKeyCode::CTRL('f'):
$this->find(); $this->find();
break; break;
case KeyType::DEL_KEY: case KeyType::DELETE:
$this->document->delete($this->cursor);
break;
case KeyType::BACKSPACE: case KeyType::BACKSPACE:
if ($this->cursor->x > 0 || $this->cursor->y > 0) $this->removeChar($c);
{
$this->moveCursor(KeyType::ARROW_LEFT);
$this->document->delete($this->cursor);
}
break; break;
case KeyType::ARROW_UP: case KeyType::ARROW_UP:
@ -647,12 +640,12 @@ class Editor {
case KeyType::ARROW_RIGHT: case KeyType::ARROW_RIGHT:
case KeyType::PAGE_UP: case KeyType::PAGE_UP:
case KeyType::PAGE_DOWN: case KeyType::PAGE_DOWN:
case KeyType::HOME_KEY: case KeyType::HOME:
case KeyType::END_KEY: case KeyType::END:
$this->moveCursor($c); $this->moveCursor($c);
break; break;
case KeyCode::CTRL('l'): case RawKeyCode::CTRL('l'):
case KeyType::ESCAPE: case KeyType::ESCAPE:
// Do nothing // Do nothing
break; break;
@ -737,11 +730,11 @@ class Editor {
: $this->document->numRows; : $this->document->numRows;
break; break;
case KeyType::HOME_KEY: case KeyType::HOME:
$x = 0; $x = 0;
break; break;
case KeyType::END_KEY: case KeyType::END:
if ($y < $this->document->numRows) if ($y < $this->document->numRows)
{ {
$x = $row->size; $x = $row->size;
@ -773,6 +766,19 @@ class Editor {
$this->moveCursor(KeyType::ARROW_RIGHT); $this->moveCursor(KeyType::ARROW_RIGHT);
} }
protected function removeChar(string $ch): void
{
if ($ch === KeyType::DELETE)
{
$this->document->delete($this->cursor);
}
else if ($ch === KeyType::BACKSPACE && ($this->cursor->x > 0 || $this->cursor->y > 0))
{
$this->document->delete($this->cursor);
$this->moveCursor(KeyType::ARROW_LEFT);
}
}
protected function quitAttempt(): void protected function quitAttempt(): void
{ {
if ($this->document->isDirty() && $this->quitTimes > 0) if ($this->document->isDirty() && $this->quitTimes > 0)

View File

@ -16,11 +16,11 @@ class KeyType {
public const ARROW_RIGHT = 'KEY_ARROW_RIGHT'; public const ARROW_RIGHT = 'KEY_ARROW_RIGHT';
public const ARROW_UP = 'KEY_ARROW_UP'; public const ARROW_UP = 'KEY_ARROW_UP';
public const BACKSPACE = 'KEY_BACKSPACE'; public const BACKSPACE = 'KEY_BACKSPACE';
public const DEL_KEY = 'KEY_DELETE'; public const DELETE = 'KEY_DELETE';
public const END_KEY = 'KEY_END'; public const END = 'KEY_END';
public const ENTER = 'KEY_ENTER'; public const ENTER = 'KEY_ENTER';
public const ESCAPE = 'KEY_ESCAPE'; public const ESCAPE = 'KEY_ESCAPE';
public const HOME_KEY = 'KEY_HOME'; public const HOME = 'KEY_HOME';
public const PAGE_DOWN = 'KEY_PAGE_DOWN'; public const PAGE_DOWN = 'KEY_PAGE_DOWN';
public const PAGE_UP = 'KEY_PAGE_UP'; public const PAGE_UP = 'KEY_PAGE_UP';
} }

View File

@ -9,7 +9,7 @@ use function Aviat\Kilo\ctrl_key;
* 'Raw' input from stdin * 'Raw' input from stdin
* @enum * @enum
*/ */
class KeyCode { class RawKeyCode {
use Traits\ConstList; use Traits\ConstList;
public const ARROW_DOWN = "\e[B"; public const ARROW_DOWN = "\e[B";
@ -19,7 +19,7 @@ class KeyCode {
public const BACKSPACE = "\x7f"; public const BACKSPACE = "\x7f";
public const BELL = "\a"; public const BELL = "\a";
public const CARRIAGE_RETURN = "\r"; public const CARRIAGE_RETURN = "\r";
public const DEL_KEY = "\e[3~"; public const DELETE = "\e[3~";
public const EMPTY = ''; public const EMPTY = '';
public const ENTER = "\r"; public const ENTER = "\r";
public const ESCAPE = "\e"; public const ESCAPE = "\e";

View File

@ -3,7 +3,7 @@
namespace Aviat\Kilo; namespace Aviat\Kilo;
use Aviat\Kilo\Enum\Highlight; use Aviat\Kilo\Enum\Highlight;
use Aviat\Kilo\Enum\KeyCode; use Aviat\Kilo\Enum\RawKeyCode;
/** /**
* @property-read int $size * @property-read int $size
@ -305,7 +305,7 @@ class Row {
$klen = strlen($k); $klen = strlen($k);
$nextCharOffset = $i + $klen; $nextCharOffset = $i + $klen;
$isEndOfLine = $nextCharOffset >= $this->rsize; $isEndOfLine = $nextCharOffset >= $this->rsize;
$nextChar = ($isEndOfLine) ? KeyCode::NULL : $this->render[$nextCharOffset]; $nextChar = ($isEndOfLine) ? RawKeyCode::NULL : $this->render[$nextCharOffset];
if (substr($this->render, $i, $klen) === $k && is_separator($nextChar)) if (substr($this->render, $i, $klen) === $k && is_separator($nextChar))
{ {

View File

@ -2,7 +2,7 @@
namespace Aviat\Kilo; namespace Aviat\Kilo;
use Aviat\Kilo\Enum\KeyCode; use Aviat\Kilo\Enum\RawKeyCode;
use Aviat\Kilo\Enum\KeyType; use Aviat\Kilo\Enum\KeyType;
use Aviat\Kilo\Type\TerminalSize; use Aviat\Kilo\Type\TerminalSize;
@ -90,26 +90,26 @@ class Terminal {
return match($c) return match($c)
{ {
// Unambiguous mappings // Unambiguous mappings
KeyCode::ARROW_DOWN => KeyType::ARROW_DOWN, RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN,
KeyCode::ARROW_LEFT => KeyType::ARROW_LEFT, RawKeyCode::ARROW_LEFT => KeyType::ARROW_LEFT,
KeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT, RawKeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT,
KeyCode::ARROW_UP => KeyType::ARROW_UP, RawKeyCode::ARROW_UP => KeyType::ARROW_UP,
KeyCode::DEL_KEY => KeyType::DEL_KEY, RawKeyCode::DELETE => KeyType::DELETE,
KeyCode::ENTER => KeyType::ENTER, RawKeyCode::ENTER => KeyType::ENTER,
KeyCode::PAGE_DOWN => KeyType::PAGE_DOWN, RawKeyCode::PAGE_DOWN => KeyType::PAGE_DOWN,
KeyCode::PAGE_UP => KeyType::PAGE_UP, RawKeyCode::PAGE_UP => KeyType::PAGE_UP,
// Backspace // Backspace
KeyCode::CTRL('h'), KeyCode::BACKSPACE => KeyType::BACKSPACE, RawKeyCode::CTRL('h'), RawKeyCode::BACKSPACE => KeyType::BACKSPACE,
// Escape // Escape
KeyCode::CTRL('l'), KeyCode::ESCAPE => KeyType::ESCAPE, RawKeyCode::CTRL('l'), RawKeyCode::ESCAPE => KeyType::ESCAPE,
// Home Key // Home Key
"\eOH", "\e[7~", "\e[1~", ANSI::RESET_CURSOR => KeyType::HOME_KEY, "\eOH", "\e[7~", "\e[1~", ANSI::RESET_CURSOR => KeyType::HOME,
// End Key // End Key
"\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END_KEY, "\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END,
default => $c, default => $c,
}; };

View File

@ -2,7 +2,7 @@
namespace Aviat\Kilo; namespace Aviat\Kilo;
use Aviat\Kilo\Enum\{Color, Highlight, KeyCode}; use Aviat\Kilo\Enum\{Color, Highlight, RawKeyCode};
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// ! C function/macro equivalents // ! C function/macro equivalents
@ -77,12 +77,12 @@ function is_digit(string $char): bool
function is_space(string $char): bool function is_space(string $char): bool
{ {
return match($char) { return match($char) {
KeyCode::CARRIAGE_RETURN, RawKeyCode::CARRIAGE_RETURN,
KeyCode::FORM_FEED, RawKeyCode::FORM_FEED,
KeyCode::NEWLINE, RawKeyCode::NEWLINE,
KeyCode::SPACE, RawKeyCode::SPACE,
KeyCode::TAB, RawKeyCode::TAB,
KeyCode::VERTICAL_TAB => true, RawKeyCode::VERTICAL_TAB => true,
default => false, default => false,
}; };
@ -107,7 +107,7 @@ function is_separator(string $char): bool
$isSep = str_contains(',.()+-/*=~%<>[];', $char); $isSep = str_contains(',.()+-/*=~%<>[];', $char);
return is_space($char) || $char === KeyCode::NULL || $isSep; return is_space($char) || $char === RawKeyCode::NULL || $isSep;
} }
/** /**
@ -185,7 +185,7 @@ function syntax_to_color(int $hl): int
*/ */
function tabs_to_spaces(string $str, int $number = KILO_TAB_STOP): string function tabs_to_spaces(string $str, int $number = KILO_TAB_STOP): string
{ {
return str_replace(KeyCode::TAB, str_repeat(KeyCode::SPACE, $number), $str); return str_replace(RawKeyCode::TAB, str_repeat(RawKeyCode::SPACE, $number), $str);
} }
function error_code_name(int $code): string function error_code_name(int $code): string

View File

@ -4,7 +4,7 @@ namespace Aviat\Kilo\Tests\Enum;
use function Aviat\Kilo\ctrl_key; use function Aviat\Kilo\ctrl_key;
use Aviat\Kilo\Enum\KeyCode; use Aviat\Kilo\Enum\RawKeyCode;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class KeyCodeTest extends TestCase { class KeyCodeTest extends TestCase {
@ -16,7 +16,7 @@ class KeyCodeTest extends TestCase {
$ord = $i; $ord = $i;
$expected = chr($ord); $expected = chr($ord);
$actual = KeyCode::CTRL($char); $actual = RawKeyCode::CTRL($char);
$this->assertEquals(ctrl_key($char), $ord, 'chr(ctrl_key) !== CTRL'); $this->assertEquals(ctrl_key($char), $ord, 'chr(ctrl_key) !== CTRL');
$this->assertEquals($expected, $actual, "CTRL+'{$char}' should return chr($ord)"); $this->assertEquals($expected, $actual, "CTRL+'{$char}' should return chr($ord)");
@ -25,13 +25,13 @@ class KeyCodeTest extends TestCase {
public function testNullOnInvalidChar(): void public function testNullOnInvalidChar(): void
{ {
$this->assertNull(KeyCode::CTRL("\t")); $this->assertNull(RawKeyCode::CTRL("\t"));
} }
public function testSameOutputOnUpperOrLower(): void public function testSameOutputOnUpperOrLower(): void
{ {
$lower = KeyCode::CTRL('v'); $lower = RawKeyCode::CTRL('v');
$upper = KeyCode::CTRL('V'); $upper = RawKeyCode::CTRL('V');
$this->assertEquals($lower, $upper); $this->assertEquals($lower, $upper);
} }