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

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": { "autoload": {
"files": [ "files": [
"src/constants.php", "src/Kilo.php"
"src/config.php",
"src/functions.php"
], ],
"psr-4": { "psr-4": {
"Aviat\\Kilo\\": "src/" "Aviat\\Kilo\\": "src/"

454
composer.lock generated

File diff suppressed because it is too large Load Diff

21
kilo
View File

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

View File

@ -695,7 +695,7 @@ class Editor {
$y--; $y--;
$x = $row->size - 1; $x = $row->size - 1;
} }
break; break;
case KeyType::ArrowRight: case KeyType::ArrowRight:
if ($x < $row->size) if ($x < $row->size)
@ -707,40 +707,40 @@ class Editor {
$y++; $y++;
$x = 0; $x = 0;
} }
break; break;
case KeyType::ArrowUp: case KeyType::ArrowUp:
if ($y !== 0) if ($y !== 0)
{ {
$y--; $y--;
} }
break; break;
case KeyType::ArrowDown: case KeyType::ArrowDown:
if ($y < $this->document->numRows) if ($y < $this->document->numRows)
{ {
$y++; $y++;
} }
break; break;
case KeyType::PageUp: case KeyType::PageUp:
$y = saturating_sub($y, $this->terminalSize->rows); $y = saturating_sub($y, $this->terminalSize->rows);
break; break;
case KeyType::PageDown: case KeyType::PageDown:
$y = saturating_add($y, $this->terminalSize->rows, $this->document->numRows); $y = saturating_add($y, $this->terminalSize->rows, $this->document->numRows);
break; break;
case KeyType::Home: case KeyType::Home:
$x = 0; $x = 0;
break; break;
case KeyType::End: case KeyType::End:
if ($y < $this->document->numRows) if ($y < $this->document->numRows)
{ {
$x = $row->size; $x = $row->size;
} }
break; break;
default: default:
// Do nothing // Do nothing

View File

@ -2,7 +2,51 @@
namespace Aviat\Kilo; 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 // ! C function/macro equivalents

View File

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