Cleanup some unused code
timw4mail/php-kilo/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2021-03-09 13:37:03 -05:00
parent 3b6cd21070
commit 5329378b93
5 changed files with 54 additions and 116 deletions

View File

@ -65,8 +65,8 @@ class Editor {
private function __construct() private function __construct()
{ {
$this->statusMsgTime = time(); $this->statusMsgTime = time();
$this->cursor = Position::default(); $this->cursor = Position::new();
$this->offset = Position::default(); $this->offset = Position::new();
[$this->screenRows, $this->screenCols] = Terminal::getWindowSize(); [$this->screenRows, $this->screenCols] = Terminal::getWindowSize();
@ -172,6 +172,13 @@ class Editor {
// ! Row Operations // ! Row Operations
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/**
* Cursor X to Render X
*
* @param Row $row
* @param int $cx
* @return int
*/
protected function rowCxToRx(Row $row, int $cx): int protected function rowCxToRx(Row $row, int $cx): int
{ {
$rx = 0; $rx = 0;
@ -187,6 +194,13 @@ class Editor {
return $rx; return $rx;
} }
/**
* Render X to Cursor X
*
* @param Row $row
* @param int $rx
* @return int
*/
protected function rowRxToCx(Row $row, int $rx): int protected function rowRxToCx(Row $row, int $rx): int
{ {
$cur_rx = 0; $cur_rx = 0;
@ -773,68 +787,71 @@ class Editor {
protected function moveCursor(string $key): void 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) switch ($key)
{ {
case KeyType::ARROW_LEFT: 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--; // Beginning of a line, go to end of previous line
$this->cursor->x = $this->rows[$this->cursor->y]->size; $y--;
$x = $this->rows[$y]->size - 1;
} }
break; break;
case KeyType::ARROW_RIGHT: 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++; $y++;
$this->cursor->x = 0; $x = 0;
} }
break; break;
case KeyType::ARROW_UP: case KeyType::ARROW_UP:
if ($this->cursor->y !== 0) if ($y !== 0)
{ {
$this->cursor->y--; $y--;
} }
break; break;
case KeyType::ARROW_DOWN: case KeyType::ARROW_DOWN:
if ($this->cursor->y < $this->numRows) if ($y < $this->numRows)
{ {
$this->cursor->y++; $y++;
} }
break; break;
case KeyType::PAGE_UP: case KeyType::PAGE_UP:
$this->cursor->y = ($this->cursor->y > $this->screenRows) $y = ($y > $this->screenRows)
? $this->cursor->y - $this->screenRows ? $y - $this->screenRows
: 0; : 0;
break; break;
case KeyType::PAGE_DOWN: case KeyType::PAGE_DOWN:
$this->cursor->y = ($this->cursor->y + $this->screenRows < $this->numRows) $y = ($y + $this->screenRows < $this->numRows)
? $this->cursor->y + $this->screenRows ? $y + $this->screenRows
: $this->numRows; : $this->numRows;
break; break;
case KeyType::HOME_KEY: case KeyType::HOME_KEY:
$this->cursor->x = 0; $x = 0;
break; break;
case KeyType::END_KEY: 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; break;
@ -842,10 +859,17 @@ class Editor {
// Do nothing // 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 public function processKeypress(): ?string

View File

@ -1,56 +0,0 @@
<?php declare(strict_types=1);
namespace Aviat\Kilo;
class Event {
use Traits\ConstList;
// ------------------------------------------------------------------------
// Valid Events
// ------------------------------------------------------------------------
public const INPUT_KEY = 'INPUT_KEY';
public const PAGE_CHANGE = 'PAGE_CHANGE';
public const MOVE_CURSOR = 'MOVE_CURSOR';
public const QUIT_ATTEMPT = 'QUIT_ATTEMPT';
// Mapping of events to handlers
private static array $subscribeMap = [];
public static function fire(string $eventName, mixed $value): void
{
static::validateEvent($eventName);
if (array_key_exists($eventName, static::$subscribeMap))
{
foreach (static::$subscribeMap[$eventName] as $fn)
{
$fn($value);
}
}
}
public static function on(string $eventName, callable $fn): void
{
static::validateEvent($eventName);
if ( ! array_key_exists($eventName, static::$subscribeMap))
{
static::$subscribeMap[$eventName] = [];
}
if ( ! in_array($fn, static::$subscribeMap[$eventName], TRUE))
{
static::$subscribeMap[$eventName][] = $fn;
}
}
private static function validateEvent(string $eventName): void
{
$validEvents = self::getConstList();
if ( ! array_key_exists($eventName, $validEvents))
{
throw new \InvalidArgumentException("Invalid event '{$eventName}'. Event const must exist in Aviat\\Kilo\\Event.");
}
}
}

View File

@ -5,13 +5,8 @@ namespace Aviat\Kilo;
class Position { class Position {
private function __construct(public int $x, public int $y) {} private function __construct(public int $x, public int $y) {}
public static function new(int $x, int $y): self public static function new(int $x = 0, int $y = 0): self
{ {
return new Position($x, $y); return new Position($x, $y);
} }
public static function default(): self
{
return new Position(0, 0);
}
} }

View File

@ -36,7 +36,9 @@ class Row {
return $self; return $self;
} }
private function __construct() {} private function __construct() {
// Private in favor of ::new static function
}
public function __get(string $name): mixed public function __get(string $name): mixed
{ {

View File

@ -1,27 +0,0 @@
<?php declare(strict_types=1);
namespace Aviat\Kilo\Tests;
use Aviat\Kilo\Event;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class EventTest extends TestCase {
public function testRequiresValidEvent(): void
{
$this->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);
}
}