8000 [Console][QuestionHelper] Use String width() to properly move the cursor backwards by fancyweb · Pull Request #35578 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console][QuestionHelper] Use String width() to properly move the cursor backwards #35578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Console][QuestionHelper] Use String width() to properly move the cur…
…sor backwards
  • Loading branch information
fancyweb committed Feb 10, 2020
commit 67a1f55ce137cf0d21aa805d24d14e9f6936075d
6 changes: 4 additions & 2 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Terminal;
use function Symfony\Component\String\s;

/**
* The QuestionHelper class provides helpers to interact with the user.
Expand Down Expand Up @@ -242,9 +243,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
} elseif ("\177" === $c) { // Backspace Character
if (0 === $numMatches && 0 !== $i) {
--$i;
$fullChoice = self::substr($fullChoice, 0, $i);
// Move cursor backwards
$output->write("\033[1D");
$output->write(sprintf("\033[%dD", s($fullChoice)->slice(-1)->width(false)));

$fullChoice = self::substr($fullChoice, 0, $i);
}

if (0 === $i) {
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,25 @@ public function testTraversableMultiselectAutocomplete()
$this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
}

public function testAutocompleteMoveCursorBackwards()
{
// F<TAB><BACKSPACE><BACKSPACE><BACKSPACE>
$inputStream = $this->getInputStream("F\t\177\177\177");

$dialog = new QuestionHelper();
$helperSet = new HelperSet([new FormatterHelper()]);
$dialog->setHelperSet($helperSet);

$question = new Question('Question?', 'F⭐Y');
$question->setAutocompleterValues(['F⭐Y']);

$dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);

$stream = $output->getStream();
rewind($stream);
$this->assertStringEndsWith("\033[1D\033[K\033[2D\033[K\033[1D\033[K", stream_get_contents($stream));
}

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"php": "^7.2.5",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php73": "^1.8",
"symfony/service-contracts": "^1.1|^2"
"symfony/service-contracts": "^1.1|^2",
"symfony/string": "^5.1"
},
"require-dev": {
"symfony/config": "^4.4|^5.0",
Expand Down
0