diff --git a/src/Readline.php b/src/Readline.php index 0317cae..c92142c 100644 --- a/src/Readline.php +++ b/src/Readline.php @@ -427,8 +427,21 @@ public function setAutocomplete($autocomplete) */ public function redraw() { - // Erase characters from cursor to end of line - $output = "\r\033[K" . $this->prompt; + // Erase characters from cursor to end of line and then redraw actual input + $this->output->write("\r\033[K" . $this->getDrawString()); + + return $this; + } + + /** + * Returns the string that is used to draw the input prompt + * + * @return string + * @internal + */ + public function getDrawString() + { + $output = $this->prompt; if ($this->echo !== false) { if ($this->echo === true) { $buffer = $this->linebuffer; @@ -439,9 +452,8 @@ public function redraw() // write output, then move back $reverse chars (by sending backspace) $output .= $buffer . str_repeat("\x08", $this->strwidth($buffer) - $this->getCursorCell()); } - $this->output->write($output); - return $this; + return $output; } /** diff --git a/src/Stdio.php b/src/Stdio.php index 5cd8acb..c01e840 100644 --- a/src/Stdio.php +++ b/src/Stdio.php @@ -91,8 +91,9 @@ public function pipe(WritableStreamInterface $dest, array $options = array()) public function write($data) { - if ($this->ending || (string)$data === '') { - return; + // return false if already ended, return true if writing empty string + if ($this->ending || $data === '') { + return !$this->ending; } $out = $data; @@ -147,8 +148,7 @@ public function write($data) if ($restoreReadline) { // write output and restore original readline prompt and line buffer - $this->output->write($out); - $this->readline->redraw(); + return $this->output->write($out . $this->readline->getDrawString()); } else { // restore original cursor position in readline prompt $pos = $this->width($this->readline->getPrompt()) + $this->readline->getCursorCell(); @@ -158,7 +158,7 @@ public function write($data) } // write to actual output stream - $this->output->write($out); + return $this->output->write($out); } } diff --git a/tests/StdioTest.php b/tests/StdioTest.php index e127dda..d6f2baa 100644 --- a/tests/StdioTest.php +++ b/tests/StdioTest.php @@ -50,7 +50,7 @@ public function testWriteEmptyStringWillNotWriteToOutput() $output->expects($this->never())->method('write'); - $stdio->write(''); + $this->assertTrue($stdio->write('')); } public function testWriteWillClearReadlineWriteOutputAndRestoreReadline() @@ -72,7 +72,7 @@ public function testWriteWillClearReadlineWriteOutputAndRestoreReadline() $stdio->write('test'); - $this->assertEquals("\r\033[K" . "test\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer); } public function testWriteAgainWillMoveToPreviousLineWriteOutputAndRestoreReadlinePosition() @@ -144,7 +144,7 @@ public function testWriteAgainWithNewlinesWillClearReadlineMoveToPreviousLineWri $stdio->write("ond" . "\n" . "third"); - $this->assertEquals("\r\033[K" . "\033[A" . "\r\033[3C" . "ond\nthird\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "\033[A" . "\r\033[3C" . "ond\nthird\n" . "> input", $buffer); } public function testWriteAfterReadlineInputWillClearReadlineWriteOutputAndRestoreReadline() @@ -170,7 +170,7 @@ public function testWriteAfterReadlineInputWillClearReadlineWriteOutputAndRestor $stdio->writeln('test'); - $this->assertEquals("\r\033[K" . "test\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer); } public function testOverwriteWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline() @@ -194,7 +194,7 @@ public function testOverwriteWillClearReadlineMoveToPreviousLineWriteOutputAndRe $stdio->overwrite('overwrite'); - $this->assertEquals("\r\033[K" . "\033[A" . "\r\033[K" . "overwrite\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "\033[A" . "\r\033[K" . "overwrite\n" . "> input", $buffer); } public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndRestoreReadline() @@ -218,7 +218,7 @@ public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndResto $stdio->overwrite('overwrite'); - $this->assertEquals("\r\033[K" . "overwrite\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "overwrite\n" . "> input", $buffer); } public function testWriteLineWillClearReadlineWriteOutputAndRestoreReadline() @@ -240,7 +240,7 @@ public function testWriteLineWillClearReadlineWriteOutputAndRestoreReadline() $stdio->writeln('test'); - $this->assertEquals("\r\033[K" . "test\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer); } public function testWriteTwoLinesWillClearReadlineWriteOutputAndRestoreReadline() @@ -263,7 +263,7 @@ public function testWriteTwoLinesWillClearReadlineWriteOutputAndRestoreReadline( $stdio->writeln('hello'); $stdio->writeln('world'); - $this->assertEquals("\r\033[K" . "hello\n" . "\r\033[K" . "> input" . "\r\033[K" . "world\n" . "\r\033[K" . "> input", $buffer); + $this->assertEquals("\r\033[K" . "hello\n" . "> input" . "\r\033[K" . "world\n" . "> input", $buffer); } public function testPauseWillBeForwardedToInput() @@ -423,7 +423,7 @@ public function testWriteAfterEndWillNotWriteToOutput() $output->expects($this->never())->method('write'); - $stdio->write('test'); + $this->assertFalse($stdio->write('test')); } public function testEndTwiceWillCloseInputAndEndOutputOnce()