php-shell/lib/LineRead.php

147 lines
4.1 KiB
PHP
Raw Normal View History

2016-04-13 01:19:22 +00:00
<?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);
2016-04-25 18:47:30 +00:00
$this->sttyOld = trim(exec('stty -g'));
exec('stty raw -echo'); // isig');
2016-04-13 01:19:22 +00:00
}
public function __destruct()
{
2016-04-25 18:47:30 +00:00
exec('stty '.$this->sttyOld);
2016-04-13 01:19:22 +00:00
}
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;
2016-04-25 18:47:30 +00:00
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
fprintf(STDOUT, "\r\e[2K\e[32;1m%s\e[37m%s\e[%dG\e[0m", $prompt, $buffer, $cursor);
2016-04-13 01:19:22 +00:00
}
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);
}
2016-04-25 18:47:30 +00:00
} elseif ($keyBuffer[0]=="\x03") {
$this->posCursor = 0;
$this->buffer = null;
fprintf(STDOUT, "\r\n");
return $keyBuffer[0];
2016-04-13 01:19:22 +00:00
} 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;
2016-04-25 18:47:30 +00:00
printf("\n\r");
2016-04-13 01:19:22 +00:00
$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));
}
}
2016-04-25 18:47:30 +00:00
public function setPrompt($prompt)
{
$this->prompt = $prompt;
}
2016-04-13 01:19:22 +00:00
}