Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
5 / 5
RawKeyCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 CTRL
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
1<?php declare(strict_types=1);
2
3namespace Aviat\Kilo\Enum;
4
5use Aviat\Kilo\Traits;
6use function Aviat\Kilo\ctrl_key;
7
8/**
9 * 'Raw' input from stdin
10 * @enum
11 */
12class RawKeyCode {
13    use Traits\ConstList;
14
15    public const ARROW_DOWN = "\e[B";
16    public const ARROW_LEFT = "\e[D";
17    public const ARROW_RIGHT = "\e[C";
18    public const ARROW_UP = "\e[A";
19    public const BACKSPACE = "\x7f";
20    public const BELL = "\a";
21    public const CARRIAGE_RETURN = "\r";
22    public const DELETE = "\e[3~";
23    public const EMPTY = '';
24    public const ENTER = "\r";
25    public const ESCAPE = "\e";
26    public const FORM_FEED = "\f";
27    public const NEWLINE = "\n";
28    public const NULL = "\0";
29    public const PAGE_DOWN = "\e[6~";
30    public const PAGE_UP = "\e[5~";
31    public const SPACE = ' ';
32    public const TAB = "\t";
33    public const VERTICAL_TAB = "\v";
34
35    /**
36     * Returns the ascii character for the specified
37     * ctrl + letter combo
38     *
39     * @param string $char
40     * @return string|null
41     */
42    public static function CTRL(string $char): ?string
43    {
44        $char = strtolower($char);
45        $ord = ord($char);
46
47        // a = 0x61
48        // z = 0x7a
49        // So, 0x60 < $ord < 0x7b
50        if ($ord > 0x60 && $ord < 0x7b)
51        {
52            return chr(ctrl_key($char));
53        }
54
55        // Invalid input, not an ascii letter
56        return NULL;
57    }
58}