Skip to content

Commit aa8c9cd

Browse files
committed
Merge pull request #23 from clue-labs/move
Direction keys only move cursor if moving is enabled
2 parents b997dec + 10dc427 commit aa8c9cd

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Readline.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,17 @@ public function onKeyInsert()
424424
/** @internal */
425425
public function onKeyHome()
426426
{
427-
$this->moveCursorTo(0);
427+
if ($this->move) {
428+
$this->moveCursorTo(0);
429+
}
428430
}
429431

430432
/** @internal */
431433
public function onKeyEnd()
432434
{
433-
$this->moveCursorTo($this->strlen($this->linebuffer));
435+
if ($this->move) {
436+
$this->moveCursorTo($this->strlen($this->linebuffer));
437+
}
434438
}
435439

436440
/** @internal */

tests/ReadlineTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ public function testSettingInputMovesCursorToEnd()
4242
$this->assertEquals(7, $this->readline->getCursorCell());
4343
}
4444

45+
public function testSettingMoveOffDoesNotAllowDirectionKeysToChangePosition()
46+
{
47+
$this->readline->setInput('test');
48+
$this->readline->setMove(false);
49+
$this->readline->moveCursorTo(2);
50+
51+
$this->readline->onKeyLeft();
52+
$this->assertEquals(2, $this->readline->getCursorPosition());
53+
54+
$this->readline->onKeyRight();
55+
$this->assertEquals(2, $this->readline->getCursorPosition());
56+
57+
$this->readline->onKeyHome();
58+
$this->assertEquals(2, $this->readline->getCursorPosition());
59+
60+
$this->readline->onKeyEnd();
61+
$this->assertEquals(2, $this->readline->getCursorPosition());
62+
}
63+
4564
public function testMultiByteInput()
4665
{
4766
$this->readline->setInput('täst');
@@ -192,6 +211,32 @@ public function testKeysEndMovesToEnd(Readline $readline)
192211
$readline->onKeyEnd();
193212

194213
$this->assertEquals(4, $readline->getCursorPosition());
214+
215+
return $readline;
216+
}
217+
218+
/**
219+
* @depends testKeysEndMovesToEnd
220+
* @param Readline $readline
221+
*/
222+
public function testKeysLeftMovesToLeft(Readline $readline)
223+
{
224+
$readline->onKeyLeft();
225+
226+
$this->assertEquals(3, $readline->getCursorPosition());
227+
228+
return $readline;
229+
}
230+
231+
/**
232+
* @depends testKeysLeftMovesToLeft
233+
* @param Readline $readline
234+
*/
235+
public function testKeysRightMovesToRight(Readline $readline)
236+
{
237+
$readline->onKeyRight();
238+
239+
$this->assertEquals(4, $readline->getCursorPosition());
195240
}
196241

197242
public function testKeysSimpleChars()

0 commit comments

Comments
 (0)