Bugfixes in lineread, implemented command history

This commit is contained in:
Chris 2016-11-02 22:11:35 +01:00
parent 427cac578a
commit 46806e3d62
4 changed files with 36 additions and 21 deletions

View File

@ -1,14 +0,0 @@
<?php
namespace NoccyLabs\Shell\AutoComplete;
class Hinter implement HinterInterface
{
public function getHints()
{
}
public function addHinter(HinterInterface $hinter)
{
}
}

View File

@ -1,7 +0,0 @@
<?php
namespace NoccyLabs\Shell\AutoComplete;
interface HinterInterface
{
}

View File

@ -16,6 +16,8 @@ class LineRead
protected $history = []; protected $history = [];
protected $stashedBuffer = null;
protected $posHistory = 0; protected $posHistory = 0;
protected $posCursor = 0; protected $posCursor = 0;
@ -61,6 +63,11 @@ class LineRead
{ {
$prompt = $this->prompt; $prompt = $this->prompt;
$buffer = $this->buffer; $buffer = $this->buffer;
if ($this->posCursor > strlen($this->buffer)) {
$this->posCursor = strlen($this->buffer);
}
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll; $cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
$endStyle = "\e[0m"; $endStyle = "\e[0m";
@ -85,6 +92,10 @@ class LineRead
$returnBuffer = null; $returnBuffer = null;
while (strlen($keyBuffer)>0) { while (strlen($keyBuffer)>0) {
if ($keyBuffer[0] == "\e") { if ($keyBuffer[0] == "\e") {
if (strlen($keyBuffer)==1) {
$keyBuffer = "";
return "\e";
}
if ($keyBuffer[1] == "[") { if ($keyBuffer[1] == "[") {
$ctrlChar = substr($keyBuffer, 0,3); $ctrlChar = substr($keyBuffer, 0,3);
$keyBuffer = substr($keyBuffer, 3); $keyBuffer = substr($keyBuffer, 3);
@ -113,6 +124,7 @@ class LineRead
} }
} elseif ($keyCode == 13) { } elseif ($keyCode == 13) {
$returnBuffer = $this->buffer; $returnBuffer = $this->buffer;
array_unshift($this->history, $this->buffer);
$this->buffer = null; $this->buffer = null;
$this->posCursor = 0; $this->posCursor = 0;
printf("\n\r"); printf("\n\r");
@ -146,7 +158,25 @@ class LineRead
$this->redraw(); $this->redraw();
break; break;
case "\e[A": // up case "\e[A": // up
if ($this->posHistory == 0) {
$this->stashedBuffer = $this->buffer;
}
if ($this->posHistory < count($this->history)) {
$this->posHistory++;
$this->buffer = $this->history[$this->posHistory-1];
$this->redraw();
}
break;
case "\e[B": // down case "\e[B": // down
if ($this->posHistory > 1) {
$this->posHistory--;
$this->buffer = $this->history[$this->posHistory-1];
$this->redraw();
} elseif ($this->posHistory > 0) {
$this->posHistory--;
$this->buffer = $this->stashedBuffer;
$this->redraw();
}
break; break;
default: default:
fprintf(STDERR, "\n%s\n", substr($code,1)); fprintf(STDERR, "\n%s\n", substr($code,1));

View File

@ -300,8 +300,14 @@ class Shell
if (!($buffer = $this->lineReader->update())) { if (!($buffer = $this->lineReader->update())) {
usleep(10000); usleep(10000);
} }
// Escape is handy too...
if ($buffer == "\e") {
$this->dispatchEvent("shell.abort");
continue;
}
// we get a ^C on ^C, so deal with the ^C. // we get a ^C on ^C, so deal with the ^C.
if ($buffer == "\x03") { if ($buffer == "\x03") {
$this->dispatchEvent("shell.stop");
$this->stop(); $this->stop();
continue; continue;
} }