Skip to content

Commit a40a30e

Browse files
committed
Adjust current history position when truncating to limit
1 parent 6664dcd commit a40a30e

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/Readline.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,13 @@ public function getInput()
319319
*
320320
* @param string $line
321321
* @return self
322+
* @uses self::limitHistory() to make sure list does not exceed limits
322323
*/
323324
public function addHistory($line)
324325
{
325326
$this->historyLines []= $line;
326327

327-
if ($this->historyLimit !== null) {
328-
$this->historyLines = array_slice($this->historyLines, -$this->historyLimit, $this->historyLimit);
329-
}
330-
331-
return $this;
328+
return $this->limitHistory($this->historyLimit);
332329
}
333330

334331
/**
@@ -369,7 +366,20 @@ public function limitHistory($limit)
369366
{
370367
$this->historyLimit = $limit === null ? null : (int)$limit;
371368

372-
if ($this->historyLimit !== null) {
369+
// limit send and currently exceeded
370+
if ($this->historyLimit !== null && isset($this->historyLines[$this->historyLimit])) {
371+
// adjust position in history according to new position after applying limit
372+
if ($this->historyPosition !== null) {
373+
$this->historyPosition -= count($this->historyLines) - $this->historyLimit;
374+
375+
// current position will drop off from list => restore original
376+
if ($this->historyPosition < 0) {
377+
$this->setInput($this->historyUnsaved);
378+
$this->historyPosition = null;
379+
$this->historyUnsaved = null;
380+
}
381+
}
382+
373383
$this->historyLines = array_slice($this->historyLines, -$this->historyLimit, $this->historyLimit);
374384
}
375385

@@ -543,14 +553,15 @@ public function onKeyDown()
543553
return;
544554
}
545555

546-
if (($this->historyPosition + 1) < count($this->historyLines)) {
556+
if (isset($this->historyLines[$this->historyPosition + 1])) {
547557
// this is still a valid position => advance by one and apply
548558
$this->historyPosition++;
549559
$this->setInput($this->historyLines[$this->historyPosition]);
550560
} else {
551561
// moved beyond bottom => restore original unsaved input
552562
$this->setInput($this->historyUnsaved);
553563
$this->historyPosition = null;
564+
$this->historyUnsaved = null;
554565
}
555566
}
556567

tests/ReadlineTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,4 +839,67 @@ public function testHistoryLimitUnlimitedDoesNotTruncate()
839839

840840
$this->assertCount(1000, $this->readline->listHistory());
841841
}
842+
843+
public function testHistoryLimitRestoresOriginalInputIfCurrentIsTruncated()
844+
{
845+
$this->readline->addHistory('a');
846+
$this->readline->addHistory('b');
847+
848+
$this->readline->setInput('hello');
849+
850+
$this->readline->onKeyUp();
851+
852+
$this->readline->limitHistory(0);
853+
854+
$this->assertEquals('hello', $this->readline->getInput());
855+
}
856+
857+
public function testHistoryLimitKeepsCurrentIfCurrentRemainsDespiteTruncation()
858+
{
859+
$this->readline->addHistory('a');
860+
$this->readline->addHistory('b');
861+
862+
$this->readline->onKeyUp();
863+
864+
$this->readline->limitHistory(1);
865+
866+
$this->assertEquals('b', $this->readline->getInput());
867+
}
868+
869+
public function testHistoryLimitOnlyInBetweenTruncatesToLastAndKeepsInput()
870+
{
871+
$this->readline->addHistory('a');
872+
$this->readline->addHistory('b');
873+
874+
$this->readline->onKeyUp();
875+
876+
$this->readline->limitHistory(3);
877+
878+
$this->assertEquals('b', $this->readline->getInput());
879+
880+
$this->readline->addHistory('c');
881+
$this->readline->addHistory('d');
882+
883+
$this->assertCount(3, $this->readline->listHistory());
884+
$this->assertEquals(array('b', 'c', 'd'), $this->readline->listHistory());
885+
886+
$this->assertEquals('b', $this->readline->getInput());
887+
}
888+
889+
public function testHistoryLimitRestoresOriginalIfCurrentIsTruncatedDueToAdding()
890+
{
891+
$this->readline->addHistory('a');
892+
$this->readline->addHistory('b');
893+
894+
$this->readline->setInput('hello');
895+
896+
$this->readline->onKeyUp();
897+
898+
$this->readline->limitHistory(1);
899+
900+
$this->readline->addHistory('c');
901+
$this->readline->addHistory('d');
902+
903+
$this->assertEquals('hello', $this->readline->getInput());
904+
}
842905
}

0 commit comments

Comments
 (0)