From 18207a46b85fc9c40153d5d1988b8c4cc83f5784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 12 Jun 2016 18:03:19 +0200 Subject: [PATCH] Explicitly redraw prompt on empty input --- src/Readline.php | 8 +++++++- tests/ReadlineTest.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Readline.php b/src/Readline.php index e1c65a3..7870986 100644 --- a/src/Readline.php +++ b/src/Readline.php @@ -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) { diff --git a/tests/ReadlineTest.php b/tests/ReadlineTest.php index 2686daf..93a5780 100644 --- a/tests/ReadlineTest.php +++ b/tests/ReadlineTest.php @@ -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());