Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accessor for current cursor position #9

Merged
merged 1 commit into from
May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}