Bugfixes in lineread, implemented command history
This commit is contained in:
		@@ -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));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user