From 6ff23957114b39eb6215d13449b569e9465b49ad Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Tue, 10 Dec 2019 15:41:36 +0200 Subject: [PATCH] Use cursor class in QuestionHelper --- src/Symfony/Component/Console/Cursor.php | 14 +++++++++----- .../Component/Console/Helper/QuestionHelper.php | 11 +++++++---- src/Symfony/Component/Console/Tests/CursorTest.php | 8 ++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Console/Cursor.php b/src/Symfony/Component/Console/Cursor.php index 37947cd0de22b..a6b05d8514f76 100644 --- a/src/Symfony/Component/Console/Cursor.php +++ b/src/Symfony/Component/Console/Cursor.php @@ -57,12 +57,12 @@ public function moveToPosition(int $column, int $row) public function savePosition() { - $this->output->write("\x1b[s"); + $this->output->write("\x1b7"); } public function restorePosition() { - $this->output->write("\x1b[u"); + $this->output->write("\x1b8"); } public function hide() @@ -76,11 +76,15 @@ public function show() } /** - * Clears all the output from the of the current line. + * Clears all the output from the current line. */ - public function clearLine() + public function clearLine(bool $fromCurrentPosition = false) { - $this->output->write("\x1b[2K"); + if (true === $fromCurrentPosition) { + $this->output->write("\x1b[K"); + } else { + $this->output->write("\x1b[2K"); + } } /** diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index faf2648bd1bd1..589a177109bdb 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Helper; +use Symfony\Component\Console\Cursor; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Formatter\OutputFormatterStyle; @@ -203,6 +204,8 @@ protected function writeError(OutputInterface $output, \Exception $error) */ private function autocomplete(OutputInterface $output, Question $question, $inputStream, callable $autocomplete): string { + $cursor = new Cursor($output); + $fullChoice = ''; $ret = ''; @@ -232,7 +235,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu --$i; $fullChoice = self::substr($fullChoice, 0, $i); // Move cursor backwards - $output->write("\033[1D"); + $cursor->moveLeft(); } if (0 === $i) { @@ -319,16 +322,16 @@ function ($match) use ($ret) { } // Erase characters from cursor to end of line - $output->write("\033[K"); + $cursor->clearLine(true); if ($numMatches > 0 && -1 !== $ofs) { // Save cursor position - $output->write("\0337"); + $cursor->savePosition(); // Write highlighted text, complete the partially entered response $charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))); $output->write(''.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).''); // Restore cursor position - $output->write("\0338"); + $cursor->restorePosition(); } } diff --git a/src/Symfony/Component/Console/Tests/CursorTest.php b/src/Symfony/Component/Console/Tests/CursorTest.php index 3151575a9574e..08e84fa2cdd55 100644 --- a/src/Symfony/Component/Console/Tests/CursorTest.php +++ b/src/Symfony/Component/Console/Tests/CursorTest.php @@ -19,12 +19,12 @@ class CursorTest extends TestCase { protected $stream; - protected function setUp() + protected function setUp(): void { $this->stream = fopen('php://memory', 'r+'); } - protected function tearDown() + protected function tearDown(): void { fclose($this->stream); $this->stream = null; @@ -135,7 +135,7 @@ public function testSavePosition() $cursor->savePosition(); - $this->assertEquals("\x1b[s", $this->getOutputContent($output)); + $this->assertEquals("\x1b7", $this->getOutputContent($output)); } public function testHide() @@ -162,7 +162,7 @@ public function testRestorePosition() $cursor->restorePosition(); - $this->assertEquals("\x1b[u", $this->getOutputContent($output)); + $this->assertEquals("\x1b8", $this->getOutputContent($output)); } public function testClearOutput()