Start to add 256 color rendering support
All checks were successful
timw4mail/php-kilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2022-08-26 15:44:00 -04:00
parent 3b4ed0f245
commit 048f2c5f38
8 changed files with 2682 additions and 2244 deletions

View File

@ -2,6 +2,9 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\Color;
use Aviat\Kilo\Enum\Color256;
/**
* ANSI
*/
@ -58,14 +61,48 @@ class ANSI {
/**
* Generate the ascii sequence for basic foreground text color
*
* @param Enum\Color $color
* @param Color|Color256|int $color
* @param Color $ground
* @return string
*/
public static function color(Enum\Color $color): string
public static function color(Enum\Color | Enum\Color256 | int $color, Enum\Color $ground = Enum\Color::Fg): string
{
if ( ! $color instanceof Enum\Color)
{
return self::color256($color, $ground);
}
return self::escapeSequence('%dm', $color->value);
}
/**
* Generate the ANSI sequence for a 256 mode color
*
* @param Color256 | int $color
* @param Color $ground
* @return string
*/
public static function color256(Enum\Color256 | int $color, Enum\Color $ground = Enum\Color::Fg): string
{
if ($color instanceof Enum\Color256)
{
$color = $color->value;
}
return self::escapeSequence('%d;5;%dm', $ground->value, $color);
}
/**
* Invert the foreground/background on a text segment
*
* @param string $text
* @return string
*/
public static function invert(string $text): string
{
return self::INVERSE_TEXT . $text . self::RESET_TEXT;
}
/**
* Generate a sequence for an rgb color
*

View File

@ -5,6 +5,7 @@ namespace Aviat\Kilo;
use Aviat\Kilo\Type\TerminalSize;
use Aviat\Kilo\Enum\{
Color,
Color256,
RawKeyCode,
KeyType,
Highlight,
@ -429,9 +430,7 @@ class Editor {
$sym = (ord($ch) <= 26)
? chr(ord('@') + ord($ch))
: '?';
$this->outputBuffer .= ANSI::color(Color::INVERT);
$this->outputBuffer .= $sym;
$this->outputBuffer .= ANSI::RESET_TEXT;
$this->outputBuffer .= ANSI::invert($sym);
if ($currentColor !== -1)
{
$this->outputBuffer .= ANSI::color($currentColor);
@ -496,7 +495,7 @@ class Editor {
protected function drawStatusBar(): void
{
$this->outputBuffer .= ANSI::color(Color::INVERT);
$this->outputBuffer .= ANSI::INVERSE_TEXT;
$statusFilename = $this->document->filename !== '' ? $this->document->filename : '[No Name]';
$syntaxType = $this->document->fileType->name;

View File

@ -11,6 +11,10 @@ use Aviat\Kilo\Traits;
enum Color: int {
use Traits\ConstList;
// Foreground/Background
case Fg = 38;
case Bg = 48;
// Foreground colors
case FG_BLACK = 30;
case FG_RED = 31;

31
src/Enum/Color256.php Normal file
View File

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
namespace Aviat\Kilo\Enum;
use Aviat\Kilo\Traits;
/**
* ANSI 256 Color escape sequences
* @enum
*/
enum Color256: int {
use Traits\ConstList;
// Base colors for 256-color setup
case Black = 0;
case Red = 1;
case Green = 2;
case Yellow = 3;
case Blue = 4;
case Magenta = 5;
case Cyan = 6;
case White = 7;
case BrightBlack = 8;
case BrightRed = 9;
case BrightGreen = 10;
case BrightYellow = 11;
case BrightBlue = 12;
case BrightMagenta = 13;
case BrightCyan = 14;
case BrightWhite = 15;
}

View File

@ -25,6 +25,7 @@ enum Highlight implements JsonSerializable {
case SearchMatch;
case Identifier;
case Character;
case Embed;
/**
* Map a PHP syntax token to its associated highlighting type
@ -37,6 +38,7 @@ enum Highlight implements JsonSerializable {
return match($token) {
// Delimiters
T_ARRAY,
T_ATTRIBUTE,
T_CURLY_OPEN,
T_DOLLAR_OPEN_CURLY_BRACES,
T_OPEN_TAG,
@ -66,6 +68,8 @@ enum Highlight implements JsonSerializable {
// Operators
T_AS,
T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG,
T_AND_EQUAL,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
@ -185,6 +189,11 @@ enum Highlight implements JsonSerializable {
// Invalid syntax
T_BAD_CHARACTER => Highlight::Invalid,
// Comments
T_DOC_COMMENT => Highlight::MultiLineComment,
T_INLINE_HTML => Highlight::Embed,
default => Highlight::Normal,
};
}

View File

@ -3,15 +3,16 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\Color;
use Aviat\Kilo\Enum\Color256;
use Aviat\Kilo\Enum\Highlight;
/**
* Configure syntax highlighting colors
*
* @param Highlight $hl
* @return Color
* @return Color | Color256 | int
*/
function get_syntax_color(Highlight $hl): Color {
function get_syntax_color(Highlight $hl): Color | Color256 | int {
return match ($hl)
{
Highlight::Comment => Color::FG_CYAN,

View File

@ -17,6 +17,7 @@ abstract class Foo implements Ifoo {
protected function doNothing(): void {}
}
#[Attribute]
class Test {
public function __construct(public string $foo, public string $bar) {}
}
@ -30,9 +31,12 @@ class FooBar extends Foo implements Ifoo {
$cstr = print_r($c, TRUE);
$d();
return "{$a}, ${b}, " . $cstr;
$r = $this->operations($a, (int)$b);
return "{$a}, ${b}, " . $cstr . " = {$r}";
}
#[Test('a', 'b')]
private function operations(int $a, int $b): int
{
$this?->x?->bar();

File diff suppressed because it is too large Load Diff