Files
jsonedit/src/Terminal/Terminal.php
T

161 lines
4.8 KiB
PHP
Raw Normal View History

2024-10-01 18:46:03 +02:00
<?php
namespace NoccyLabs\JsonEdit\Terminal;
2024-10-01 18:46:03 +02:00
class Terminal
{
private static int $init = 0;
private ?string $oldStty = null;
private int $lines = 0;
private int $columns = 0;
private bool $active = false;
2024-10-04 10:51:59 +02:00
public bool $resized = false;
2024-10-01 18:46:03 +02:00
public function __construct()
{
if (self::$init++ == 0) {
$this->setup();
}
}
public function __destruct()
{
if (--self::$init == 0) {
$this->shutdown();
}
}
private function setup(): void
{
$this->oldStty = exec("stty -g");
exec("stty raw -echo");
//ob_start();
echo "\e[s"; // save cursor pos
echo "\e[?1047h"; // alt buffer
echo "\e[H\e[0m\e[2J"; // clear screen
echo "\e[?7l"; // autowrap
//ob_flush();
$this->measureTerminal();
2024-10-04 10:51:59 +02:00
pcntl_signal(SIGWINCH, function () {
$this->resized = true;
$this->measureTerminal();
});
pcntl_async_signals(true);
2024-10-01 18:46:03 +02:00
$this->active = true;
}
private function measureTerminal(): void
{
$this->lines = intval(exec("tput lines"));
$this->columns = intval(exec("tput cols"));
}
public function shutdown(): void
{
//ob_end_clean();
if (!$this->active) return;
exec("stty {$this->oldStty}");
echo "\e[?1047l"; // main buffer
echo "\e[u\e[?25h"; // restore cursor pos and visibility
echo "\e[?7h"; // autowrap
$this->active = false;
}
public function getSize(): array
{
return [ $this->columns, $this->lines ];
}
2024-10-01 22:31:51 +02:00
public function setCursor(int $column, int $row, bool $visible = false): self
2024-10-01 18:46:03 +02:00
{
if ($row > 0 && $column > 0)
printf("\e[%d;%dH", $row, $column);
echo "\e[?25".($visible?"h":"l");
2024-10-01 22:31:51 +02:00
return $this;
}
public function writeAt(int $column, int $row, string $text, bool $visible = false): self
{
$this->setCursor($column, $row, $visible);
echo $text;
return $this;
2024-10-01 18:46:03 +02:00
}
private string $inputBuffer = '';
public function readKey(): ?string
{
$r = [ STDIN ]; $w = $e = [];
if (stream_select($r, $w, $e, 0)) {
$read = fread(STDIN, 64);
$this->inputBuffer .= $read;
}
return $this->parseInputBuffer();
}
private function parseInputBuffer(): ?string
{
if (str_starts_with($this->inputBuffer, "\e")) {
if (strncmp($this->inputBuffer, "\e[A", 3) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 3);
return "k{UP}";
} elseif (strncmp($this->inputBuffer, "\e[B", 3) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 3);
return "k{DOWN}";
} elseif (strncmp($this->inputBuffer, "\e[C", 3) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 3);
return "k{RIGHT}";
2024-10-03 18:26:36 +02:00
} elseif (strncmp($this->inputBuffer, "\e[1;5C", 6) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 6);
return "k{CRIGHT}";
2024-10-01 18:46:03 +02:00
} elseif (strncmp($this->inputBuffer, "\e[D", 3) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 3);
return "k{LEFT}";
2024-10-03 18:26:36 +02:00
} elseif (strncmp($this->inputBuffer, "\e[1;5D", 6) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 6);
return "k{CLEFT}";
2024-10-03 17:57:41 +02:00
} elseif (strncmp($this->inputBuffer, "\e[H", 3) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 3);
return "k{HOME}";
} elseif (strncmp($this->inputBuffer, "\e[F", 3) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 3);
return "k{END}";
2024-10-01 18:46:03 +02:00
} elseif (strncmp($this->inputBuffer, "\e[5~", 4) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 4);
return "k{PGUP}";
} elseif (strncmp($this->inputBuffer, "\e[6~", 4) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 4);
return "k{PGDN}";
} elseif (strncmp($this->inputBuffer, "\e\e", 2) === 0) {
$this->inputBuffer = mb_substr($this->inputBuffer, 2);
return chr(27);
}
$this->inputBuffer = mb_substr($this->inputBuffer, 1);
}
if (mb_strlen($this->inputBuffer) > 0) {
$ch = mb_substr($this->inputBuffer, 0, 1);
// if (ord($ch) == 3) {
// return "k{^C}";
// }
$this->inputBuffer = mb_substr($this->inputBuffer, 1);
return $ch;
}
return null;
}
}