Skip to content

Commit 398de09

Browse files
committed
Add addInput() helper method
1 parent 56e6e3d commit 398de09

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,25 @@ $readline->setEcho(true);
224224
Everything the user types will be buffered in the current *user input buffer*.
225225
Once the user hits enter, the *user input buffer* will be processed and cleared.
226226

227+
The `addInput($input)` method can be used to add text to the *user input
228+
buffer* at the current cursor position.
229+
The given text will be inserted just like the user would type in a text and as
230+
such adjusts the current cursor position accordingly.
231+
The user will be able to delete and/or rewrite the buffer at any time.
232+
Changing the *user input buffer* can be useful for presenting a preset input to
233+
the usser (like the last password attempt).
234+
Simply pass an input string like this:
235+
236+
```php
237+
$readline->addInput('hello');
238+
```
239+
227240
The `setInput($buffer)` method can be used to control the *user input buffer*.
241+
The given text will be used to replace the entire current *user input buffer*
242+
and as such adjusts the current cursor position to the end of the new buffer.
228243
The user will be able to delete and/or rewrite the buffer at any time.
229-
Changing the *user input buffer* can be useful for presenting a preset input to the user
230-
(like the last password attempt).
244+
Changing the *user input buffer* can be useful for presenting a preset input to
245+
the user (like the last password attempt).
231246
Simply pass an input string like this:
232247

233248
```php

src/Readline.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,36 @@ public function moveCursorTo($n)
276276
return $this;
277277
}
278278

279+
/**
280+
* Appends the given input to the current text input buffer at the current position
281+
*
282+
* This moves the cursor accordingly to the number of characters added.
283+
*
284+
* @param string $input
285+
* @return self
286+
* @uses self::redraw()
287+
*/
288+
public function addInput($input)
289+
{
290+
if ($input === '') {
291+
return $this;
292+
}
293+
294+
// read everything up until before current position
295+
$pre = $this->substr($this->linebuffer, 0, $this->linepos);
296+
$post = $this->substr($this->linebuffer, $this->linepos);
297+
298+
$this->linebuffer = $pre . $input . $post;
299+
$this->linepos += $this->strlen($input);
300+
301+
// only redraw if input should be echo'ed (i.e. is not hidden anyway)
302+
if ($this->echo !== false) {
303+
$this->redraw();
304+
}
305+
306+
return $this;
307+
}
308+
279309
/**
280310
* set current text input buffer
281311
*
@@ -694,14 +724,7 @@ public function onKeyDown()
694724
*/
695725
public function onFallback($chars)
696726
{
697-
// read everything up until before current position
698-
$pre = $this->substr($this->linebuffer, 0, $this->linepos);
699-
$post = $this->substr($this->linebuffer, $this->linepos);
700-
701-
$this->linebuffer = $pre . $chars . $post;
702-
$this->linepos += $this->strlen($chars);
703-
704-
$this->redraw();
727+
$this->addInput($chars);
705728
}
706729

707730
/**

tests/ReadlineTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ public function testGetInputAfterSetting()
4040
$this->assertEquals(5, $this->readline->getCursorCell());
4141
}
4242

43+
public function testAddInputAfterSetting()
44+
{
45+
$this->readline->setInput('hello');
46+
47+
$this->assertSame($this->readline, $this->readline->addInput(' world'));
48+
$this->assertEquals('hello world', $this->readline->getInput());
49+
$this->assertEquals(11, $this->readline->getCursorPosition());
50+
$this->assertEquals(11, $this->readline->getCursorCell());
51+
}
52+
53+
public function testAddInputAfterSettingCurrentCursorPosition()
54+
{
55+
$this->readline->setInput('hello');
56+
$this->readline->moveCursorTo(2);
57+
58+
$this->assertSame($this->readline, $this->readline->addInput('ha'));
59+
$this->assertEquals('hehallo', $this->readline->getInput());
60+
$this->assertEquals(4, $this->readline->getCursorPosition());
61+
$this->assertEquals(4, $this->readline->getCursorCell());
62+
}
63+
4364
public function testPromptAfterSetting()
4465
{
4566
$this->assertSame($this->readline, $this->readline->setPrompt('> '));
@@ -180,6 +201,22 @@ public function testSettingInputWithoutEchoDoesNotNeedToRedraw()
180201
$this->assertSame($this->readline, $this->readline->setInput('test'));
181202
}
182203

204+
public function testAddingEmptyInputDoesNotNeedToRedraw()
205+
{
206+
$this->output->expects($this->never())->method('write');
207+
208+
$this->assertSame($this->readline, $this->readline->addInput(''));
209+
}
210+
211+
public function testAddingInputWithoutEchoDoesNotNeedToRedraw()
212+
{
213+
$this->readline->setEcho(false);
214+
215+
$this->output->expects($this->never())->method('write');
216+
217+
$this->assertSame($this->readline, $this->readline->addInput('test'));
218+
}
219+
183220
public function testMovingCursorWithoutEchoDoesNotNeedToRedraw()
184221
{
185222
$this->readline->setEcho(false);

0 commit comments

Comments
 (0)