Skip to content

Commit acab9fa

Browse files
authored
Merge pull request #37 from clue-labs/empty
Explicitly redraw prompt on empty input
2 parents 2796dae + 18207a4 commit acab9fa

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Readline.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,13 @@ protected function processLine()
539539
{
540540
// store and reset/clear/redraw current input
541541
$line = $this->linebuffer;
542-
$this->setInput('');
542+
if ($line !== '') {
543+
// the line is not empty, reset it (and implicitly redraw prompt)
544+
$this->setInput('');
545+
} elseif ($this->echo !== false) {
546+
// explicitly redraw prompt after empty line
547+
$this->redraw();
548+
}
543549

544550
// process stored input buffer
545551
if ($this->history !== null) {

tests/ReadlineTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,45 @@ public function testSetInputDuringEmitKeepsInput()
511511
$this->assertEquals('test', $readline->getInput());
512512
}
513513

514+
public function testWillRedrawEmptyPromptOnEnter()
515+
{
516+
$this->readline->setPrompt('> ');
517+
518+
$buffer = '';
519+
$this->output->expects($this->atLeastOnce())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
520+
$buffer .= $data;
521+
}));
522+
523+
$this->readline->onKeyEnter();
524+
525+
$this->assertEquals("\n\r\033[K" . "> ", $buffer);
526+
}
527+
528+
public function testWillRedrawEmptyPromptOnEnterWithData()
529+
{
530+
$this->readline->setPrompt('> ');
531+
$this->readline->setInput('test');
532+
533+
$buffer = '';
534+
$this->output->expects($this->atLeastOnce())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
535+
$buffer .= $data;
536+
}));
537+
538+
$this->readline->onKeyEnter();
539+
540+
$this->assertEquals("\n\r\033[K" . "> ", $buffer);
541+
}
542+
543+
public function testWillNotRedrawPromptOnEnterWhenEchoIsOff()
544+
{
545+
$this->readline->setEcho(false);
546+
$this->readline->setPrompt('> ');
547+
548+
$this->output->expects($this->never())->method('write');
549+
550+
$this->readline->onKeyEnter();
551+
}
552+
514553
public function testEmitErrorWillEmitErrorAndClose()
515554
{
516555
$this->readline->on('error', $this->expectCallableOnce());

0 commit comments

Comments
 (0)