Bugfixed history logic, example tweaks

This commit is contained in:
2024-03-02 02:17:32 +01:00
parent fdbcc7adae
commit 266c538f01
2 changed files with 14 additions and 7 deletions

View File

@ -82,7 +82,7 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
public function setPrompt(string $prompt): void
{
$this->prompt = $prompt;
$this->promptWidth = mb_strlen($this->prompt);
$this->promptWidth = mb_strlen(preg_replace('<(\e\[[0-9;]+?m)>m', '', $this->prompt));
}
private function handleInput($v) {
@ -99,6 +99,7 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
// Update the history
array_unshift($this->history, $this->buffer);
while (count($this->history) > $this->maxHistory) array_pop($this->history);
$this->historyIndex = 0;
// Parse and empty the buffer
$buffer = str_getcsv(trim($this->buffer), " ");
$this->buffer = '';
@ -128,12 +129,12 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
if ($this->historyIndex === null && count($this->history) > 0) {
$this->historyCache = $this->buffer;
$this->historyIndex = 0;
$this->buffer = $this->history[$this->historyIndex];
$this->buffer = $this->history[$this->historyIndex]??'';
} elseif ($this->historyIndex < count($this->history) - 1) {
$this->historyIndex++;
$this->buffer = $this->history[$this->historyIndex];
$this->buffer = $this->history[$this->historyIndex]??'';
}
$this->cursorPos = min(strlen($this->buffer), $this->cursorPos) - $this->scrollOffset;
$this->cursorPos = mb_strlen($this->buffer);
break;
case "\e[B": // down
if ($this->historyIndex === null) {
@ -144,9 +145,9 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
$this->historyCache = null;
} else {
$this->historyIndex--;
$this->buffer = $this->history[$this->historyIndex];
$this->buffer = $this->history[$this->historyIndex]??'';
}
$this->cursorPos = min(strlen($this->buffer), $this->cursorPos) - $this->scrollOffset;
$this->cursorPos = mb_strlen($this->buffer);
break;
case "\e[H": // home