Only refresh screen when a key is pressed
Some checks failed
timw4mail/php-kilo/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2022-08-23 14:09:00 -04:00
parent 07fded57e8
commit 5887faf7d4

View File

@ -126,10 +126,15 @@ class Editor {
*/ */
public function run(): void public function run(): void
{ {
$this->refreshScreen();
while ( ! $this->shouldQuit) while ( ! $this->shouldQuit)
{ {
$this->refreshScreen(); // Don't redraw unless the screen actually needs to update!
$this->processKeypress(); if ($this->processKeypress() !== false)
{
$this->refreshScreen();
}
} }
} }
@ -602,29 +607,31 @@ class Editor {
/** /**
* Input processing * Input processing
*
* Returns `false` on no keypress
*/ */
protected function processKeypress(): void protected function processKeypress(): bool|null
{ {
$c = Terminal::readKey(); $c = Terminal::readKey();
if ($c === RawKeyCode::NULL || $c === RawKeyCode::EMPTY) if ($c === RawKeyCode::NULL || $c === RawKeyCode::EMPTY)
{ {
return; return false;
} }
switch ($c) switch ($c)
{ {
case RawKeyCode::CTRL('q'): case RawKeyCode::CTRL('q'):
$this->quitAttempt(); $this->quitAttempt();
return; return true;
case RawKeyCode::CTRL('s'): case RawKeyCode::CTRL('s'):
$this->save(); $this->save();
break; break;
case RawKeyCode::CTRL('f'): case RawKeyCode::CTRL('f'):
$this->find(); $this->find();
break; break;
case KeyType::Delete: case KeyType::Delete:
case KeyType::Backspace: case KeyType::Backspace:
@ -658,6 +665,8 @@ class Editor {
$this->quitTimes = KILO_QUIT_TIMES; $this->quitTimes = KILO_QUIT_TIMES;
$this->setStatusMessage(''); $this->setStatusMessage('');
} }
return true;
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------