From 5887faf7d4dcb1811e0f12f5fb781b639a383097 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 23 Aug 2022 14:09:00 -0400 Subject: [PATCH] Only refresh screen when a key is pressed --- src/Editor.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Editor.php b/src/Editor.php index 3204c7a..d5feaea 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -126,10 +126,15 @@ class Editor { */ public function run(): void { + $this->refreshScreen(); + while ( ! $this->shouldQuit) { - $this->refreshScreen(); - $this->processKeypress(); + // Don't redraw unless the screen actually needs to update! + if ($this->processKeypress() !== false) + { + $this->refreshScreen(); + } } } @@ -602,29 +607,31 @@ class Editor { /** * Input processing + * + * Returns `false` on no keypress */ - protected function processKeypress(): void + protected function processKeypress(): bool|null { $c = Terminal::readKey(); if ($c === RawKeyCode::NULL || $c === RawKeyCode::EMPTY) { - return; + return false; } switch ($c) { case RawKeyCode::CTRL('q'): $this->quitAttempt(); - return; + return true; - case RawKeyCode::CTRL('s'): - $this->save(); - break; + case RawKeyCode::CTRL('s'): + $this->save(); + break; - case RawKeyCode::CTRL('f'): - $this->find(); - break; + case RawKeyCode::CTRL('f'): + $this->find(); + break; case KeyType::Delete: case KeyType::Backspace: @@ -658,6 +665,8 @@ class Editor { $this->quitTimes = KILO_QUIT_TIMES; $this->setStatusMessage(''); } + + return true; } // ------------------------------------------------------------------------