Make ANSI escape colors into constants

This commit is contained in:
Timothy Warren 2019-11-06 16:11:38 -05:00
parent ae5228a6f1
commit bb10b69e12
2 changed files with 59 additions and 13 deletions

46
src/Color.php Normal file
View File

@ -0,0 +1,46 @@
<?php declare(strict_types=1);
namespace Kilo;
/**
* ANSI Color escape sequences
*/
class Color {
// Foreground colors
public const FG_BLACK = 30;
public const FG_RED = 31;
public const FG_GREEN = 32;
public const FG_YELLOW = 33;
public const FG_BLUE = 34;
public const FG_MAGENTA = 35;
public const FG_CYAN = 36;
public const FG_WHITE = 37;
public const FG_BRIGHT_BLACK = 90;
public const FG_BRIGHT_RED = 91;
public const FG_BRIGHT_GREEN = 92;
public const FG_BRIGHT_YELLOW = 93;
public const FG_BRIGHT_BLUE = 94;
public const FG_BRIGHT_MAGENTA = 95;
public const FG_BRIGHT_CYAN = 96;
public const FG_BRIGHT_WHITE = 97;
// Background colors
public const BG_BLACK = 40;
public const BG_RED = 41;
public const BG_GREEN = 42;
public const BG_YELLOW = 43;
public const BG_BLUE = 44;
public const BG_MAGENTA = 45;
public const BG_CYAN = 46;
public const BG_WHITE = 47;
public const BG_BRIGHT_BLACK = 100;
public const BG_BRIGHT_RED = 101;
public const BG_BRIGHT_GREEN = 102;
public const BG_BRIGHT_YELLOW = 103;
public const BG_BRIGHT_BLUE = 104;
public const BG_BRIGHT_MAGENTA = 105;
public const BG_BRIGHT_CYAN = 106;
public const BG_BRIGHT_WHITE = 107;
public const INVERT = 7;
}

View File

@ -294,22 +294,22 @@ function array_replace_range(array &$array, int $offset, int $length, $value):vo
function syntax_to_color(int $hl): int function syntax_to_color(int $hl): int
{ {
$map = [ $map = [
Highlight::COMMENT => 36, // Foreground Cyan, Highlight::COMMENT => Color::FG_CYAN,
Highlight::ML_COMMENT => 90, // Foreground Bright Black Highlight::ML_COMMENT => Color::FG_BRIGHT_BLACK,
Highlight::KEYWORD1 => 33, // Foreground Yellow Highlight::KEYWORD1 => Color::FG_YELLOW,
Highlight::KEYWORD2 => 32, // Foreground Green Highlight::KEYWORD2 => Color::FG_GREEN,
Highlight::STRING => 35, // Foreground Magenta Highlight::STRING => Color::FG_MAGENTA,
Highlight::NUMBER => 31, // Foreground Red Highlight::NUMBER => Color::FG_RED,
Highlight::OPERATOR => 92, // Foreground Bright Green Highlight::OPERATOR => Color::FG_BRIGHT_GREEN,
Highlight::VARIABLE => 96, // Foreground Bright Cyan Highlight::VARIABLE => Color::FG_BRIGHT_CYAN,
Highlight::DELIMITER => 34, // Foreground Blue Highlight::DELIMITER => Color::FG_BLUE,
Highlight::INVALID => 101, // Background Bright Red Highlight::INVALID => Color::BG_BRIGHT_RED,
Highlight::MATCH => 7, // Reverse! Highlight::MATCH => Color::INVERT,
]; ];
return (array_key_exists($hl, $map)) return (array_key_exists($hl, $map))
? $map[$hl] ? $map[$hl]
: 37; // Foreground White : Color::FG_WHITE;
} }
/** /**
@ -350,7 +350,7 @@ function get_php_tokens(string $code): array
'type' => $type, 'type' => $type,
'typeName' => token_name($type), 'typeName' => token_name($type),
'char' => $char, 'char' => $char,
// 'line' => $currentLine, 'line' => $currentLine,
]; ];
if ($currentLine !== $lineNum) if ($currentLine !== $lineNum)