Skip to content

Commit

Permalink
Merge pull request #37 from clue-labs/empty
Browse files Browse the repository at this point in the history
Explicitly redraw prompt on empty input
  • Loading branch information
clue authored Jun 12, 2016
2 parents 2796dae + 18207a4 commit acab9fa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,13 @@ protected function processLine()
{
// store and reset/clear/redraw current input
$line = $this->linebuffer;
$this->setInput('');
if ($line !== '') {
// the line is not empty, reset it (and implicitly redraw prompt)
$this->setInput('');
} elseif ($this->echo !== false) {
// explicitly redraw prompt after empty line
$this->redraw();
}

// process stored input buffer
if ($this->history !== null) {
Expand Down
39 changes: 39 additions & 0 deletions tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,45 @@ public function testSetInputDuringEmitKeepsInput()
$this->assertEquals('test', $readline->getInput());
}

public function testWillRedrawEmptyPromptOnEnter()
{
$this->readline->setPrompt('> ');

$buffer = '';
$this->output->expects($this->atLeastOnce())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
}));

$this->readline->onKeyEnter();

$this->assertEquals("\n\r\033[K" . "> ", $buffer);
}

public function testWillRedrawEmptyPromptOnEnterWithData()
{
$this->readline->setPrompt('> ');
$this->readline->setInput('test');

$buffer = '';
$this->output->expects($this->atLeastOnce())->method('write')->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
}));

$this->readline->onKeyEnter();

$this->assertEquals("\n\r\033[K" . "> ", $buffer);
}

public function testWillNotRedrawPromptOnEnterWhenEchoIsOff()
{
$this->readline->setEcho(false);
$this->readline->setPrompt('> ');

$this->output->expects($this->never())->method('write');

$this->readline->onKeyEnter();
}

public function testEmitErrorWillEmitErrorAndClose()
{
$this->readline->on('error', $this->expectCallableOnce());
Expand Down

0 comments on commit acab9fa

Please sign in to comment.