php-shell/lib/LineRead.php

137 lines
3.8 KiB
PHP

<?php
namespace NoccyLabs\Shell;
class LineRead
{
const STATE_IDLE = 0;
const STATE_READ = 1;
protected $state = self::STATE_IDLE;
protected $prompt = "cmd:>";
protected $buffer = null;
protected $history = [];
protected $posHistory = 0;
protected $posCursor = 0;
protected $posScroll = 0;
protected $termWidth = 0;
protected $sttyOld;
public function __construct()
{
stream_set_blocking(STDIN, false);
$this->sttyOld = exec('stty -g');
exec('stty raw -echo isig');
}
public function __destruct()
{
exec('stty {$this->sttyOld}');
}
public function update()
{
if ($this->state == self::STATE_IDLE) {
$this->state = self::STATE_READ;
$this->redraw();
}
$buffer = $this->handleInput();
return $buffer;
}
public function erase()
{
fprintf(STDOUT, "\r\e[0m\e[2K");
}
public function redraw()
{
$prompt = $this->prompt;
$buffer = $this->buffer;
$cursor = strlen($this->prompt) + 2 + $this->posCursor - $this->posScroll;
fprintf(STDOUT, "\r\e[2K\e[36;1;4m%s\e[37;24m %s\e[%dG\e[0m", $prompt, $buffer, $cursor);
}
protected function handleInput()
{
static $keyBuffer;
$readBuf = fread(STDIN, 32);
$keyBuffer .= $readBuf;
$update = false;
$returnBuffer = null;
while (strlen($keyBuffer)>0) {
if ($keyBuffer[0] == "\e") {
if ($keyBuffer[1] == "[") {
$ctrlChar = substr($keyBuffer, 0,3);
$keyBuffer = substr($keyBuffer, 3);
$this->parseControlCode($ctrlChar);
}
} else {
$keyChar = $keyBuffer[0];
$keyCode = ord($keyChar);
$keyBuffer = substr($keyBuffer, 1);
if (($keyCode >= 32) && ($keyCode < 127)) {
$edBuffer = substr($this->buffer, 0, $this->posCursor) . $keyChar . substr($this->buffer, $this->posCursor);
$this->posCursor++;
$this->buffer = $edBuffer;
$update = true;
} elseif ($keyCode == 127) {
if ($this->posCursor > 0) {
$this->posCursor--;
$edBuffer = substr($this->buffer, 0, $this->posCursor) . substr($this->buffer, $this->posCursor+1);
$this->buffer = $edBuffer;
$update = true;
}
} elseif ($keyCode == 13) {
$returnBuffer = $this->buffer;
$this->buffer = null;
$this->posCursor = 0;
printf("\n");
$this->state = self::STATE_IDLE;
}
}
}
if ($update) {
$this->redraw();
}
return $returnBuffer;
}
protected function parseControlCode($code)
{
switch ($code) {
case "\e[D":
$this->posCursor = max($this->posCursor-1,0);
$this->redraw();
break;
case "\e[C":
$this->posCursor = min($this->posCursor+1,strlen($this->buffer));
$this->redraw();
break;
case "\e[H":
$this->posCursor = 0;
$this->redraw();
break;
case "\e[F":
$this->posCursor = strlen($this->buffer);
$this->redraw();
break;
case "\e[A": // up
case "\e[B": // down
break;
default:
fprintf(STDERR, "\n%s\n", substr($code,1));
}
}
}