From 60b1cca69588ba4af02246586f5e3c86eeeddd65 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 29 Jan 2020 16:13:05 -0500 Subject: [PATCH] Cut down on number of keycode escapes with constants --- src/Editor.php | 12 ++++++------ src/Enum/Event.php | 12 ------------ src/Enum/KeyCode.php | 8 ++++++++ src/Event.php | 19 ++++++++++++++----- src/Row.php | 3 ++- src/functions.php | 27 ++++++++++++++------------- tests/EventTest.php | 7 +++---- tests/FunctionTest.php | 10 +++++----- 8 files changed, 52 insertions(+), 46 deletions(-) delete mode 100644 src/Enum/Event.php diff --git a/src/Editor.php b/src/Editor.php index 61d67b3..e882573 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -177,7 +177,7 @@ class Editor { $rx = 0; for ($i = 0; $i < $cx; $i++) { - if ($row->chars[$i] === "\t") + if ($row->chars[$i] === KeyCode::TAB) { $rx += (KILO_TAB_STOP - 1) - ($rx % KILO_TAB_STOP); } @@ -192,7 +192,7 @@ class Editor { $cur_rx = 0; for ($cx = 0; $cx < $row->size; $cx++) { - if ($row->chars[$cx] === "\t") + if ($row->chars[$cx] === KeyCode::TAB) { $cur_rx += (KILO_TAB_STOP - 1) - ($cur_rx % KILO_TAB_STOP); } @@ -426,7 +426,7 @@ class Editor { $savedHl = []; } - if ($key === "\r" || $key === "\e") + if ($key === KeyCode::ENTER || $key === KeyCode::ESCAPE) { $lastMatch = -1; $direction = 1; @@ -598,7 +598,7 @@ class Editor { for ($i = 0; $i < $len; $i++) { // Handle 'non-printable' characters - if (is_cntrl($c[$i])) + if (is_ctrl($c[$i])) { $sym = (ord($c[$i]) <= 26) ? chr(ord('@') + ord($c[$i])) @@ -760,7 +760,7 @@ class Editor { { $buffer = substr($buffer, 0, -1); } - else if (is_ascii($c) && ( ! is_cntrl($c)) && ! in_array($c, $modifiers, TRUE)) + else if (is_ascii($c) && ( ! is_ctrl($c)) && ! in_array($c, $modifiers, TRUE)) { $buffer .= $c; } @@ -835,7 +835,7 @@ class Editor { $c = $this->readKey(); - if ($c === "\0" || $c === '') + if ($c === KeyCode::NULL || $c === KeyCode::EMPTY) { return ''; } diff --git a/src/Enum/Event.php b/src/Enum/Event.php deleted file mode 100644 index e297d51..0000000 --- a/src/Enum/Event.php +++ /dev/null @@ -1,12 +0,0 @@ -= $this->rsize; - $nextChar = ($isEndOfLine) ? "\0" : $this->render[$nextCharOffset]; + $nextChar = ($isEndOfLine) ? KeyCode::NULL : $this->render[$nextCharOffset]; if (substr($this->render, $i, $klen) === $k && is_separator($nextChar)) { diff --git a/src/functions.php b/src/functions.php index dfc2ee3..7f40e4f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -4,11 +4,7 @@ namespace Aviat\Kilo; use FFI; -use Aviat\Kilo\Enum\{ - C, - Color, - Highlight, -}; +use Aviat\Kilo\Enum\{C, Color, Highlight, KeyCode}; /** * See if tput exists for fallback terminal size detection @@ -18,7 +14,7 @@ use Aviat\Kilo\Enum\{ */ function has_tput(): bool { - return strpos(shell_exec('type tput'), ' is ') !== FALSE; + return str_contains(shell_exec('type tput'), ' is '); } // ---------------------------------------------------------------------------- @@ -104,7 +100,7 @@ function is_ascii(string $single_char): bool * @param string $char * @return bool */ -function is_cntrl(string $char): bool +function is_ctrl(string $char): bool { $c = ord($char); return is_ascii($char) && ( $c === 0x7f || $c < 0x20 ); @@ -130,7 +126,14 @@ function is_digit(string $char): bool */ function is_space(string $char): bool { - $ws = [' ', "\t", "\n", "\r", "\v", "\f"]; + $ws = [ + KeyCode::CARRIAGE_RETURN, + KeyCode::FORM_FEED, + KeyCode::NEWLINE, + KeyCode::SPACE, + KeyCode::TAB, + KeyCode::VERTICAL_TAB, + ]; return is_ascii($char) && in_array($char, $ws, TRUE); } @@ -168,11 +171,9 @@ function is_separator(string $char): bool return FALSE; } - // `strpos` is used instead of `strchr`/`strstr` as we don't care about the actual match - // while `strchr` would match the C version, it also returns the match - $isSep = (strpos(',.()+-/*=~%<>[];', $char) !== FALSE); + $isSep = str_contains(',.()+-/*=~%<>[];', $char); - return is_space($char) || $char === "\0" || $isSep; + return is_space($char) || $char === KeyCode::NULL || $isSep; } /** @@ -287,7 +288,7 @@ function syntax_to_color(int $hl): int */ function tabs_to_spaces(string $str, ?int $number = KILO_TAB_STOP): string { - return str_replace("\t", str_repeat(' ', $number), $str); + return str_replace(KeyCode::TAB, str_repeat(KeyCode::SPACE, $number), $str); } /** diff --git a/tests/EventTest.php b/tests/EventTest.php index 41e419e..09df342 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -3,7 +3,6 @@ namespace Aviat\Kilo\Tests; use Aviat\Kilo\Event; -use Aviat\Kilo\Enum\Event as EventType; use InvalidArgumentException; use PHPUnit\Framework\TestCase; @@ -11,7 +10,7 @@ class EventTest extends TestCase { public function testRequiresValidEvent(): void { $this->expectException(InvalidArgumentException::class); - Event::bind('badEventName', fn () => null); + Event::on('badEventName', fn () => null); $this->expectException(InvalidArgumentException::class); Event::fire('badEventName', []); @@ -22,7 +21,7 @@ class EventTest extends TestCase { $fn = static function($value = false) { static::assertTrue($value); }; - Event::bind(EventType::INPUT_KEY, $fn); - Event::fire(EventType::INPUT_KEY, TRUE); + Event::on(Event::INPUT_KEY, $fn); + Event::fire(Event::INPUT_KEY, TRUE); } } diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 1945d6a..f7afa26 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -11,7 +11,7 @@ use function Aviat\Kilo\ctrl_key; use function Aviat\Kilo\get_file_syntax_map; use function Aviat\Kilo\get_window_size; use function Aviat\Kilo\is_ascii; -use function Aviat\Kilo\is_cntrl; +use function Aviat\Kilo\is_ctrl; use function Aviat\Kilo\is_digit; use function Aviat\Kilo\is_separator; use function Aviat\Kilo\is_space; @@ -42,22 +42,22 @@ class FunctionTest extends TestCase { $this->assertEquals(0x01, ctrl_key('a')); } - public function test_is_cntrl(): void + public function test_is_ctrl(): void { for ($i = 0x0; $i < 0x20; $i++) { $char = chr($i); - $this->assertTrue(is_cntrl($char), 'Should be a control character'); + $this->assertTrue(is_ctrl($char), 'Should be a control character'); } for ($n = 0x20; $n < 0x7f; $n++) { $char = chr($n); - $this->assertFalse(is_cntrl($char), 'Should not be a control character'); + $this->assertFalse(is_ctrl($char), 'Should not be a control character'); } // Escape, code 7f, is an outlier - $this->assertTrue(is_cntrl(chr(0x7f))); + $this->assertTrue(is_ctrl(chr(0x7f))); } public function test_is_space(): void