Skip to content

Commit

Permalink
Use cursor class in QuestionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Dec 10, 2019
1 parent 30af066 commit 6ff2395
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/Symfony/Component/Console/Cursor.php
Expand Up @@ -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()
Expand All @@ -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");
}
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '';

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).'</hl>');
// Restore cursor position
$output->write("\0338");
$cursor->restorePosition();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Tests/CursorTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 6ff2395

Please sign in to comment.