From 3f05fafc2b59ddf4b3d10a89d0781860dbc79a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 14 May 2015 17:15:50 +0200 Subject: [PATCH] Add accessors for text input buffer. See Readline::setInput() and Readline::getInput() --- src/Readline.php | 29 +++++++++++++++++++++++++++++ tests/ReadlineTest.php | 11 +++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Readline.php b/src/Readline.php index 0afc161..dae85b8 100644 --- a/src/Readline.php +++ b/src/Readline.php @@ -110,6 +110,35 @@ public function setMove($move) return $this; } + /** + * set current text input buffer + * + * this moves the cursor to the end of the current + * input buffer (if any). + * + * @param string $input + * @return self + * @uses self::redraw() + */ + public function setInput($input) + { + $this->linebuffer = $input; + $this->linepos = $this->strlen($this->linebuffer); + $this->redraw(); + + return $this; + } + + /** + * get current text input buffer + * + * @return string + */ + public function getInput() + { + return $this->linebuffer; + } + /** * set history handler to use (or none) * diff --git a/tests/ReadlineTest.php b/tests/ReadlineTest.php index 7eb07ea..20e507e 100644 --- a/tests/ReadlineTest.php +++ b/tests/ReadlineTest.php @@ -16,4 +16,15 @@ public function testSettersReturnSelf() $this->assertSame($this->readline, $this->readline->setMove(true)); $this->assertSame($this->readline, $this->readline->setPrompt('')); } + + public function testInputStartsEmpty() + { + $this->assertEquals('', $this->readline->getInput()); + } + + public function testGetInputAfterSetting() + { + $this->assertSame($this->readline, $this->readline->setInput('hello')); + $this->assertEquals('hello', $this->readline->getInput()); + } }