php-kilo/src/Enum/RawKeyCode.php

59 lines
1.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Aviat\Kilo\Enum;
use Aviat\Kilo\Traits;
2020-02-05 16:32:17 -05:00
use function Aviat\Kilo\ctrl_key;
2020-02-05 14:50:31 -05:00
/**
* 'Raw' input from stdin
2021-03-04 12:03:51 -05:00
* @enum
2020-02-05 14:50:31 -05:00
*/
2021-04-09 13:52:01 -04:00
class RawKeyCode {
use Traits\ConstList;
public const ARROW_DOWN = "\e[B";
public const ARROW_LEFT = "\e[D";
public const ARROW_RIGHT = "\e[C";
public const ARROW_UP = "\e[A";
public const BACKSPACE = "\x7f";
2021-03-17 15:38:52 -04:00
public const BELL = "\a";
2020-02-05 14:50:31 -05:00
public const CARRIAGE_RETURN = "\r";
2021-04-09 13:52:01 -04:00
public const DELETE = "\e[3~";
2020-02-05 14:50:31 -05:00
public const EMPTY = '';
public const ENTER = "\r";
public const ESCAPE = "\e";
2020-02-05 14:50:31 -05:00
public const FORM_FEED = "\f";
public const NEWLINE = "\n";
public const NULL = "\0";
public const PAGE_DOWN = "\e[6~";
public const PAGE_UP = "\e[5~";
2020-02-05 14:50:31 -05:00
public const SPACE = ' ';
public const TAB = "\t";
public const VERTICAL_TAB = "\v";
2020-02-05 16:32:17 -05:00
/**
* Returns the ascii character for the specified
* ctrl + letter combo
*
* @param string $char
2021-03-04 12:03:51 -05:00
* @return string|null
2020-02-05 16:32:17 -05:00
*/
public static function CTRL(string $char): ?string
{
$char = strtolower($char);
$ord = ord($char);
// a = 0x61
// z = 0x7a
// So, 0x60 < $ord < 0x7b
if ($ord > 0x60 && $ord < 0x7b)
{
return chr(ctrl_key($char));
}
// Invalid input, not an ascii letter
return NULL;
}
}