From 998102816e9352841cac6e27c28976fa62017782 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 10 Oct 2023 13:02:05 -0400 Subject: [PATCH] Use c stdlib function to set up raw mode --- kilo | 2 + src/Editor.php | 13 ++-- src/{ => Terminal}/ANSI.php | 3 +- src/{ => Terminal}/C.php | 2 +- src/{ => Terminal}/Terminal.php | 6 +- src/{ => Terminal}/Termios.php | 9 +-- src/{ => Terminal}/TermiosException.php | 7 +- src/{ => Terminal}/ffi.h | 3 +- tests/ANSITest.php | 2 +- tests/EditorTest.php | 2 +- tests/TerminalTest.php | 3 +- tests/TermiosTest.php | 2 +- .../EditorTest__testOpen__1.json | 67 ++++++++++++++----- 13 files changed, 76 insertions(+), 45 deletions(-) rename src/{ => Terminal}/ANSI.php (98%) rename src/{ => Terminal}/C.php (99%) rename src/{ => Terminal}/Terminal.php (97%) rename src/{ => Terminal}/Termios.php (90%) rename src/{ => Terminal}/TermiosException.php (50%) rename src/{ => Terminal}/ffi.h (96%) diff --git a/kilo b/kilo index 9de083e..9d55bdf 100755 --- a/kilo +++ b/kilo @@ -3,6 +3,8 @@ namespace Aviat\Kilo; +use Aviat\Kilo\Terminal\Termios; + require_once __DIR__ . '/src/Kilo.php'; // Remove the composer install requirement by diff --git a/src/Editor.php b/src/Editor.php index c2d1739..84e1f7f 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -2,16 +2,11 @@ namespace Aviat\Kilo; -use Aviat\Kilo\Type\TerminalSize; -use Aviat\Kilo\Enum\{ - Color, - Color256, - RawKeyCode, - KeyType, - Highlight, - SearchDirection -}; +use Aviat\Kilo\Enum\{Color, Highlight, KeyType, RawKeyCode, SearchDirection}; +use Aviat\Kilo\Terminal\ANSI; +use Aviat\Kilo\Terminal\Terminal; use Aviat\Kilo\Type\{Point, StatusMessage}; +use Aviat\Kilo\Type\TerminalSize; /** * // Don't highlight this! diff --git a/src/ANSI.php b/src/Terminal/ANSI.php similarity index 98% rename from src/ANSI.php rename to src/Terminal/ANSI.php index 460c80c..0163bf0 100644 --- a/src/ANSI.php +++ b/src/Terminal/ANSI.php @@ -1,7 +1,8 @@ originalTermios; - $termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON); - $termios->c_oflag &= ~(C::OPOST); - $termios->c_cflag |= (C::CS8); - $termios->c_lflag &= ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG ); - $termios->c_cc[C::VMIN] = 0; - $termios->c_cc[C::VTIME] = 1; // Turn on raw mode + self::ffi()->cfmakeraw(FFI::addr($termios)); $res = self::ffi() ->tcsetattr(C::STDIN_FILENO, C::TCSAFLUSH, FFI::addr($termios)); diff --git a/src/TermiosException.php b/src/Terminal/TermiosException.php similarity index 50% rename from src/TermiosException.php rename to src/Terminal/TermiosException.php index 18fdb15..76f2a09 100644 --- a/src/TermiosException.php +++ b/src/Terminal/TermiosException.php @@ -1,11 +1,14 @@ diff --git a/tests/ANSITest.php b/tests/ANSITest.php index 672b5e9..73780ed 100644 --- a/tests/ANSITest.php +++ b/tests/ANSITest.php @@ -2,8 +2,8 @@ namespace Aviat\Kilo\Tests; -use Aviat\Kilo\ANSI; use Aviat\Kilo\Enum\Color; +use Aviat\Kilo\Terminal\ANSI; use PHPUnit\Framework\TestCase; class ANSITest extends TestCase { diff --git a/tests/EditorTest.php b/tests/EditorTest.php index eb995ef..c8402f8 100644 --- a/tests/EditorTest.php +++ b/tests/EditorTest.php @@ -47,7 +47,7 @@ class EditorTest extends TestCase { public function testOpen(): void { - $editor = MockEditor::mock('src/ffi.h'); + $editor = MockEditor::mock('src/Terminal/ffi.h'); $state = json_encode($editor->__debugInfo(), JSON_THROW_ON_ERROR); $this->assertMatchesJsonSnapshot($state); diff --git a/tests/TerminalTest.php b/tests/TerminalTest.php index b97cc4e..7fb7eff 100644 --- a/tests/TerminalTest.php +++ b/tests/TerminalTest.php @@ -2,10 +2,9 @@ namespace Aviat\Kilo\Tests; +use Aviat\Kilo\Terminal\Terminal; use PHPUnit\Framework\TestCase; -use Aviat\Kilo\Terminal; - class TerminalTest extends TestCase { public function test_getWindowSize(): void diff --git a/tests/TermiosTest.php b/tests/TermiosTest.php index 7b0d011..a572028 100644 --- a/tests/TermiosTest.php +++ b/tests/TermiosTest.php @@ -2,7 +2,7 @@ namespace Aviat\Kilo\Tests; -use Aviat\Kilo\{Termios, TermiosException}; +use Aviat\Kilo\{Terminal\Termios, Terminal\TermiosException}; use PHPUnit\Framework\TestCase; class TermiosTest extends TestCase { diff --git a/tests/__snapshots__/EditorTest__testOpen__1.json b/tests/__snapshots__/EditorTest__testOpen__1.json index d44ea8a..a01290d 100644 --- a/tests/__snapshots__/EditorTest__testOpen__1.json +++ b/tests/__snapshots__/EditorTest__testOpen__1.json @@ -103,7 +103,7 @@ } }, "tokens": [], - "filename": "src\/ffi.h", + "filename": "src\/Terminal\/ffi.h", "rows": [ { "render": "\/**", @@ -465,7 +465,7 @@ "idx": 10 }, { - "render": "#define FFI_LIB \"libc.so.6\"", + "render": "#define FFI_LIB \"libc.so\"", "hl": [ "Keyword2", "Keyword2", @@ -491,8 +491,6 @@ "String", "String", "String", - "String", - "String", "String" ], "idx": 11 @@ -1740,10 +1738,49 @@ ], "idx": 56 }, + { + "render": "void cfmakeraw(struct termios *);", + "hl": [ + "Keyword2", + "Keyword2", + "Keyword2", + "Keyword2", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Delimiter", + "Keyword1", + "Keyword1", + "Keyword1", + "Keyword1", + "Keyword1", + "Keyword1", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Normal", + "Operator", + "Delimiter", + "Operator" + ], + "idx": 57 + }, { "render": "", "hl": [], - "idx": 57 + "idx": 58 }, { "render": "\/\/ -----------------------------------------------------------------------------", @@ -1829,7 +1866,7 @@ "Comment", "Comment" ], - "idx": 58 + "idx": 59 }, { "render": "\/\/! ", @@ -1852,7 +1889,7 @@ "Comment", "Comment" ], - "idx": 59 + "idx": 60 }, { "render": "\/\/ -----------------------------------------------------------------------------", @@ -1938,7 +1975,7 @@ "Comment", "Comment" ], - "idx": 60 + "idx": 61 }, { "render": "struct winsize {", @@ -1960,7 +1997,7 @@ "Normal", "Delimiter" ], - "idx": 61 + "idx": 62 }, { "render": " unsigned short ws_row;", @@ -1992,7 +2029,7 @@ "Normal", "Operator" ], - "idx": 62 + "idx": 63 }, { "render": " unsigned short ws_col;", @@ -2024,7 +2061,7 @@ "Normal", "Operator" ], - "idx": 63 + "idx": 64 }, { "render": " unsigned short ws_xpixel;", @@ -2059,7 +2096,7 @@ "Normal", "Operator" ], - "idx": 64 + "idx": 65 }, { "render": " unsigned short ws_ypixel;", @@ -2094,7 +2131,7 @@ "Normal", "Operator" ], - "idx": 65 + "idx": 66 }, { "render": "};", @@ -2102,7 +2139,7 @@ "Delimiter", "Operator" ], - "idx": 66 + "idx": 67 }, { "render": "int ioctl (int, int, ...);", @@ -2134,7 +2171,7 @@ "Delimiter", "Operator" ], - "idx": 67 + "idx": 68 } ], "dirty": false