Minor code tweaks, consolidate non-autoloaded code into one file
Some checks failed
timw4mail/php-kilo/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2023-10-10 10:25:38 -04:00
parent dd4b707d12
commit fd478b697f
8 changed files with 320 additions and 270 deletions

View File

@ -1,9 +1,7 @@
{
"autoload": {
"files": [
"src/constants.php",
"src/config.php",
"src/functions.php"
"src/Kilo.php"
],
"psr-4": {
"Aviat\\Kilo\\": "src/"

454
composer.lock generated

File diff suppressed because it is too large Load Diff

19
kilo
View File

@ -3,13 +3,11 @@
namespace Aviat\Kilo;
require_once __DIR__ . '/src/constants.php';
require_once __DIR__ . '/src/config.php';
require_once __DIR__ . '/src/functions.php';
require_once __DIR__ . '/src/Kilo.php';
// Remove the composer install requirement by
// manually handling autoloading
spl_autoload_register(function (string $class) {
spl_autoload_register(static function (string $class): void {
$nsParts = explode('\\', $class);
array_shift($nsParts);
array_shift($nsParts);
@ -20,20 +18,17 @@ spl_autoload_register(function (string $class) {
if (file_exists($file))
{
require_once($file);
return true;
return;
}
return false;
});
// Log notices/errors/warnings to file
set_error_handler(static function (
$errno,
$errstr,
$errfile,
$errline
int $errno,
string $errstr,
string $errfile,
int $errline,
) {
$msg = print_r([
'code' => error_code_name($errno),

View File

@ -2,7 +2,51 @@
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\{Color, Highlight, RawKeyCode};
use Aviat\Kilo\Enum\Color;
use Aviat\Kilo\Enum\Color256;
use Aviat\Kilo\Enum\Highlight;
use Aviat\Kilo\Enum\RawKeyCode;
// -----------------------------------------------------------------------------
// ! App Constants
// -----------------------------------------------------------------------------
const KILO_VERSION = '0.3.0';
const KILO_TAB_STOP = 4;
const KILO_QUIT_TIMES = 3;
const NO_MATCH = -1;
const T_RAW = -1;
// -----------------------------------------------------------------------------
// ! App Config
// -----------------------------------------------------------------------------
/**
* Configure syntax highlighting colors
*
* @param Highlight $hl
* @return Color | Color256 | int
*/
function get_syntax_color(Highlight $hl): Color | Color256 | int {
return match ($hl)
{
Highlight::Comment => Color::FG_CYAN,
Highlight::MultiLineComment => Color::FG_BRIGHT_BLACK,
Highlight::Keyword1 => Color::FG_YELLOW,
Highlight::Keyword2 => Color::FG_GREEN,
Highlight::String => Color::FG_MAGENTA,
Highlight::Character => Color::FG_BRIGHT_MAGENTA,
Highlight::Number => Color::FG_BRIGHT_RED,
Highlight::Operator => Color::FG_BRIGHT_GREEN,
Highlight::Variable => Color::FG_BRIGHT_CYAN,
Highlight::Delimiter => Color::FG_BLUE,
Highlight::Invalid => Color::BG_BRIGHT_RED,
Highlight::SearchMatch => Color::INVERT,
Highlight::Identifier => Color::FG_BRIGHT_WHITE,
default => Color::FG_WHITE,
};
}
// ----------------------------------------------------------------------------
// ! C function/macro equivalents

View File

@ -58,7 +58,7 @@ class Termios {
$termios = clone $instance->originalTermios;
$termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON);
$termios->c_oflag = 0; // &= ~(C::OPOST);
$termios->c_oflag &= ~(C::OPOST);
$termios->c_cflag |= (C::CS8);
$termios->c_lflag &= ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG );
$termios->c_cc[C::VMIN] = 0;
@ -95,7 +95,6 @@ class Termios {
/**
* Get the size of the current terminal window
*
* @codeCoverageIgnore
* @return array|null
*/
public static function getWindowSize(): ?array

View File

@ -1,33 +0,0 @@
<?php declare(strict_types=1);
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 | Color256 | int
*/
function get_syntax_color(Highlight $hl): Color | Color256 | int {
return match ($hl)
{
Highlight::Comment => Color::FG_CYAN,
Highlight::MultiLineComment => Color::FG_BRIGHT_BLACK,
Highlight::Keyword1 => Color::FG_YELLOW,
Highlight::Keyword2 => Color::FG_GREEN,
Highlight::String => Color::FG_MAGENTA,
Highlight::Character => Color::FG_BRIGHT_MAGENTA,
Highlight::Number => Color::FG_BRIGHT_RED,
Highlight::Operator => Color::FG_BRIGHT_GREEN,
Highlight::Variable => Color::FG_BRIGHT_CYAN,
Highlight::Delimiter => Color::FG_BLUE,
Highlight::Invalid => Color::BG_BRIGHT_RED,
Highlight::SearchMatch => Color::INVERT,
Highlight::Identifier => Color::FG_BRIGHT_WHITE,
default => Color::FG_WHITE,
};
}

View File

@ -1,13 +0,0 @@
<?php declare(strict_types=1);
namespace Aviat\Kilo;
// -----------------------------------------------------------------------------
// ! App Constants
// -----------------------------------------------------------------------------
const KILO_VERSION = '0.3.0';
const KILO_TAB_STOP = 4;
const KILO_QUIT_TIMES = 3;
const NO_MATCH = -1;
const T_RAW = -1;