From 644f27bb37d06b67831c42fcbd12750538a2518e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Apr 2021 13:52:01 -0400 Subject: [PATCH] More consistent names for keys --- src/Document.php | 4 +- src/Editor.php | 56 +++++++++++++----------- src/Enum/KeyType.php | 6 +-- src/Enum/{KeyCode.php => RawKeyCode.php} | 4 +- src/Row.php | 4 +- src/Terminal.php | 26 +++++------ src/functions.php | 18 ++++---- tests/Enum/KeyCodeTest.php | 10 ++--- 8 files changed, 67 insertions(+), 61 deletions(-) rename src/Enum/{KeyCode.php => RawKeyCode.php} (95%) diff --git a/src/Document.php b/src/Document.php index ebd06d1..0127982 100644 --- a/src/Document.php +++ b/src/Document.php @@ -2,7 +2,7 @@ namespace Aviat\Kilo; -use Aviat\Kilo\Enum\KeyCode; +use Aviat\Kilo\Enum\RawKeyCode; use Aviat\Kilo\Enum\KeyType; use Aviat\Kilo\Tokens\PHP8; use Aviat\Kilo\Type\Point; @@ -102,7 +102,7 @@ class Document { return; } - if ($c === KeyType::ENTER || $c === KeyCode::CARRIAGE_RETURN) + if ($c === KeyType::ENTER || $c === RawKeyCode::CARRIAGE_RETURN) { $this->insertNewline($at); return; diff --git a/src/Editor.php b/src/Editor.php index 36885fc..9320bca 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -5,7 +5,7 @@ namespace Aviat\Kilo; use Aviat\Kilo\Type\TerminalSize; use Aviat\Kilo\Enum\{ Color, - KeyCode, + RawKeyCode, KeyType, Highlight, SearchDirection @@ -163,7 +163,7 @@ class Editor { $rx = 0; 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); } @@ -185,7 +185,7 @@ class Editor { $cur_rx = 0; 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); } @@ -266,7 +266,7 @@ class Editor { $lastMatch = NO_MATCH; $direction = SearchDirection::FORWARD; - if ($key === KeyCode::ENTER || $key === KeyCode::ESCAPE) + if ($key === RawKeyCode::ENTER || $key === RawKeyCode::ESCAPE) { return; } @@ -577,17 +577,17 @@ class Editor { $c = Terminal::readKey(); $isModifier = in_array($c, $modifiers, TRUE); - if ($c === KeyType::ESCAPE || ($c === KeyCode::ENTER && $buffer !== '')) + if ($c === KeyType::ESCAPE || ($c === RawKeyCode::ENTER && $buffer !== '')) { $this->setStatusMessage(''); if ($callback !== NULL) { $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); } @@ -610,35 +610,28 @@ class Editor { { $c = Terminal::readKey(); - if ($c === KeyCode::NULL || $c === KeyCode::EMPTY) + if ($c === RawKeyCode::NULL || $c === RawKeyCode::EMPTY) { return; } switch ($c) { - case KeyCode::CTRL('q'): + case RawKeyCode::CTRL('q'): $this->quitAttempt(); return; - case KeyCode::CTRL('s'): + case RawKeyCode::CTRL('s'): $this->save(); break; - case KeyCode::CTRL('f'): + case RawKeyCode::CTRL('f'): $this->find(); break; - case KeyType::DEL_KEY: - $this->document->delete($this->cursor); - break; - + case KeyType::DELETE: case KeyType::BACKSPACE: - if ($this->cursor->x > 0 || $this->cursor->y > 0) - { - $this->moveCursor(KeyType::ARROW_LEFT); - $this->document->delete($this->cursor); - } + $this->removeChar($c); break; case KeyType::ARROW_UP: @@ -647,12 +640,12 @@ class Editor { case KeyType::ARROW_RIGHT: case KeyType::PAGE_UP: case KeyType::PAGE_DOWN: - case KeyType::HOME_KEY: - case KeyType::END_KEY: + case KeyType::HOME: + case KeyType::END: $this->moveCursor($c); break; - case KeyCode::CTRL('l'): + case RawKeyCode::CTRL('l'): case KeyType::ESCAPE: // Do nothing break; @@ -737,11 +730,11 @@ class Editor { : $this->document->numRows; break; - case KeyType::HOME_KEY: + case KeyType::HOME: $x = 0; break; - case KeyType::END_KEY: + case KeyType::END: if ($y < $this->document->numRows) { $x = $row->size; @@ -773,6 +766,19 @@ class Editor { $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 { if ($this->document->isDirty() && $this->quitTimes > 0) diff --git a/src/Enum/KeyType.php b/src/Enum/KeyType.php index 25cbbe6..a9a32d2 100644 --- a/src/Enum/KeyType.php +++ b/src/Enum/KeyType.php @@ -16,11 +16,11 @@ class KeyType { public const ARROW_RIGHT = 'KEY_ARROW_RIGHT'; public const ARROW_UP = 'KEY_ARROW_UP'; public const BACKSPACE = 'KEY_BACKSPACE'; - public const DEL_KEY = 'KEY_DELETE'; - public const END_KEY = 'KEY_END'; + public const DELETE = 'KEY_DELETE'; + public const END = 'KEY_END'; public const ENTER = 'KEY_ENTER'; 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_UP = 'KEY_PAGE_UP'; } \ No newline at end of file diff --git a/src/Enum/KeyCode.php b/src/Enum/RawKeyCode.php similarity index 95% rename from src/Enum/KeyCode.php rename to src/Enum/RawKeyCode.php index faa0806..9a07db4 100644 --- a/src/Enum/KeyCode.php +++ b/src/Enum/RawKeyCode.php @@ -9,7 +9,7 @@ use function Aviat\Kilo\ctrl_key; * 'Raw' input from stdin * @enum */ -class KeyCode { +class RawKeyCode { use Traits\ConstList; public const ARROW_DOWN = "\e[B"; @@ -19,7 +19,7 @@ class KeyCode { public const BACKSPACE = "\x7f"; public const BELL = "\a"; public const CARRIAGE_RETURN = "\r"; - public const DEL_KEY = "\e[3~"; + public const DELETE = "\e[3~"; public const EMPTY = ''; public const ENTER = "\r"; public const ESCAPE = "\e"; diff --git a/src/Row.php b/src/Row.php index 2f665d4..823f67a 100644 --- a/src/Row.php +++ b/src/Row.php @@ -3,7 +3,7 @@ namespace Aviat\Kilo; use Aviat\Kilo\Enum\Highlight; -use Aviat\Kilo\Enum\KeyCode; +use Aviat\Kilo\Enum\RawKeyCode; /** * @property-read int $size @@ -305,7 +305,7 @@ class Row { $klen = strlen($k); $nextCharOffset = $i + $klen; $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)) { diff --git a/src/Terminal.php b/src/Terminal.php index 25bb978..2ff37b1 100644 --- a/src/Terminal.php +++ b/src/Terminal.php @@ -2,7 +2,7 @@ namespace Aviat\Kilo; -use Aviat\Kilo\Enum\KeyCode; +use Aviat\Kilo\Enum\RawKeyCode; use Aviat\Kilo\Enum\KeyType; use Aviat\Kilo\Type\TerminalSize; @@ -90,26 +90,26 @@ class Terminal { return match($c) { // Unambiguous mappings - KeyCode::ARROW_DOWN => KeyType::ARROW_DOWN, - KeyCode::ARROW_LEFT => KeyType::ARROW_LEFT, - KeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT, - KeyCode::ARROW_UP => KeyType::ARROW_UP, - KeyCode::DEL_KEY => KeyType::DEL_KEY, - KeyCode::ENTER => KeyType::ENTER, - KeyCode::PAGE_DOWN => KeyType::PAGE_DOWN, - KeyCode::PAGE_UP => KeyType::PAGE_UP, + RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN, + RawKeyCode::ARROW_LEFT => KeyType::ARROW_LEFT, + RawKeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT, + RawKeyCode::ARROW_UP => KeyType::ARROW_UP, + RawKeyCode::DELETE => KeyType::DELETE, + RawKeyCode::ENTER => KeyType::ENTER, + RawKeyCode::PAGE_DOWN => KeyType::PAGE_DOWN, + RawKeyCode::PAGE_UP => KeyType::PAGE_UP, // Backspace - KeyCode::CTRL('h'), KeyCode::BACKSPACE => KeyType::BACKSPACE, + RawKeyCode::CTRL('h'), RawKeyCode::BACKSPACE => KeyType::BACKSPACE, // Escape - KeyCode::CTRL('l'), KeyCode::ESCAPE => KeyType::ESCAPE, + RawKeyCode::CTRL('l'), RawKeyCode::ESCAPE => KeyType::ESCAPE, // 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 - "\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END_KEY, + "\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END, default => $c, }; diff --git a/src/functions.php b/src/functions.php index 9893a86..4a559f4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,7 +2,7 @@ namespace Aviat\Kilo; -use Aviat\Kilo\Enum\{Color, Highlight, KeyCode}; +use Aviat\Kilo\Enum\{Color, Highlight, RawKeyCode}; // ---------------------------------------------------------------------------- // ! C function/macro equivalents @@ -77,12 +77,12 @@ function is_digit(string $char): bool function is_space(string $char): bool { return match($char) { - KeyCode::CARRIAGE_RETURN, - KeyCode::FORM_FEED, - KeyCode::NEWLINE, - KeyCode::SPACE, - KeyCode::TAB, - KeyCode::VERTICAL_TAB => true, + RawKeyCode::CARRIAGE_RETURN, + RawKeyCode::FORM_FEED, + RawKeyCode::NEWLINE, + RawKeyCode::SPACE, + RawKeyCode::TAB, + RawKeyCode::VERTICAL_TAB => true, default => false, }; @@ -107,7 +107,7 @@ function is_separator(string $char): bool $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 { - 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 diff --git a/tests/Enum/KeyCodeTest.php b/tests/Enum/KeyCodeTest.php index c62cd2f..6eb90ed 100644 --- a/tests/Enum/KeyCodeTest.php +++ b/tests/Enum/KeyCodeTest.php @@ -4,7 +4,7 @@ namespace Aviat\Kilo\Tests\Enum; use function Aviat\Kilo\ctrl_key; -use Aviat\Kilo\Enum\KeyCode; +use Aviat\Kilo\Enum\RawKeyCode; use PHPUnit\Framework\TestCase; class KeyCodeTest extends TestCase { @@ -16,7 +16,7 @@ class KeyCodeTest extends TestCase { $ord = $i; $expected = chr($ord); - $actual = KeyCode::CTRL($char); + $actual = RawKeyCode::CTRL($char); $this->assertEquals(ctrl_key($char), $ord, 'chr(ctrl_key) !== CTRL'); $this->assertEquals($expected, $actual, "CTRL+'{$char}' should return chr($ord)"); @@ -25,13 +25,13 @@ class KeyCodeTest extends TestCase { public function testNullOnInvalidChar(): void { - $this->assertNull(KeyCode::CTRL("\t")); + $this->assertNull(RawKeyCode::CTRL("\t")); } public function testSameOutputOnUpperOrLower(): void { - $lower = KeyCode::CTRL('v'); - $upper = KeyCode::CTRL('V'); + $lower = RawKeyCode::CTRL('v'); + $upper = RawKeyCode::CTRL('V'); $this->assertEquals($lower, $upper); }