php-kilo/src/functions.php

47 lines
1.0 KiB
PHP
Raw Normal View History

2019-10-10 12:28:46 -04:00
<?php declare(strict_types=1);
namespace Kilo;
use FFI;
2019-10-10 15:49:01 -04:00
$ffi = FFI::load(__DIR__ . '/ffi.h');
$original_termios = $ffi->new('struct termios');
2019-10-10 12:28:46 -04:00
function enableRawMode(): void
{
global $ffi;
global $original_termios;
// Populate the original terminal settings
$ffi->tcgetattr(STDIN_FILENO, FFI::addr($original_termios));
2019-10-10 15:49:01 -04:00
$termios = $ffi->new('struct termios');
$termios->c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
$termios->c_oflag &= ~(OPOST);
$termios->c_cflag |= (CS8);
$termios->c_lflag &= ~(_ECHO | ICANON | IEXTEN | ISIG);
$termios->c_cc[VMIN] = 0;
$termios->c_cc[VTIME] = 1;
2019-10-10 12:28:46 -04:00
// Turn on raw mode
$ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($termios));
}
function disableRawMode(): void
{
global $ffi;
global $original_termios;
$ffi->tcsetattr(STDIN_FILENO, TCSAFLUSH, FFI::addr($original_termios));
}
2019-10-10 15:49:01 -04:00
function read_stdin(int $len = 128): string
{
$handle = fopen('php://stdin', 'rb');
$input = fread($handle, $len);
$input = rtrim($input);
2019-10-10 12:28:46 -04:00
fclose($handle);
return $input;
}