Bugfixes in lineread, implemented command history
This commit is contained in:
		@@ -1,14 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\Shell\AutoComplete;
 | 
			
		||||
 | 
			
		||||
class Hinter implement HinterInterface
 | 
			
		||||
{
 | 
			
		||||
    public function getHints()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function addHinter(HinterInterface $hinter)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\Shell\AutoComplete;
 | 
			
		||||
 | 
			
		||||
interface HinterInterface
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
@@ -15,6 +15,8 @@ class LineRead
 | 
			
		||||
    protected $buffer = null;
 | 
			
		||||
 | 
			
		||||
    protected $history = [];
 | 
			
		||||
 | 
			
		||||
    protected $stashedBuffer = null;
 | 
			
		||||
    
 | 
			
		||||
    protected $posHistory = 0;
 | 
			
		||||
    
 | 
			
		||||
@@ -61,6 +63,11 @@ class LineRead
 | 
			
		||||
    {
 | 
			
		||||
        $prompt = $this->prompt;
 | 
			
		||||
        $buffer = $this->buffer;
 | 
			
		||||
 | 
			
		||||
        if ($this->posCursor > strlen($this->buffer)) {
 | 
			
		||||
            $this->posCursor = strlen($this->buffer);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
 | 
			
		||||
        
 | 
			
		||||
        $endStyle = "\e[0m";
 | 
			
		||||
@@ -85,6 +92,10 @@ class LineRead
 | 
			
		||||
        $returnBuffer = null;
 | 
			
		||||
        while (strlen($keyBuffer)>0) {
 | 
			
		||||
            if ($keyBuffer[0] == "\e") {
 | 
			
		||||
                if (strlen($keyBuffer)==1) {
 | 
			
		||||
                    $keyBuffer = "";
 | 
			
		||||
                    return "\e";
 | 
			
		||||
                }
 | 
			
		||||
                if ($keyBuffer[1] == "[") {
 | 
			
		||||
                    $ctrlChar = substr($keyBuffer, 0,3);
 | 
			
		||||
                    $keyBuffer = substr($keyBuffer, 3);
 | 
			
		||||
@@ -113,6 +124,7 @@ class LineRead
 | 
			
		||||
                    }
 | 
			
		||||
                } elseif ($keyCode == 13) {
 | 
			
		||||
                    $returnBuffer = $this->buffer;
 | 
			
		||||
                    array_unshift($this->history, $this->buffer);
 | 
			
		||||
                    $this->buffer = null;
 | 
			
		||||
                    $this->posCursor = 0;
 | 
			
		||||
                    printf("\n\r");
 | 
			
		||||
@@ -146,7 +158,25 @@ class LineRead
 | 
			
		||||
                $this->redraw();
 | 
			
		||||
                break;
 | 
			
		||||
            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
 | 
			
		||||
                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;
 | 
			
		||||
            default:
 | 
			
		||||
                fprintf(STDERR, "\n%s\n", substr($code,1));
 | 
			
		||||
 
 | 
			
		||||
@@ -300,8 +300,14 @@ class Shell
 | 
			
		||||
            if (!($buffer = $this->lineReader->update())) {
 | 
			
		||||
                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.
 | 
			
		||||
            if ($buffer == "\x03") {
 | 
			
		||||
                $this->dispatchEvent("shell.stop");
 | 
			
		||||
                $this->stop();
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user