8000 [Console][QuestionHelper] Use String width() to properly move the cur… · symfony/symfony@67a1f55 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67a1f55

Browse files
committed
[Console][QuestionHelper] Use String width() to properly move the cursor backwards
1 parent 9eb7cb1 commit 67a1f55

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Console\Question\ChoiceQuestion;
2323
use Symfony\Component\Console\Question\Question;
2424
use Symfony\Component\Console\Terminal;
25+
use function Symfony\Component\String\s;
2526

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

250252
if (0 === $i) {

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,25 @@ public function testTraversableMultiselectAutocomplete()
797797
$this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
798798
}
799799

800+
public function testAutocompleteMoveCursorBackwards()
801+
{
802+
// F<TAB><BACKSPACE><BACKSPACE><BACKSPACE>
803+
$inputStream = $this->getInputStream("F\t\177\177\177");
804+
805+
$dialog = new QuestionHelper();
806+
$helperSet = new HelperSet([new FormatterHelper()]);
807+
$dialog->setHelperSet($helperSet);
808+
809+
$question = new Question('Question?', 'F⭐Y');
810+
$question->setAutocompleterValues(['F⭐Y']);
811+
812+
$dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
813+
814+
$stream = $output->getStream();
815+
rewind($stream);
816+
$this->assertStringEndsWith("\033[1D\033[K\033[2D\033[K\033[1D\033[K", stream_get_contents($stream));
817+
}
818+
800819
protected function getInputStream($input)
801820
{
802821
$stream = fopen('php://memory', 'r+', false);

src/Symfony/Component/Console/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": "^7.2.5",
2020
"symfony/polyfill-mbstring": "~1.0",
2121
"symfony/polyfill-php73": "^1.8",
22-
"symfony/service-contracts": "^1.1|^2"
22+
"symfony/service-contracts": "^1.1|^2",
23+
"symfony/string": "^5.1"
2324
},
2425
"require-dev": {
2526
"symfony/config": "^4.4|^5.0",

0 commit comments

Comments
 (0)
0