php-kilo/src/functions.php

180 lines
3.6 KiB
PHP

<?php declare(strict_types=1);
namespace Kilo;
use FFI;
$ffi = FFI::load(__DIR__ . '/ffi.h');
$original_termios = $ffi->new('struct termios');
// ----------------------------------------------------------------------------
// ! Raw mode / Terminal size
// ----------------------------------------------------------------------------
function enable_raw_mode(): void
{
global $ffi;
global $original_termios;
register_shutdown_function('Kilo\disable_raw_mode');
// Populate the original terminal settings
$res = $ffi->tcgetattr(STDIN_FILENO, FFI::addr($original_termios));
if ($res === -1)
{
die('tcgetattr');
}
// So, the only thing that seems to really matter here is that c_oflag is 0...
$termios = clone $original_termios;
$termios->c_iflag = 0; //$termios->c_iflag & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
$termios->c_oflag = 0; // $termios->c_oflag && ~(OPOST);
$termios->c_cflag |= (CS8);
$termios->c_lflag = $termios->c_lflag & ~(_ECHO | ICANON | IEXTEN | ISIG);
$termios->c_cc[VMIN] = 0;
$termios->c_cc[VTIME] = 1;
// Turn on raw mode
$res = $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($termios));
if ($res === -1)
{
die('tcsetattr');
}
}
function disable_raw_mode(): void
{
global $ffi;
global $original_termios;
$res = $ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($original_termios));
echo "\n";
if ($res === -1)
{
die('tcsetattr');
}
}
/**
* @TODO fix
*/
function get_cursor_position()
{
write_stdout("\x1b[999C\x1b[999B");
write_stdout("\x1b[6n");
$rows = 0;
$cols = 0;
$buffer = read_stdout();
$res = sscanf($buffer, '\x1b[%d;%dR', $rows, $cols);
if ($res === -1 || $buffer[0] !== '\x1b' || $buffer[1] !== '[')
{
die('Failed to get screen size');
}
return [$rows, $cols];
}
function get_window_size()
{
global $ffi;
$ws = $ffi->new('struct winsize');
$res = $ffi->ioctl(STDOUT_FILENO, TIOCGWINSZ, FFI::addr($ws));
if ($res === -1 || $ws->ws_col === 0)
{
return get_cursor_position();
}
return [$ws->ws_row, $ws->ws_col];
}
// ----------------------------------------------------------------------------
// ! C function/macro equivalents
// ----------------------------------------------------------------------------
/**
* Do bit twiddling to convert a letter into
* its Ctrl-letter equivalent ordinal ascii value
*
* @param string $char
* @return int
*/
function ctrl_key(string $char): int
{
if ( ! is_ascii($char))
{
return -1;
}
// b1,100,001 (a) & b0,011,111 = b0,000,001
// b1,100,010 (b) & b0,011,111 = b0,000,010
return ord($char) & 0x1f;
}
function is_ascii(string $single_char): bool
{
if (strlen($single_char) > 1)
{
return FALSE;
}
return ord($single_char) < 0x80;
}
function is_cntrl(string $char): bool
{
$c = ord($char);
return is_ascii($char) && ( $c < 0x20 || $c === 0x7f );
}
function is_digit(string $char): bool
{
$c = ord($char);
return is_ascii($char) && ( $c > 0x2f && $c < 0x3a );
}
// ----------------------------------------------------------------------------
// ! Helper functions
// ----------------------------------------------------------------------------
function read_stdin(int $len = 128): string
{
$handle = fopen('php://stdin', 'rb');
$input = fread($handle, $len);
fclose($handle);
return $input;
}
function write_stdout(string $str, int $len = NULL): int
{
$handle = fopen('php://stdout', 'ab');
$res = (is_int($len))
? fwrite($handle, $str, $len)
: fwrite($handle, $str);
fclose($handle);
return $res;
}
function read_stdout(int $len = 128): string
{
$handle = fopen('php://stdout', 'rb');
$input = fread($handle, $len);
$input = rtrim($input);
fclose($handle);
return $input;
}