More consistent names for keys

This commit is contained in:
Timothy Warren 2021-04-09 13:52:01 -04:00
parent 35ec8f27ad
commit 644f27bb37
8 changed files with 67 additions and 61 deletions

View File

@ -2,7 +2,7 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\KeyCode;
use Aviat\Kilo\Enum\RawKeyCode;
use Aviat\Kilo\Enum\KeyType;
use Aviat\Kilo\Tokens\PHP8;
use Aviat\Kilo\Type\Point;
@ -102,7 +102,7 @@ class Document {
return;
}
if ($c === KeyType::ENTER || $c === KeyCode::CARRIAGE_RETURN)
if ($c === KeyType::ENTER || $c === RawKeyCode::CARRIAGE_RETURN)
{
$this->insertNewline($at);
return;

View File

@ -5,7 +5,7 @@ namespace Aviat\Kilo;
use Aviat\Kilo\Type\TerminalSize;
use Aviat\Kilo\Enum\{
Color,
KeyCode,
RawKeyCode,
KeyType,
Highlight,
SearchDirection
@ -163,7 +163,7 @@ class Editor {
$rx = 0;
for ($i = 0; $i < $cx; $i++)
{
if ($row->chars[$i] === KeyCode::TAB)
if ($row->chars[$i] === RawKeyCode::TAB)
{
$rx += (KILO_TAB_STOP - 1) - ($rx % KILO_TAB_STOP);
}
@ -185,7 +185,7 @@ class Editor {
$cur_rx = 0;
for ($cx = 0; $cx < $row->size; $cx++)
{
if ($row->chars[$cx] === KeyCode::TAB)
if ($row->chars[$cx] === RawKeyCode::TAB)
{
$cur_rx += (KILO_TAB_STOP - 1) - ($cur_rx % KILO_TAB_STOP);
}
@ -266,7 +266,7 @@ class Editor {
$lastMatch = NO_MATCH;
$direction = SearchDirection::FORWARD;
if ($key === KeyCode::ENTER || $key === KeyCode::ESCAPE)
if ($key === RawKeyCode::ENTER || $key === RawKeyCode::ESCAPE)
{
return;
}
@ -577,17 +577,17 @@ class Editor {
$c = Terminal::readKey();
$isModifier = in_array($c, $modifiers, TRUE);
if ($c === KeyType::ESCAPE || ($c === KeyCode::ENTER && $buffer !== ''))
if ($c === KeyType::ESCAPE || ($c === RawKeyCode::ENTER && $buffer !== ''))
{
$this->setStatusMessage('');
if ($callback !== NULL)
{
$callback($buffer, $c);
}
return ($c === KeyCode::ENTER) ? $buffer : '';
return ($c === RawKeyCode::ENTER) ? $buffer : '';
}
if ($c === KeyType::DEL_KEY || $c === KeyType::BACKSPACE)
if ($c === KeyType::DELETE || $c === KeyType::BACKSPACE)
{
$buffer = substr($buffer, 0, -1);
}
@ -610,35 +610,28 @@ class Editor {
{
$c = Terminal::readKey();
if ($c === KeyCode::NULL || $c === KeyCode::EMPTY)
if ($c === RawKeyCode::NULL || $c === RawKeyCode::EMPTY)
{
return;
}
switch ($c)
{
case KeyCode::CTRL('q'):
case RawKeyCode::CTRL('q'):
$this->quitAttempt();
return;
case KeyCode::CTRL('s'):
case RawKeyCode::CTRL('s'):
$this->save();
break;
case KeyCode::CTRL('f'):
case RawKeyCode::CTRL('f'):
$this->find();
break;
case KeyType::DEL_KEY:
$this->document->delete($this->cursor);
break;
case KeyType::DELETE:
case KeyType::BACKSPACE:
if ($this->cursor->x > 0 || $this->cursor->y > 0)
{
$this->moveCursor(KeyType::ARROW_LEFT);
$this->document->delete($this->cursor);
}
$this->removeChar($c);
break;
case KeyType::ARROW_UP:
@ -647,12 +640,12 @@ class Editor {
case KeyType::ARROW_RIGHT:
case KeyType::PAGE_UP:
case KeyType::PAGE_DOWN:
case KeyType::HOME_KEY:
case KeyType::END_KEY:
case KeyType::HOME:
case KeyType::END:
$this->moveCursor($c);
break;
case KeyCode::CTRL('l'):
case RawKeyCode::CTRL('l'):
case KeyType::ESCAPE:
// Do nothing
break;
@ -737,11 +730,11 @@ class Editor {
: $this->document->numRows;
break;
case KeyType::HOME_KEY:
case KeyType::HOME:
$x = 0;
break;
case KeyType::END_KEY:
case KeyType::END:
if ($y < $this->document->numRows)
{
$x = $row->size;
@ -773,6 +766,19 @@ class Editor {
$this->moveCursor(KeyType::ARROW_RIGHT);
}
protected function removeChar(string $ch): void
{
if ($ch === KeyType::DELETE)
{
$this->document->delete($this->cursor);
}
else if ($ch === KeyType::BACKSPACE && ($this->cursor->x > 0 || $this->cursor->y > 0))
{
$this->document->delete($this->cursor);
$this->moveCursor(KeyType::ARROW_LEFT);
}
}
protected function quitAttempt(): void
{
if ($this->document->isDirty() && $this->quitTimes > 0)

View File

@ -16,11 +16,11 @@ class KeyType {
public const ARROW_RIGHT = 'KEY_ARROW_RIGHT';
public const ARROW_UP = 'KEY_ARROW_UP';
public const BACKSPACE = 'KEY_BACKSPACE';
public const DEL_KEY = 'KEY_DELETE';
public const END_KEY = 'KEY_END';
public const DELETE = 'KEY_DELETE';
public const END = 'KEY_END';
public const ENTER = 'KEY_ENTER';
public const ESCAPE = 'KEY_ESCAPE';
public const HOME_KEY = 'KEY_HOME';
public const HOME = 'KEY_HOME';
public const PAGE_DOWN = 'KEY_PAGE_DOWN';
public const PAGE_UP = 'KEY_PAGE_UP';
}

View File

@ -9,7 +9,7 @@ use function Aviat\Kilo\ctrl_key;
* 'Raw' input from stdin
* @enum
*/
class KeyCode {
class RawKeyCode {
use Traits\ConstList;
public const ARROW_DOWN = "\e[B";
@ -19,7 +19,7 @@ class KeyCode {
public const BACKSPACE = "\x7f";
public const BELL = "\a";
public const CARRIAGE_RETURN = "\r";
public const DEL_KEY = "\e[3~";
public const DELETE = "\e[3~";
public const EMPTY = '';
public const ENTER = "\r";
public const ESCAPE = "\e";

View File

@ -3,7 +3,7 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\Highlight;
use Aviat\Kilo\Enum\KeyCode;
use Aviat\Kilo\Enum\RawKeyCode;
/**
* @property-read int $size
@ -305,7 +305,7 @@ class Row {
$klen = strlen($k);
$nextCharOffset = $i + $klen;
$isEndOfLine = $nextCharOffset >= $this->rsize;
$nextChar = ($isEndOfLine) ? KeyCode::NULL : $this->render[$nextCharOffset];
$nextChar = ($isEndOfLine) ? RawKeyCode::NULL : $this->render[$nextCharOffset];
if (substr($this->render, $i, $klen) === $k && is_separator($nextChar))
{

View File

@ -2,7 +2,7 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\KeyCode;
use Aviat\Kilo\Enum\RawKeyCode;
use Aviat\Kilo\Enum\KeyType;
use Aviat\Kilo\Type\TerminalSize;
@ -90,26 +90,26 @@ class Terminal {
return match($c)
{
// Unambiguous mappings
KeyCode::ARROW_DOWN => KeyType::ARROW_DOWN,
KeyCode::ARROW_LEFT => KeyType::ARROW_LEFT,
KeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT,
KeyCode::ARROW_UP => KeyType::ARROW_UP,
KeyCode::DEL_KEY => KeyType::DEL_KEY,
KeyCode::ENTER => KeyType::ENTER,
KeyCode::PAGE_DOWN => KeyType::PAGE_DOWN,
KeyCode::PAGE_UP => KeyType::PAGE_UP,
RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN,
RawKeyCode::ARROW_LEFT => KeyType::ARROW_LEFT,
RawKeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT,
RawKeyCode::ARROW_UP => KeyType::ARROW_UP,
RawKeyCode::DELETE => KeyType::DELETE,
RawKeyCode::ENTER => KeyType::ENTER,
RawKeyCode::PAGE_DOWN => KeyType::PAGE_DOWN,
RawKeyCode::PAGE_UP => KeyType::PAGE_UP,
// Backspace
KeyCode::CTRL('h'), KeyCode::BACKSPACE => KeyType::BACKSPACE,
RawKeyCode::CTRL('h'), RawKeyCode::BACKSPACE => KeyType::BACKSPACE,
// Escape
KeyCode::CTRL('l'), KeyCode::ESCAPE => KeyType::ESCAPE,
RawKeyCode::CTRL('l'), RawKeyCode::ESCAPE => KeyType::ESCAPE,
// Home Key
"\eOH", "\e[7~", "\e[1~", ANSI::RESET_CURSOR => KeyType::HOME_KEY,
"\eOH", "\e[7~", "\e[1~", ANSI::RESET_CURSOR => KeyType::HOME,
// End Key
"\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END_KEY,
"\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END,
default => $c,
};

View File

@ -2,7 +2,7 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\{Color, Highlight, KeyCode};
use Aviat\Kilo\Enum\{Color, Highlight, RawKeyCode};
// ----------------------------------------------------------------------------
// ! C function/macro equivalents
@ -77,12 +77,12 @@ function is_digit(string $char): bool
function is_space(string $char): bool
{
return match($char) {
KeyCode::CARRIAGE_RETURN,
KeyCode::FORM_FEED,
KeyCode::NEWLINE,
KeyCode::SPACE,
KeyCode::TAB,
KeyCode::VERTICAL_TAB => true,
RawKeyCode::CARRIAGE_RETURN,
RawKeyCode::FORM_FEED,
RawKeyCode::NEWLINE,
RawKeyCode::SPACE,
RawKeyCode::TAB,
RawKeyCode::VERTICAL_TAB => true,
default => false,
};
@ -107,7 +107,7 @@ function is_separator(string $char): bool
$isSep = str_contains(',.()+-/*=~%<>[];', $char);
return is_space($char) || $char === KeyCode::NULL || $isSep;
return is_space($char) || $char === RawKeyCode::NULL || $isSep;
}
/**
@ -185,7 +185,7 @@ function syntax_to_color(int $hl): int
*/
function tabs_to_spaces(string $str, int $number = KILO_TAB_STOP): string
{
return str_replace(KeyCode::TAB, str_repeat(KeyCode::SPACE, $number), $str);
return str_replace(RawKeyCode::TAB, str_repeat(RawKeyCode::SPACE, $number), $str);
}
function error_code_name(int $code): string

View File

@ -4,7 +4,7 @@ namespace Aviat\Kilo\Tests\Enum;
use function Aviat\Kilo\ctrl_key;
use Aviat\Kilo\Enum\KeyCode;
use Aviat\Kilo\Enum\RawKeyCode;
use PHPUnit\Framework\TestCase;
class KeyCodeTest extends TestCase {
@ -16,7 +16,7 @@ class KeyCodeTest extends TestCase {
$ord = $i;
$expected = chr($ord);
$actual = KeyCode::CTRL($char);
$actual = RawKeyCode::CTRL($char);
$this->assertEquals(ctrl_key($char), $ord, 'chr(ctrl_key) !== CTRL');
$this->assertEquals($expected, $actual, "CTRL+'{$char}' should return chr($ord)");
@ -25,13 +25,13 @@ class KeyCodeTest extends TestCase {
public function testNullOnInvalidChar(): void
{
$this->assertNull(KeyCode::CTRL("\t"));
$this->assertNull(RawKeyCode::CTRL("\t"));
}
public function testSameOutputOnUpperOrLower(): void
{
$lower = KeyCode::CTRL('v');
$upper = KeyCode::CTRL('V');
$lower = RawKeyCode::CTRL('v');
$upper = RawKeyCode::CTRL('V');
$this->assertEquals($lower, $upper);
}