Skip to content

Commit

Permalink
Merge pull request #9 from clue-labs/position
Browse files Browse the repository at this point in the history
Add accessor for current cursor position
  • Loading branch information
clue committed May 14, 2015
2 parents f6c88ab + a4b607c commit 5e8a48f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ public function setMove($move)
return $this;
}

/**
* get current cursor position
*
* cursor position is measured in number of text characters
*
* @return int
* @see self::moveCursorTo() to move the cursor to a given position
* @see self::moveCursorBy() to move the cursor by given number of characters
* @see self::setMove() to toggle whether the user can move the cursor position
*/
public function getCursorPosition()
{
return $this->linepos;
}

/**
* set current text input buffer
*
Expand Down Expand Up @@ -323,11 +338,12 @@ public function deleteChar($n)
* zero or out of range moves are simply ignored
*
* @param int $n
* @return self
* @uses self::moveCursorTo()
*/
public function moveCursorBy($n)
{
$this->moveCursorTo($this->linepos + $n);
return $this->moveCursorTo($this->linepos + $n);
}

/**
Expand All @@ -336,6 +352,7 @@ public function moveCursorBy($n)
* out of range (exceeding current input buffer) are simply ignored
*
* @param int $n
* @return self
* @uses self::redraw()
*/
public function moveCursorTo($n)
Expand All @@ -346,6 +363,8 @@ public function moveCursorTo($n)

$this->linepos = $n;
$this->redraw();

return $this;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,29 @@ public function testSettersReturnSelf()
public function testInputStartsEmpty()
{
$this->assertEquals('', $this->readline->getInput());
$this->assertEquals(0, $this->readline->getCursorPosition());
}

public function testGetInputAfterSetting()
{
$this->assertSame($this->readline, $this->readline->setInput('hello'));
$this->assertEquals('hello', $this->readline->getInput());
$this->assertEquals(5, $this->readline->getCursorPosition());
}

public function testSettingInputMovesCursorToEnd()
{
$this->readline->setInput('hello');
$this->readline->moveCursorTo(3);

$this->readline->setInput('testing');
$this->assertEquals(7, $this->readline->getCursorPosition());
}

public function testMultiByteInput()
{
$this->readline->setInput('täst');
$this->assertEquals('täst', $this->readline->getInput());
$this->assertEquals(4, $this->readline->getCursorPosition());
}
}

0 comments on commit 5e8a48f

Please sign in to comment.