From 5329378b939dca160cfcfee968856e761aecf434 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 9 Mar 2021 13:37:03 -0500 Subject: [PATCH] Cleanup some unused code --- src/Editor.php | 76 +++++++++++++++++++++++++++++---------------- src/Event.php | 56 --------------------------------- src/Position.php | 7 +---- src/Row.php | 4 ++- tests/EventTest.php | 27 ---------------- 5 files changed, 54 insertions(+), 116 deletions(-) delete mode 100644 src/Event.php delete mode 100644 tests/EventTest.php diff --git a/src/Editor.php b/src/Editor.php index 9eadaf9..4dc58db 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -65,8 +65,8 @@ class Editor { private function __construct() { $this->statusMsgTime = time(); - $this->cursor = Position::default(); - $this->offset = Position::default(); + $this->cursor = Position::new(); + $this->offset = Position::new(); [$this->screenRows, $this->screenCols] = Terminal::getWindowSize(); @@ -172,6 +172,13 @@ class Editor { // ! Row Operations // ------------------------------------------------------------------------ + /** + * Cursor X to Render X + * + * @param Row $row + * @param int $cx + * @return int + */ protected function rowCxToRx(Row $row, int $cx): int { $rx = 0; @@ -187,6 +194,13 @@ class Editor { return $rx; } + /** + * Render X to Cursor X + * + * @param Row $row + * @param int $rx + * @return int + */ protected function rowRxToCx(Row $row, int $rx): int { $cur_rx = 0; @@ -773,68 +787,71 @@ class Editor { protected function moveCursor(string $key): void { - $row = $this->rows[$this->cursor->y]; + $x = $this->cursor->x; + $y = $this->cursor->y; + $row = $this->rows[$y]; switch ($key) { case KeyType::ARROW_LEFT: - if ($this->cursor->x !== 0) + if ($x !== 0) { - $this->cursor->x--; + $x--; } - else if ($this->cursor->y > 0) + else if ($y > 0) { - $this->cursor->y--; - $this->cursor->x = $this->rows[$this->cursor->y]->size; + // Beginning of a line, go to end of previous line + $y--; + $x = $this->rows[$y]->size - 1; } break; case KeyType::ARROW_RIGHT: - if ($row && $this->cursor->x < $row->size) + if ($row && $x < $row->size) { - $this->cursor->x++; + $x++; } - else if ($row && $this->cursor->x === $row->size) + else if ($row && $x === $row->size) { - $this->cursor->y++; - $this->cursor->x = 0; + $y++; + $x = 0; } break; case KeyType::ARROW_UP: - if ($this->cursor->y !== 0) + if ($y !== 0) { - $this->cursor->y--; + $y--; } break; case KeyType::ARROW_DOWN: - if ($this->cursor->y < $this->numRows) + if ($y < $this->numRows) { - $this->cursor->y++; + $y++; } break; case KeyType::PAGE_UP: - $this->cursor->y = ($this->cursor->y > $this->screenRows) - ? $this->cursor->y - $this->screenRows + $y = ($y > $this->screenRows) + ? $y - $this->screenRows : 0; break; case KeyType::PAGE_DOWN: - $this->cursor->y = ($this->cursor->y + $this->screenRows < $this->numRows) - ? $this->cursor->y + $this->screenRows + $y = ($y + $this->screenRows < $this->numRows) + ? $y + $this->screenRows : $this->numRows; break; case KeyType::HOME_KEY: - $this->cursor->x = 0; + $x = 0; break; case KeyType::END_KEY: - if ($this->cursor->y < $this->numRows) + if ($y < $this->numRows) { - $this->cursor->x = $this->rows[$this->cursor->y]->size - 1; + $x = $this->rows[$y]->size - 1; } break; @@ -842,10 +859,17 @@ class Editor { // Do nothing } - if ($this->cursor->x > $row->size) + if ($x > $row->size) { - $this->cursor->x = $row->size; + $x = $row->size; } + if ($y > $this->screenRows) + { + $y = $this->screenRows; + } + + $this->cursor->x = $x; + $this->cursor->y = $y; } public function processKeypress(): ?string diff --git a/src/Event.php b/src/Event.php deleted file mode 100644 index 6c9f9af..0000000 --- a/src/Event.php +++ /dev/null @@ -1,56 +0,0 @@ -expectException(InvalidArgumentException::class); - Event::on('badEventName', fn () => null); - - $this->expectException(InvalidArgumentException::class); - Event::fire('badEventName', []); - } - - public function testBindAndFire(): void - { - $fn = static function($value = false) { - static::assertTrue($value); - }; - Event::on(Event::INPUT_KEY, $fn); - Event::fire(Event::INPUT_KEY, TRUE); - } -}