Skip to content

Commit 30bd22a

Browse files
committed
Merge pull request #28 from clue-labs/data
Support Readline::setInput() during 'line' event
2 parents 79cc794 + f68c5ff commit 30bd22a

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/Readline.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,21 +537,20 @@ public function deleteChar($n)
537537

538538
/**
539539
* process the current line buffer, emit event and redraw empty line
540+
*
541+
* @uses self::setInput()
540542
*/
541543
protected function processLine()
542544
{
545+
// store and reset/clear/redraw current input
543546
$line = $this->linebuffer;
547+
$this->setInput('');
544548

545-
$this->emit('data', array($line));
546-
549+
// process stored input buffer
547550
if ($this->history !== null) {
548551
$this->history->addLine($line);
549552
}
550-
551-
$this->linebuffer = '';
552-
$this->linepos = 0;
553-
554-
$this->redraw();
553+
$this->emit('data', array($line));
555554
}
556555

557556
protected function strlen($str)

tests/ReadlineTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,33 @@ public function testCursorCellObeysCustomEchoAsterisk(Readline $readline)
449449
$this->assertEquals(3, $readline->getCursorCell());
450450
}
451451

452+
public function testEmitEmptyInputOnEnter()
453+
{
454+
$this->readline->on('data', $this->expectCallableOnceWith(''));
455+
$this->readline->onKeyEnter();
456+
}
457+
458+
public function testEmitInputOnEnterAndClearsInput()
459+
{
460+
$this->readline->setInput('test');
461+
$this->readline->on('data', $this->expectCallableOnceWith('test'));
462+
$this->readline->onKeyEnter();
463+
464+
$this->assertEquals('', $this->readline->getInput());
465+
}
466+
467+
public function testSetInputDuringEmitKeepsInput()
468+
{
469+
$readline = $this->readline;
470+
471+
$readline->on('data', function ($line) use ($readline) {
472+
$readline->setInput('test');
473+
});
474+
$readline->onKeyEnter();
475+
476+
$this->assertEquals('test', $readline->getInput());
477+
}
478+
452479
private function pushInputBytes(Readline $readline, $bytes)
453480
{
454481
foreach (str_split($bytes, 1) as $byte) {

tests/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ protected function expectCallableNever()
2626
return $mock;
2727
}
2828

29+
protected function expectCallableOnceWith($value)
30+
{
31+
$mock = $this->createCallableMock();
32+
$mock
33+
->expects($this->once())
34+
->method('__invoke')
35+
->with($this->equalTo($value));
36+
37+
return $mock;
38+
}
39+
2940
protected function expectCallableOnceParameter($type)
3041
{
3142
$mock = $this->createCallableMock();

0 commit comments

Comments
 (0)