Skip to content

Commit 5de09dd

Browse files
committed
Support restoring current user input when cycling history
1 parent f58f800 commit 5de09dd

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Readline/MemoryHistory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class MemoryHistory implements History
88
{
99
private $lines = array();
1010
private $position = null;
11+
private $unsaved = null;
1112

1213
public function addLine($line)
1314
{
@@ -29,8 +30,7 @@ public function moveUp(Readline $readline)
2930
if ($this->position === null) {
3031
// first time up => move to last entry
3132
$this->position = count($this->lines) - 1;
32-
33-
// TODO: buffer current user input
33+
$this->unsaved = $readline->getInput();
3434
} else {
3535
// somewhere in the list => move by one
3636
$this->position--;
@@ -50,8 +50,8 @@ public function moveDown(Readline $readline)
5050
$this->position++;
5151
$readline->setInput($this->lines[$this->position]);
5252
} else {
53-
// moved beyond bottom => restore empty input
54-
$readline->setInput('');
53+
// moved beyond bottom => restore original unsaved input
54+
$readline->setInput($this->unsaved);
5555
$this->position = null;
5656
}
5757
}

tests/Readline/MemoryHistoryTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,28 @@ public function testMovingInEmptyHistoryDoesNothing()
7474
$this->history->moveUp($this->readline);
7575
$this->history->moveDown($this->readline);
7676
}
77+
78+
public function testMovingUpClearsCurrentInput()
79+
{
80+
$this->readline->expects($this->once())->method('getInput')->will($this->returnValue('input'));
81+
82+
$this->history->addLine('first');
83+
84+
$this->readline->expects($this->once())->method('setInput')->with($this->equalTo('first'));
85+
86+
$this->history->moveUp($this->readline);
87+
88+
return $this->history;
89+
}
90+
91+
/**
92+
* @depends testMovingUpClearsCurrentInput
93+
* @param History $history
94+
*/
95+
public function testMovingDownRestoresLastBufferAgain(History $history)
96+
{
97+
$this->readline->expects($this->once())->method('setInput')->with($this->equalTo('input'));
98+
99+
$history->moveDown($this->readline);
100+
}
77101
}

0 commit comments

Comments
 (0)